1

我正在尝试为用户显示一条 toast 消息,以显示他选择的项目。我已将列表作为来自另一个类的意图传递,并在代码如下的类中接收到它:

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

在最后一个 onListItemClick 中,如何在“您已选择”之后自定义它,我可以从上面定义的 arraylist 项中输入值?

4

2 回答 2

2
 public void onListItemClick(ListView parent, View view, int position, long id)
{
Toast.makeText(this, "You have selected"+position, Toast.LENGTH_LONG).show();
}
于 2012-12-03T09:28:29.257 回答
1

如果你有你的索引和数组列表,那么你可以通过索引在集合中引用你的字符串:

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

请注意,我将 arrayList 设为一个字段,以便能够从其他方法中引用它。

于 2012-12-03T09:47:13.373 回答