Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
372 views
in Technique[技术] by (71.8m points)

android - How to create listview onItemclicklistener

I want to create listview. I got the value from the JSON. Here I got the value from the json into listview but i can't get the onItemclickListener method. Why this is happening I don't know ,Please help me my coding as shown below.

Logcat is not displaying anything.

Thanks.

<ListView
    android:id="@+id/contests_listView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:layout_below="@+id/header"
    android:focusable="true">
</ListView>

Activity.java

public class Activites_Activity extends CheerfoolznativeActivity {

private ListView contests_listView;
private ProgressBar pgb;
ActivitiesBean bean;
ArrayList<Object> listActivities;
ListAdapter adapter;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_listview);
    setHeader("Activites");


    contests_listView = (ListView) findViewById(R.id.contests_listView);
    pgb = (ProgressBar) findViewById(R.id.contests_progressBar);
    listActivities = new ArrayList<Object>();

    new FetchActivitesTask().execute();

}

public class FetchActivitesTask extends AsyncTask<Void, Void, Void> {

    int i =0;
    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
         pgb.setVisibility(View.VISIBLE);
    }

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub

        String url = "my json URL";
        String strResponse = util.makeWebCall(url);

        try {

            //my json logic here        
        }
        catch (JSONException e) {

            e.printStackTrace();

        }

        return null;
    }

    @Override
    public void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);

        pgb.setVisibility(View.GONE);
        displayAdapter();
    }
}

public void displayAdapter()
{
    adapter = new ListAdapter(this, listActivities);
    contests_listView.setAdapter(adapter);
    contests_listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1,
                int position, long id) {

            // Toast.makeText(getApplicationContext(),"Title => "+items.get(position), Toast.LENGTH_SHORT).show();

            System.out.println("=========== Click");
            bean = (ActivitiesBean) adapter.getItem(position);

            Intent in1 = new Intent(Activites_Activity.this, Activity_display.class);
            in1.putExtra("ActivityObject", bean);
            startActivity(in1);
        }
    });


}
}

I also use this

contests_listView.setItemsCanFocus(true);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this :

contests_listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
        String item = (String) contests_listView.getItemAtPosition(position);
        Toast.makeText(this,"You selected : " + item,Toast.LENGTH_SHORT).show();                
    }
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...