Ronny vdb
Ronny vdb

Reputation: 2474

Need Help ListView and onItemClickListener

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!

Upvotes: 1

Views: 1197

Answers (6)

Sander van&#39;t Veer
Sander van&#39;t Veer

Reputation: 5980

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.

Upvotes: 1

Yugandhar Babu
Yugandhar Babu

Reputation: 10349

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.

Upvotes: 0

Nikunj Patel
Nikunj Patel

Reputation: 22076

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

Upvotes: 0

Anil
Anil

Reputation: 153

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.

Upvotes: 0

himanshu
himanshu

Reputation: 1980

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

Upvotes: 0

user370305
user370305

Reputation: 109257

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

Upvotes: 0

Related Questions