简体   繁体   中英

Get position from List Adapter

I'm trying to get position of item selected in my list adapter...I want to pass the day string to next activity Not sure how to do this.. Here is my code:

private void setListAdapter(ListAdapter adapter) {
    // TODO Auto-generated method stub
}


//create a dialog with list of days 
public ListAdapter createAdapter() {

    Calendar c = Calendar.getInstance();

    // Set the calendar to monday of the current week
    //c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);

    // Print dates of the current week starting on Monday
    DateFormat df = new SimpleDateFormat("EEE dd/MM/yyyy");
    for (int i = 0; i < 14; i++) {
        dateStringArray.add(df.format(c.getTime()));

        c.add(Calendar.DATE, 1);

    }
    //adding additional option (remove) to the planner dialog 
    //dateStringArray.add("Remove");
    // Create a simple array adapter (of type string) with the test values
    weekadapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_single_choice,
            dateStringArray);
    //((ArrayList<String>) adapter).add("whatever data3");
    return weekadapter;
}   

The Simplest way to obtain the Position of item from a ListView is as below.

 ListView.setOnItemClickListener(new OnItemClickListener() {

        @Override
       public void onItemClick(AdapterView<?> arg0, View arg1,
            int position, long arg3) {
        // TODO Auto-generated method stub
        String cn = -- YOUR ARRAY NAME---.get(position).toString();

        Intent intent = new Intent(this,---NEXT ACTIVITY--.class);

        intent.putExtra("cn", cn);
        startActivity(intent);
            }
        });

Put the code above in onCreate method of your activity.

It will fetch the string of item's position and lauch next activity and take selected position string ahead using intent

使用ArrayAdapter.getPosition(item)获取项目的位置。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM