Domanda

I know there is a lot of information out there about using the onItemClickListener and list view but I am new to android development and cannot seem to get it working.

I am not quite sure where I should add the listener so I would really appreciate some help and guidance.

I have two files, the main activity:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.menu);

    ArrayList<GroceryList> menuitems = getItems();

    ListView listView = (ListView) findViewById(R.id.Menu);
    listView.setAdapter(new GroceryListAdapter(this, R.layout.categorymenu, menuitems));        
}

and the ListAdapter File:

public class GroceryListAdapter extends ArrayAdapter<GroceryList> {
private ArrayList<GroceryList> grocerylists;
private Activity activity;
public ImageManager imageManager;

public GroceryListAdapter(Activity a, int textViewResourceId, ArrayList<GroceryList> grocerylists) {
    super(a, textViewResourceId, grocerylists);
    this.grocerylists = grocerylists;
    activity = a;

}

public static class ViewHolder{
    public TextView name;
    public TextView message;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    ViewHolder holder;
    if (v == null) {        
        LayoutInflater vi = 
            (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.categorymenu, null);
        holder = new ViewHolder();
        holder.name = (TextView) v.findViewById(R.id.categoryname);
        holder.message = (TextView) v.findViewById(R.id.message);
        v.setTag(holder);
    }
    else
        holder=(ViewHolder)v.getTag();

    final GroceryList grocerylist = grocerylists.get(position);
    if (alcohollist != null) {
        holder.name.setText(grocerylist.name);
        holder.message.setText(grocerylist.message);
    }
    return v;
}

I am sorry if I am asking a question that has already been answered but I spent a lot of time trying to figure it out for myself but with no success.

I hope some one with more experience than myself will be able to tell me where and how I should add the onItemClickListen method.

Thanks!

È stato utile?

Soluzione

By the looks of it you are using a regular Activity, so you should add this:

 listView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {              
               Toast.makeText(getApplicationContext(), "You clicked item at position"+position,
              Toast.LENGTH_SHORT).show();

            }
        });

BEFORE the .setAdapter in your main activity. That should work.

Altri suggerimenti

In your Main activity:

listView.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {
      // When clicked, show a toast with the TextView text
      Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
          Toast.LENGTH_SHORT).show();
    }
  });
 @Override
    protected void onListItemClick(ListView l, View v, int position, long id){
       // startActivity( new Intent());

        Intent i = new Intent(this,"Next_Activity_Name".class);
        i.putExtra("selected",(int)selected_position);
        final int resultCode = 2;
        startActivityForResult(i,resultCode);



}

You have to add this piece of code into ur main activity after the onCreate() method..

Write down the following code after setting the adapter.

listView.setOnItemClickListener(new OnItemClickListener(){

        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub

        }});

Handle the functionality that you want to do on click, under the onItemClick method.

you have use custom adapter so you can set touchListner at perticular widget which you define in categorymenu layout. it is quit easy to do this but

if you use simple listview then

Add parameter in listview :

lview.setOnItemClickListener(this);

public void onListItemClick(ListView parent, View v, int position, long id) {
    // do with list-view item Position 
}

For more ....

Here so many answers, But it seem to be you didn't understood what they are telling. I will also try to give answer.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.menu);

    ArrayList<GroceryList> menuitems = getItems();

    ListView listView = (ListView) findViewById(R.id.Menu);
    listView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
            // do whatever you want on clicking any list itm
        }
    });
    listView.setAdapter(new GroceryListAdapter(this, R.layout.categorymenu, menuitems));        
}

I hope you will understand it.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top