web-dev-qa-db-fra.com

Comment créer listview surItemclicklistener

Je veux créer un listview. J'ai eu la valeur du JSON. Ici, j'ai obtenu la valeur de json dans listview mais je ne peux pas obtenir la méthode onItemclickListener. Pourquoi cela se produit je ne sais pas, aidez-moi s'il vous plaît mon codage comme indiqué ci-dessous.

Logcat n'affiche rien.

Merci.

<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>

Activité.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);
        }
    });


}
}

J'utilise aussi ça 

contests_listView.setItemsCanFocus(true);
15
Rahul Patel

Essaye ça : 

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();                
    }
});
26
GAMA

Essayez ce code: 

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();
}
0
K.Muthu

Utilisez ceci:

 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);
        }
    });
0
Praveen Singh