Q如何创建ListView OnEmClickListener

我想创建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>
<活动> 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);
2012-04-24 09:31:25  Rahul Patel

A回答

  • 1

    试试这个:

    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();                
        }
    });
    
    2017-10-25 07:19:55  AlexTa GAMA
    • 谢谢,但不起作用,如果有其他解决方案,请告诉我。 – Rahul Patel Apr 24 '12 at 10:24
    • 但是您得到了什么错误?如果logcat没有输出,请尝试通过ddms重置adb。 – GAMA Apr 24 '12 at 10:32
    • 1
      我得到的contests_listView必须声明为final? – Dan Dec 13 '16 at 21:03
  • 2

    请尝试以下代码:

    protected void onListItemClick(ListView l, View v, int position, long id) {
    
    super.onListItemClick(l, v, position, id);
    
    Toast.makeText(this, "message", Toast.LENGTH_LONG).show();
    }
    
    2012-11-15 11:34:59  Adinia K.Muthu
  • 3

    使用这个:

     contests_listView.setOnItemClickListener(new AdapaterView.OnItemClickListener() {
    
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                    int position, long id) {
    
                Intent in1 = new Intent(Activites_Activity.this, Activity_display.class);
    
                startActivity(in1);
            }
        });
    
    2017-02-19 12:13:08  Johnathan Logan Praveen Singh