noobcoder
noobcoder

Reputation: 12841

Displaying the listview items options on clicking the Clickable list

I am trying to display a toast message for the user to display what Item he has selected. I have passed on the list as an intent from an another class and received it in the class whose code is as follows:

public class ListViewDelete extends ListActivity {



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_list_view_delete);

    final Intent intent = getIntent();
    final Bundle extras = getIntent().getExtras();    //gets the GWID

    final MySQLitehelper dbhelper = new MySQLitehelper(this);
    ArrayList<String> thelist = new ArrayList<String>(extras.getStringArrayList(SelectOptions.EXTRA_MESSAGE));
    setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,extras.getStringArrayList(SelectOptions.EXTRA_MESSAGE)));
}       

public void onListItemClick(ListView parent, View view, int position, long id)
{
    Toast.makeText(this, "You have selected", Toast.LENGTH_LONG).show();
}
}

In the last onListItemClick, how do I customize that after the "You have selected", i can put the value from the arraylist item defined above ?

Upvotes: 1

Views: 840

Answers (2)

mango
mango

Reputation: 5636

if you have your index and the arraylist, then you can refer to your string in the collection by index:

public class ListViewDelete extends ListActivity {

    private ArrayList<String> thelist;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_list_view_delete);

        final Intent intent = getIntent();
        final Bundle extras = getIntent().getExtras();    //gets the GWID

        final MySQLitehelper dbhelper = new MySQLitehelper(this);
        thelist = new ArrayList<String>(extras.getStringArrayList(SelectOptions.EXTRA_MESSAGE));
        setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,extras.getStringArrayList(SelectOptions.EXTRA_MESSAGE)));
    }       

    public void onListItemClick(ListView parent, View view, int position, long id)
    {
        Toast.makeText(this, "You have selected" + thelist.get(position), Toast.LENGTH_LONG).show();
    }
}

Note that i made the arrayList a field in order to be able to refer to it from the other method.

Upvotes: 1

Venkat
Venkat

Reputation: 3457

 public void onListItemClick(ListView parent, View view, int position, long id)
{
Toast.makeText(this, "You have selected"+position, Toast.LENGTH_LONG).show();
}

Upvotes: 2

Related Questions