ListView中的OnClickListener _id不匹配SQLite中的正确行_id



我是Android新手。我有一个SQLite后端,其中每一行都是唯一的(想想一个人)。然后在ListView中为每个人显示这些数据。当ListView项被单击时,它将转到子视图,该子视图包含特定父ListView项的所有信息。我遇到的问题是当我为ListView设置OnItemClickListener时,int位置和长id从0开始。当我将它们中的任何一个通过意图传递给列表视图中的第一项时,我得到一个"android.database"。CursorIndexOutOfBoundsException:索引0请求,大小为0"。这是因为我的行id在SQlite中从1开始,而int位置或长id从0开始。解决这个问题的最好方法是什么?我读到你可以添加1到位置或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);
            }
        });

PersonActivity

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

我按照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);
        }
    });

PersonActivity

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

DatabaseHandler

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

如果你的listview的起始值为0,你的db的起始值为1,就像你说的,你可以只添加1到id,但如果用户短裤listview不同,你会有一个问题,因为用户4可能在listview中有1的索引。

我的建议是添加数据库id到Person类,当你点击项目,你可以调用例如

objects.get(listiewClickedPosition).getUserID();

如果我没有遗漏的东西,这将检索正确的索引,即使listview是不同的短

如果你想用不同的方式简化它你可以改变objects的顺序然后调用notifyDataSetChanged()

最新更新