(draglist)如何显示数据库中的arraylist值

时间:2014-02-02 10:14:20

标签: android arraylist drag-and-drop android-sqlite

我可以插入数据库,但无法显示数据库中的arraylist值。

请帮助我,谢谢你的建议。

public class DragListActivity extends Activity {                
private static final String CTITLE = "CTITLE"; //cloumn
private static final SQLiteDatabase db = null;
private static List<String> list = null;
private DragListAdapter adapter = null;
Button btn;
DragListView dragListView;
public static List<String> group= new ArrayList<String>();
private List<String> navList = new ArrayList<String>(); 
private List<String> hashList = new ArrayList<String>();   

private String[] p = new String[] { "'1'", "'2'", "'3'", "'4'", "5'", "'6'", "'7'", "'8'", "'9'", "'10'" };


//  private List<String> moreList = new ArrayList<String>();


@Override


    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);       
    setContentView(R.layout.drag_list_activity); 



    btn = (Button) findViewById(R.id.confirm); 

    DragListView dragListView = (DragListView)findViewById(R.id.drag_list); //ListView


    initdata();
    adapter = new DragListAdapter(this, list);
    dragListView.setAdapter(adapter);

    btn.setOnClickListener(btListner); //button
  }
  

public Button.OnClickListener btListner = new Button.OnClickListener(){

              @Override       public void onClick(View v) {       //insert  into database

     SQLiteDatabase db = openOrCreateDatabase("DB", MODE_PRIVATE,null);   
     db.execSQL("CREATE TABLE IF NOT EXISTS direction (_id INTEGER PRIMARY KEY AUTOINCREMENT,CTITLE TEXT);");   
     db.execSQL("INSERT INTO direction (_id, CTITLE) VALUES(1,"+p[0]+");");
     db.execSQL("INSERT INTO direction (_id, CTITLE) VALUES(2,"+p[1]+");");
     db.execSQL("INSERT INTO direction (_id, CTITLE) VALUES(3,"+p[2]+");");
     db.execSQL("INSERT INTO direction (_id, CTITLE) VALUES(4,"+p[3]+");");
     db.execSQL("INSERT INTO direction (_id, CTITLE) VALUES(5,"+p[4]+");");
     db.execSQL("INSERT INTO direction (_id, CTITLE) VALUES(6,"+p[5]+");");
     db.execSQL("INSERT INTO direction (_id, CTITLE) VALUES(7,"+p[6]+");");
     db.execSQL("INSERT INTO direction (_id, CTITLE) VALUES(8,"+p[7]+");");
     db.execSQL("INSERT INTO direction (_id, CTITLE) VALUES(9,"+p[8]+");");
     db.execSQL("INSERT INTO direction (_id, CTITLE) VALUES(10,"+p[9]+");");
      }
     

};

dragviewlist值 如何显示数据库中的值??????

  

public void initdata(){// show

  SQLiteDatabase db = this.getWritableDatabase();

   // ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();

      list = new ArrayList<String>();     

       navList.add(p[0]);
       navList.add(p[1]);
       navList.add(p[2]);
       navList.add(p[3]);
       navList.add(p[4]);
       navList.add(p[5]);
       navList.add(p[6]);
       navList.add(p[7]);
       navList.add(p[8]);
       navList.add(null);
       list.addAll(navList);

}

2 个答案:

答案 0 :(得分:0)

创建一个函数来查询你的数据库调用fetch();从函数返回一个游标。持有结果集:

public void query(){
    Cursor  mCur = dbHelper.fetch(); 

    list = new ArrayList<String>();



    if (mCur.moveToFirst()) {
                do {
                    String one = (mCur.getString(mCur.getColumnIndexOrThrow("Field name")));
     navList.add(one);
                    String two = (mCur.getString(mCur.getColumnIndexOrThrow("Field name")));
     navList.add(two);
                    String three = (mCur.getString(mCur.getColumnIndexOrThrow("Field name")));
     navList.add(three);


                }while(mCur.moveToNext());

            }

    }

从AsyncTask中调用函数查询,从buttonClick触发AsyncTask。

答案 1 :(得分:0)

有许多数据库库可以让您的工作更轻松。

  1. ActiveAndroid:http://www.activeandroid.com/
  2. GreenDAO ORM:http://greendao-orm.com/
  3. Sugar ORM
  4. OrmLite
  5. 对于ActiveAndroid,代码只是:

    List<Direction> list = new Select().from(Direction.class).execute();