ListView 中的 OnClickListener _id 与 SQLite 中的正确行 _id 不匹配
OnClickListener _id from ListView does not match the correct row _id in SQLite
我是 Android 的新手。我有一个 SQLite 后端,其中每一行都是唯一的(想想一个人)。然后,我在每个人的 ListView 中显示此数据。单击 ListView 项目时,它将转到子视图,其中包含特定父 ListView 项目的所有信息。我遇到的问题是当我为 ListView 设置 OnItemClickListener 时,int 位置和 long id 从 0 开始。当我通过列表视图中第一个项目的意图传递其中任何一个时,我得到一个 "android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0".这是因为我在 SQlite 中的行 ID 从 1 开始,而 int 位置或 long id 从 0 开始。解决这个问题的最佳方法是什么?我读过您可以将 1 添加到 postion 或 id,但是如果用户以不同的方式对他们的 ListView 进行排序,它不会将错误的 ID 传递给数据库吗?谢谢
MainActivity
final ArrayList<Person> objects = db.getAllPersons();
final CustomAdapter customAdapter = new CustomAdapter(this, objects);
customAdapter.notifyDataSetChanged();
final ListView listView = (ListView) findViewById(R.id.content_list);
listView.setAdapter(customAdapter);
//Listen for ListView item click
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(MainActivity.this, PersonProfile.class);
intent.putExtra("id", id;
startActivity(intent);
}
});
个人活动
public class PersonProfile extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.person_profile);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//Get unique ID from MainActivity
int personID = getIntent().getExtras().getInt("id");
//Fetch individual from the database based on unique ID
DatabaseHandler db = new DatabaseHandler(this);
db.getReadableDatabase();
Person person = db.getPerson(personID);
db.close();
//Set TextView budget using unique ID
String budget = String.valueOf(person.getBudget());
TextView textViewBudget = (TextView) findViewById(R.id.person_budget);
textViewBudget.setText(budget);
}
如果你的 listview 的起始值为 0 而你的 db 的起始值为 1,就像你说的,你可以只在 id 上加 1,但是如果用户缩短 listview 不同,你就会遇到问题,因为用户 4 在列表视图中的索引可能为 1。
我的建议是当你点击你可以调用的项目时将数据库ID添加到Person
class
objects.get(listiewClickedPosition).getUserID();
如果我没有遗漏任何东西,这将检索正确的索引,即使列表视图被不同地缩短
如果你想以不同的方式做空它,你可以在调用 notifyDataSetChanged()
之后更改 objects
的顺序
我按照 pskink 的建议实现了 SimpleCursorAdapter。它需要一些其他代码更改,但我同意这是最简单的方法。
这是我的 类 在
之后的样子
MainActivity
DatabaseHandler db = new DatabaseHandler(this);
db.getWritableDatabase();
Cursor cur = db.getAllPersons();
String[] columns = new String[]{"name", "budget"};
int[] to = new int[]{R.id.name, R.id.budget};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.row_layout, cur, columns, to);
final ListView listView = (ListView) findViewById(R.id.content_list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(MainActivity.this, PersonProfile.class);
intent.putExtra("id", id);
startActivity(intent);
}
});
个人活动
public class PersonProfile extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.person_profile);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Long intent = getIntent().getExtras().getLong("id");
DatabaseHandler db = new DatabaseHandler(this);
db.getReadableDatabase();
String budgetSet = db.getPerson(intent);
TextView textViewBudget = (TextView) findViewById(R.id.person_budget);
textViewBudget.setText(budgetSet);
db.close();
}
数据库处理程序
Cursor getAllPersons() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.query(TABLE_PERSON, new String[]{KEY_ID,
KEY_NAME, KEY_BUDGET}, null, null, null, null, null);
return cursor;
}
我是 Android 的新手。我有一个 SQLite 后端,其中每一行都是唯一的(想想一个人)。然后,我在每个人的 ListView 中显示此数据。单击 ListView 项目时,它将转到子视图,其中包含特定父 ListView 项目的所有信息。我遇到的问题是当我为 ListView 设置 OnItemClickListener 时,int 位置和 long id 从 0 开始。当我通过列表视图中第一个项目的意图传递其中任何一个时,我得到一个 "android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0".这是因为我在 SQlite 中的行 ID 从 1 开始,而 int 位置或 long id 从 0 开始。解决这个问题的最佳方法是什么?我读过您可以将 1 添加到 postion 或 id,但是如果用户以不同的方式对他们的 ListView 进行排序,它不会将错误的 ID 传递给数据库吗?谢谢
MainActivity
final ArrayList<Person> objects = db.getAllPersons();
final CustomAdapter customAdapter = new CustomAdapter(this, objects);
customAdapter.notifyDataSetChanged();
final ListView listView = (ListView) findViewById(R.id.content_list);
listView.setAdapter(customAdapter);
//Listen for ListView item click
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(MainActivity.this, PersonProfile.class);
intent.putExtra("id", id;
startActivity(intent);
}
});
个人活动
public class PersonProfile extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.person_profile);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//Get unique ID from MainActivity
int personID = getIntent().getExtras().getInt("id");
//Fetch individual from the database based on unique ID
DatabaseHandler db = new DatabaseHandler(this);
db.getReadableDatabase();
Person person = db.getPerson(personID);
db.close();
//Set TextView budget using unique ID
String budget = String.valueOf(person.getBudget());
TextView textViewBudget = (TextView) findViewById(R.id.person_budget);
textViewBudget.setText(budget);
}
如果你的 listview 的起始值为 0 而你的 db 的起始值为 1,就像你说的,你可以只在 id 上加 1,但是如果用户缩短 listview 不同,你就会遇到问题,因为用户 4 在列表视图中的索引可能为 1。
我的建议是当你点击你可以调用的项目时将数据库ID添加到Person
class
objects.get(listiewClickedPosition).getUserID();
如果我没有遗漏任何东西,这将检索正确的索引,即使列表视图被不同地缩短
如果你想以不同的方式做空它,你可以在调用 notifyDataSetChanged()
objects
的顺序
我按照 pskink 的建议实现了 SimpleCursorAdapter。它需要一些其他代码更改,但我同意这是最简单的方法。
这是我的 类 在
之后的样子MainActivity
DatabaseHandler db = new DatabaseHandler(this);
db.getWritableDatabase();
Cursor cur = db.getAllPersons();
String[] columns = new String[]{"name", "budget"};
int[] to = new int[]{R.id.name, R.id.budget};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.row_layout, cur, columns, to);
final ListView listView = (ListView) findViewById(R.id.content_list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(MainActivity.this, PersonProfile.class);
intent.putExtra("id", id);
startActivity(intent);
}
});
个人活动
public class PersonProfile extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.person_profile);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Long intent = getIntent().getExtras().getLong("id");
DatabaseHandler db = new DatabaseHandler(this);
db.getReadableDatabase();
String budgetSet = db.getPerson(intent);
TextView textViewBudget = (TextView) findViewById(R.id.person_budget);
textViewBudget.setText(budgetSet);
db.close();
}
数据库处理程序
Cursor getAllPersons() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.query(TABLE_PERSON, new String[]{KEY_ID,
KEY_NAME, KEY_BUDGET}, null, null, null, null, null);
return cursor;
}