android – 如何创建listview onItemclicklistener

前端之家收集整理的这篇文章主要介绍了android – 如何创建listview onItemclicklistener前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想创建listview.我得到了 JSON中的值.这里我从json获取值到listview,但是我无法获取onItemclickListener方法.为什么这种情况我不知道,请帮我我的编码如下所示.

Logcat没有显示任何内容.

谢谢.

<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);
        }
    });


}
}

我也用这个

contests_listView.setItemsCanFocus(true);

解决方法

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

猜你在找的Android相关文章