2016-10-02 24 views
0

我是Android新手。我有一个SQLite后端,每行都是唯一的(想想一个人)。然后,我将这些数据显示在每个人的ListView中。当单击ListView项目时,它将转到子视图,该子视图具有特定父级ListView项目的所有信息。我遇到的问题是当我为ListView设置OnItemClickListener时,int位置和long id从0开始。当我通过列表视图中的第一个项目的意图传递这些对象时,我得到一个“android”。 database.CursorIndexOutOfBoundsException:索引0请求,大小为0“。这是因为我在SQlite中的行ID从1开始,而int位置或长ID从0开始。解决此问题的最佳方法是什么?我读过你可以将1添加到postion或id,但是如果用户以不同的方式对他们的ListView进行排序,它会不会将错误的ID传递给数据库?谢谢ListView中的OnClickListener _id与SQLite中的正确行_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); 

     }  
+0

而不是'CustomAdapter'使用'SimpleCursorAdapter',你的问题就会消失,这就是'[Simple] CursorAdapter'的用途:它从'Cursor'中读取_id列并提供正确的'id' 'onItemClick'方法 – pskink

+0

谢谢,这工作! –

+0

sure,no problema – pskink

回答

1

我实现了SimpleCursorAdapter为重由pskink表彰。它需要其他代码更改,但我同意这是最简单的方法。

这里是我的课看起来像

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

     } 
    }); 

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

} 

数据库处理器

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

太好了,我希望在使用sqlite db数据时,你会忘记自定义的'ArrayAdapter' /'BaseAdapter',顺便说一下'FilterQueryProvider',它提供了一个简单的“搜索即用型”适配器过滤 – pskink

0

如果你的列表视图具有为0的初始值和您的分贝为1的初始值,就像你说的,你可以只需将id加1,但如果用户将listview设置为不同,则会出现问题,因为用户4在列表视图中可能具有索引1。

我sugestion是数据库ID添加到Person上课的时候,你的项目点击你可以拨打为例

objects.get(listiewClickedPosition).getUserID(); 

如果我不是失去了一些东西,这将retrive正确的索引,即使列表视图被diferently短路

如果你想卖空它diferently你改变objects的订单后,你叫notifyDataSetChanged()

+0

这就是'[Simple] CursorAdapter'的用途:它从'Cursor'中读取'_id'列,并在'onItemClick'方法中提供正确的'id' – pskink

+0

从未听说过,你的aproch很多更简单,我想我必须在我自己的项目中尝试一下 –

+0

哟,代码会短很多 –