首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SimpleCursor适配器

SimpleCursor适配器
EN

Stack Overflow用户
提问于 2013-07-30 15:01:02
回答 2查看 123关注 0票数 1

我正在从sqlite管理器或资产文件夹中获取数据,我很适合使用simplecursor适配器检索数据,但是如何使用游标适配器进行分页,请帮助我这是我的代码

代码语言:javascript
复制
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);
EN

回答 2

Stack Overflow用户

发布于 2013-07-30 15:16:08

  1. 此IllegalException显示您没有某些列。向你展示异常堆栈跟踪,来自SimpleCursorAdapter的异常通常有很好的清除数据,调用e.getLocalizedMessage().
  2. Also,如果你改变现有的数据库,几乎不建议在应用程序管理器中设置“清除数据”或完全卸载并重新安装应用程序。
  3. 请避免使用已弃用的构造函数,尝试使用

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

票数 0
EN

Stack Overflow用户

发布于 2013-07-30 15:43:52

表中必须有_id列。这是它背后的主要原因。

代码语言:javascript
复制
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");




    }
});

数据库代码是

代码语言:javascript
复制
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();

代码语言:javascript
复制
 }
代码语言:javascript
复制
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; 


}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17940317

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档