0

I added to my class MyActivity the following :

private void updateMyList(){
    listing=new ArrayList<listing>();
    for(int i =0;i<10;i++)
    {
        Users user=new Users();
        user.setListingName("Name" + i);
        user.setListingPhone("i" + i);
        listing.add(user);
    }
    MyListAdapter lfa = new MyListAdapter(this, listing);
    ((ListView)findViewById(R.id.listFeed)).setAdapter(lfa);

}

This code generates 10 list views so I'd like to add a click listener, so when I click on one of the 10 lists, I get a message or sthing.

Thank you for your help.

1 Answer 1

2

I don't see why you couldn't just add an onItemClickListener in your loop to your ListView. In short, use your Adapter to create the list then just attach the listener:

    lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, connections.toArray(new String[connections.size()])));
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View item, int position, long id) {

                String item = (String) lv.getItemAtPosition(position);

            }
        });

This is if you want to know which item in each list was clicked, there is an setOnClickListener method as well in case you just want to know whether a ListView has been clicked or not.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.