1

我正在从 sqlite 管理器或资产文件夹中获取数据,并且在使用 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);
4

2 回答 2

0
  1. 此 IllegalException 表明您没有某些列。向您展示异常堆栈跟踪,来自 SimpleCursorAdapter 的异常通常具有清晰的本地化消息,调用 e.getLocalizedMessage()。
  2. 此外,如果您更改现有数据库,几乎不建议在应用程序管理器中进行“清除数据”或完全卸载并重新安装应用程序。
  3. 请避免使用过时的构造函数,尝试使用

    SimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags)
    
于 2013-07-30T07:16:07.543 回答
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; 


}
于 2013-07-30T07:43:52.307 回答