我想点击列表视图项目时开始新的活动

时间:2013-07-25 13:07:27

标签: android listview android-activity onitemclicklistener

我正确设置了OnItemClickListener但是当我点击其中一个列表项时它崩溃了。我想知道它是否可以完成

这是代码

更新

 public class Inbox extends ListActivity {

    private static String[] numbers;
    private static String[] content;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.inbox_list);
        DictionaryOpenHelper helper = new DictionaryOpenHelper(this,"MyTable", null, 2);
        SQLiteDatabase database  = helper.getReadableDatabase();
        Cursor c = database.rawQuery("SELECT * FROM MyTable", null);

        numbers = new String[c.getCount()];     
        content = new String[c.getCount()];
            c.moveToFirst();
            int count = 0;
            do{
                numbers[count] = c.getString(c.getColumnIndex("PhoneNumber"));
                content[count] = c.getString(c.getColumnIndex("Message"));
                count++;
            }
            while(c.moveToNext());

            database.close();
        setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1,
                numbers));
    this.getListView().setOnItemClickListener(listener);
        }
    private OnItemClickListener listener = new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            Intent intent = new Intent(Inbox.this, ContDisplay.class);
            intent.putExtra("number", numbers[arg2]);
            startActivity(intent);
        }
    };
}

在Oncreate中:

this.getListView().setOnItemClickListener(listener);

1 个答案:

答案 0 :(得分:0)

arg0.getAdapter().getItemId(arg2)应替换为arg2,因为arg2是点击列表元素的位置。我认为你得到ArrayIndexOutOfBoundsException,因为你试图获取数组的元素不是按元素的位置,而是按元素的id!元素的id可以是例如100203102031230。