arg0.getAdapter().getItemId(arg2)
should be replaced with arg2
, because arg2 is position of clicked list's element. I think you getting ArrayIndexOutOfBoundsException because you are trying to get element of array not by element's position, but by element's id! Element's id could be for example 100203102031230.
I want to start a new activity when I click on a listview item
-
04-06-2022 - |
سؤال
I properly set the OnItemClickListener but it crashes when I click on one of the list item. I want to know if it can even be done
Here is the code
Update:
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);
}
};
}
inside Oncreate:
this.getListView().setOnItemClickListener(listener);
المحلول
لا تنتمي إلى StackOverflow