2013-07-30 59 views
1

我正在從SQLite的經理或資產的文件夾數據,我是完美的,而使用simplecursor適配器,但如何使用光標適配器做pagenation請幫我檢索數據,這是我的代碼SimpleCursor適配器

public Cursor PreviousElectionTrends1(SQLiteDatabase db2) 
{ 

    String[] columns=new String[]{"voter_basic_info_id","report_level_id","report_level_value" ,"year","booths","total","male","female","total_diff","male_diff","female_diff"}; 
    Cursor c1=db2.query("voter_basic_info", columns, "report_level_id="+1+" and report_level_value="+221+"" , 
      null , null, null, null, null); 


    return c1; 

} 

MainActivity 


db=myDbHelper.getReadableDatabase(); 
     Cursor c=myDbHelper.PreviousElectionTrends1(db); 
     String[] columns=new String[]{"report_level_id","report_level_value" ,"year","booths","total","male","female","total_diff","male_diff","female_diff"}; 
     int[] ad1={R.id.Booths}; 
     @SuppressWarnings("deprecation") 
     SimpleCursorAdapter ad=new SimpleCursorAdapter(getApplicationContext(),R.layout.datadesign ,c,columns,ad1); 
     lv.setAdapter(ad); 
+0

首先意味着列ID匹配與否。 –

回答

0
  1. 這IllegalException表明你沒有一些列。向你展示異常棧跟蹤,來自SimpleCursorAdapter的異常通常具有良好的清晰的localizedMessage,調用e.getLocalizedMessage()。
  2. 另外,如果您更改了現有的數據庫,那麼很難推薦在應用程序管理器中創建「清除數據」,或者完全卸載並重新安裝應用程序。
  3. 請避免使用過時的構造函數,嘗試使用

    SimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) 
    
0

你必須有_id列在表格中。這是它背後的主要原因。

protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.clientview); 
    cdb=new ClientDatabase(this); 
    Bundle bundle = getIntent().getExtras(); 
      stt = bundle.getString("stuff"); 
     a=stt.substring(0,(stt.indexOf("@"))); 
     a1=stt.substring((stt.indexOf("@")+1), (stt.length())); 


    cdb.open(); 
    clientviewlist(); 
} 
private void clientviewlist() { 
    // TODO Auto-generated method stub 
    Cursor cur=cdb.getallclient(a,a1); 
    String[] columnn=new String[]{ClientDatabase.CLIENT_CODE,ClientDatabase.CLIENT_NAME}; 
int[] to=new int[]{R.id.client1,R.id.client2}; 
dataaDapter=new SimpleCursorAdapter(this,R.layout.clintlist,cur,columnn,to,0); 
final ListView listview=(ListView)findViewById(R.id.listView1); 
listview.setAdapter(dataaDapter); 
listview.setOnItemClickListener(new OnItemClickListener() { 

    @Override 
    public void onItemClick(AdapterView<?> arg0, View arg1, int positions, 
      long id) { 



     tv = (TextView)arg1.findViewById(R.id.client1); 
     st=tv.getText().toString(); 
      System.out.println(st); 

     Intent intent =new Intent(Client.this,Server.class); 
    // System.out.println(selectedFromList+"------------444444444444444444"); 




    } 
}); 

和數據庫代碼是

public class ClientDatabase { 

//public static final String CLIENT_ID="_id"; 
public static final String CLIENT_CODE="_id"; 
public static final String DISTT_CODE="_distcode"; 
public static final String CLIENT_NAME="_name"; 
public static final String TOWN_CODE="_townid"; 
private static final String DATABASE_NAME="combinedclient.db"; 
private static final String TABLE_NAME="clientdata"; 
private static final int DATABASE_VERSION=1; 
public static SQLiteDatabase db; 
private final Context context; 
private DatabaseHelper dbheLper; 
private static final String TAG = "ClientsDbAdapter"; 

public ClientDatabase(Context context){ 
    this.context=context; 
    dbheLper=new DatabaseHelper(context); 

} 
public class DatabaseHelper extends SQLiteOpenHelper{ 

    DatabaseHelper(Context context) { 
     super(context, DATABASE_NAME, null,DATABASE_VERSION); 
     // TODO Auto-generated constructor stub 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     // TODO Auto-generated method stub 
     db.execSQL("CREATE TABLE " + TABLE_NAME +"(" + CLIENT_CODE + " TEXT NOT NULL," + CLIENT_NAME + " TEXT NOT NULL," + DISTT_CODE + " INTEGER NOT NULL," + TOWN_CODE + " INTEGER NOT NULL)"); 
    }//   "CREATE TABLE " + TABLE_NAME +"(" + TOWN_ID + " INTEGER ," + TOWN_CODE + " INTEGER NOT NULL," + TOWN_NAME + " TEXT NOT NULL," + DISTT_CODE + " TEXT NOT NULL," + TOWNDISTT_CODE + " INTEGER PRIMARY KEY)"; 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     // TODO Auto-generated method stub 
     db.execSQL("DROP TABLE If EXISTS" +TABLE_NAME); 
    } 

} 
public ClientDatabase open() throws SQLException{ 
    db=dbheLper.getWritableDatabase(); 
    return this; 
    // TODO Auto-generated method stub 

} 



public Cursor getallclient(String diss, String tiw) { 
    // TODO Auto-generated method stub 
    Cursor nCursor=db.query(TABLE_NAME, new String[] {CLIENT_CODE,CLIENT_NAME},DISTT_CODE + "='" + diss + "' AND " + TOWN_CODE +"='" + tiw + "'", null, null, null, null); 
    if (nCursor != null) { 
      nCursor.moveToFirst(); 
      } 
    return nCursor; 
} 

public void close() { 

dbheLper.close();
}你必須檢查在表中的所有列名正確與否的

public void insertvalues(String id, String namee, String dist, 
     String townv) { 
    try { 
     ContentValues initial=new ContentValues(); 
     initial.put(DISTT_CODE,dist); 
     initial.put(CLIENT_CODE,id); 
     initial.put(CLIENT_NAME,namee); 
     initial.put(TOWN_CODE,townv); 
     db.insert(TABLE_NAME, null, initial); 
    } catch (Exception e) { 
     // TODO: handle exception 
     Log.d("Error is===", e.toString()); 
      System.out.println("error in insertinggggggggggggg"); 
    } 
} 

public void delete() { 
    // TODO Auto-generated method stub 
    db.delete(TABLE_NAME,null, null); 
} 



public String getclient(String clientcode) { 
    // TODO Auto-generated method stub 

    Cursor cursor=db.query(TABLE_NAME, null,CLIENT_CODE + "='" + clientcode + "'", null, null, null, null); 
    if(cursor.getCount()<1) // UserName Not Exist 
    { 
     cursor.close(); 
     return "not exist"; 
    } 
    cursor.moveToFirst(); 
    String name= cursor.getString(cursor.getColumnIndex(CLIENT_NAME)); 
    cursor.close(); 
    System.out.println(name); 
    return name; 


}