填充的ListView

问题描述:

我'面临崩溃一旦我开始通过 填充从我的分贝一个ListView使用简单的CursorAdapter填充的ListView

和logcat的是显示此消息

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.osamawaosamawa.medcine/com.example.osamawaosamawa.medcine.ppllistViews}: java.lang.IllegalArgumentException: column '_id' does not exist 

什么是_id柱?我没有这个在该类

列表视图中的Java类

public class ppllistViews extends Activity { 
DB db; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.ppllistview); 
    db=new DB(this); 
    pupulatelistview(); 

    } 

    public void pupulatelistview(){ 

    Cursor cursor = db.getpplNameData(); 
    startManagingCursor(cursor); 

    String [] pplinformation = new String[]{ 
      DB.col1T2 
    }; 
    int [] maping = new int[]{ 
      R.id.PPLID 
    }; 

    SimpleCursorAdapter mycursorAdapter = 
      new SimpleCursorAdapter(this,R.layout.pplnamelistxml,cursor,pplinformation,maping); 


    ListView ppldatalistview = (ListView)findViewById(R.id.listViewpplname); 
    ppldatalistview.setAdapter(mycursorAdapter); 
} 

我已经在我的DBCLASS

public final static String colrowid = "_id"; 

我的查询,添加此行我在哪里可以在这里新增

public Cursor getpplNameData() { 
    SQLiteDatabase db = this.getWritableDatabase(); 
    Cursor result = db.rawQuery("select * from " + Table_name2, null); 
    return result; 

} 

CursorAdapters确实需要_id列。

你可以做什么,是使用良好的旧“rowID技巧”。
只需将, rowID AS _id添加到您的查询中即可添加rowID并将其命名为_id

所以,如果您的查询是这样的:

SELECT * FROM ... 

改变它

SELECT *, rowID AS _id FROM ... 

rowID是一个隐藏字段,这使得相当多的_id领域的替代品。

+1

谢谢你坦率的工作正常。 :d – OWA