0

当我单击第一项时,此代码吐司显示位置 0。当我单击第一项时,我希望吐司向我显示位置 1。

ListView myListView = (ListView) findViewById(R.id.myListView);
    final EditText myEditText = (EditText) findViewById(R.id.myEditText);

    final ArrayList<String> todoItem = new ArrayList<String>();
    final ArrayAdapter<String> aa;
    aa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, todoItem);
    myListView.setAdapter(aa);

    myEditText.setOnKeyListener(new OnKeyListener() {

        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if(event.getAction() == KeyEvent.ACTION_DOWN)
                if((keyCode == KeyEvent.KEYCODE_DPAD_CENTER) || keyCode == KeyEvent.KEYCODE_ENTER){
                    todoItem.add(0, myEditText.getText().toString());
                    aa.notifyDataSetChanged();
                    myEditText.setText("");
                    return true;
                }
            return false;
        }
    });

    myListView.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                long arg3) {
            Toast.makeText(getApplicationContext(), "this is to do list " + position, Toast.LENGTH_SHORT).show();
        }
    });
4

2 回答 2

1

由于适配器中视图的位置从 开始0,所以位置加1,显示在Toast

myListView.setOnItemClickListener(new OnItemClickListener() {

    public void onItemClick(AdapterView<?> arg0, View arg1, int position,
            long arg3) {
        Toast.makeText(getApplicationContext(), "this is to do list " + (position+1), Toast.LENGTH_SHORT).show();
    }
});
于 2012-11-01T03:16:14.977 回答
1

只需将 1 添加到位置,因为索引位置从值 0 开始。

 public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
     Toast.makeText(getApplicationContext(), "this is to do list " + (position+1), Toast.LENGTH_SHORT).show();
 }
于 2012-11-01T03:18:15.803 回答