使用sqlite数据库的listview中的子项

时间:2014-11-18 07:40:40

标签: android sqlite

我的列表视图有问题,当我在listveiw中添加subitem然后subitem显示在另一个列表项中并单击它崩溃的项目并给出此错误java.lang.IllegalStateException:ArrayAdapter需要资源ID为TextView的

这是数据库类

  public class DBHelper extends SQLiteOpenHelper {

   public static final String DATABASE_NAME = "DailyTask.db";
   public static final String TASKS_TABLE_NAME = "task";
   public static final String TASKS_COLUMN_ID = "id";
   public static final String TASKS_COLUMN_TITLE = "title";
   public static final String TASKS_COLUMN_DESCRIPTION = "description";
   public static final String TASKS_COLUMN_NOTIFICATION = "notification";
   public static final String TASKS_COLUMN_STATUS = "status";
   public static final String TASKS_COLUMN_STATUSVALUE = "statusvalue";
   private static final String MODE_PRIVATE = null;
   private static final String PREFS_NAME = null;

   private HashMap hp;

   public DBHelper(Context context)
   {
   super(context, DATABASE_NAME , null, 1);
   }

   @Override
   public void onCreate(SQLiteDatabase db) {
  // TODO Auto-generated method stub
  db.execSQL(
  "create table task " +
  "(id integer primary key, title text,description text,notification text,status text,statusvalue int)"
  );
  }

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

   public boolean insertContact  (String title, String description, String notification)
  {
  String status = "Processing";
  String statusvalue = "2";
  SQLiteDatabase db = this.getWritableDatabase();
  ContentValues contentValues = new ContentValues();

  contentValues.put("title", title);
  contentValues.put("description", description);
  contentValues.put("notification", notification);
  contentValues.put("status", status);
  contentValues.put("statusvalue", statusvalue);

  db.insert("task", null, contentValues);
  return true;
  }
  public Cursor getData(int id){
  SQLiteDatabase db = this.getReadableDatabase();
  Cursor res =  db.rawQuery( "select * from task where id="+id+"", null );
  return res;
  }
  public int numberOfRows(){
  SQLiteDatabase db = this.getReadableDatabase();
  int numRows = (int) DatabaseUtils.queryNumEntries(db, TASKS_TABLE_NAME);
  return numRows;
  }
  public boolean updateContact (Integer id, String title, String description, String notification, String status)
  {
  SQLiteDatabase db = this.getWritableDatabase();
  ContentValues contentValues = new ContentValues();
  contentValues.put("title", title);
  contentValues.put("description", description);
  contentValues.put("notification", notification);
  contentValues.put("status", status);
  db.update("task", contentValues, "id = ? ", new String[] { Integer.toString(id) } );
  return true;
  }

  public Integer deleteContact (Integer id)
  {
  SQLiteDatabase db = this.getWritableDatabase();
  return db.delete("task", 
  "id = ? ", 
  new String[] { Integer.toString(id) });
  }
  public ArrayList getAllCotacts()
  {

  ArrayList array_list = new ArrayList();
  //hp = new HashMap();
  SQLiteDatabase db = this.getReadableDatabase();

  Cursor res =  db.rawQuery( "select * from task", null );
  res.moveToFirst();
  while(res.isAfterLast() == false){

  //String Name = res.getString(res.getColumnIndex(TASKS_COLUMN_TITLE));
  //Log.d("DBHelper", Name);

  array_list.add(res.getString(res.getColumnIndex(TASKS_COLUMN_TITLE)));
  res.moveToNext();
  }
   return array_list;
  }

  public ArrayList getProcessingCotacts()
  {
  ArrayList array_list1 = new ArrayList();
  //hp = new HashMap();
  SQLiteDatabase db = this.getReadableDatabase();

  String textValue = "Processing";
  String query = "select * from task where status = ?";
  Cursor res = db.rawQuery(query, new String[] {textValue});

  //Cursor res =  db.rawQuery( "select * from task", null );
  res.moveToFirst();
  while(res.isAfterLast() == false){

  //String Name = res.getString(res.getColumnIndex(TASKS_COLUMN_TITLE));
  //Log.d("DBHelper", Name);

  array_list1.add(res.getString(res.getColumnIndex(TASKS_COLUMN_TITLE)));
  res.moveToNext();
  }
  return array_list1;
  }
  public ArrayList getIncompletedCotacts()
  {
  ArrayList array_list1 = new ArrayList();
  //hp = new HashMap();
  SQLiteDatabase db = this.getReadableDatabase();

  String textValue = "Incompleted";
  String query = "select * from task where status = ?";
  Cursor res = db.rawQuery(query, new String[] {textValue});

  //Cursor res =  db.rawQuery( "select * from task", null );
  res.moveToFirst();
  while(res.isAfterLast() == false){

  //String Name = res.getString(res.getColumnIndex(TASKS_COLUMN_TITLE));
  //Log.d("DBHelper", Name);

  array_list1.add(res.getString(res.getColumnIndex(TASKS_COLUMN_TITLE)));
  res.moveToNext();
  }
  return array_list1;
  }

  public ArrayList getCompletedCotacts()
  {
  ArrayList array_list1 = new ArrayList();
  //hp = new HashMap();
  SQLiteDatabase db = this.getReadableDatabase();

  String textValue = "Completed";
  String query = "select * from task where status = ?";
  Cursor res = db.rawQuery(query, new String[] {textValue});

  //Cursor res =  db.rawQuery( "select * from task", null );
  res.moveToFirst();
  while(res.isAfterLast() == false){

  //String Name = res.getString(res.getColumnIndex(TASKS_COLUMN_TITLE));
  //Log.d("DBHelper", Name);

  array_list1.add(res.getString(res.getColumnIndex(TASKS_COLUMN_TITLE)));
  res.moveToNext();
  }
  return array_list1;
  }   
  }

这是MainActivity类

  public class MainActivity extends Activity {

  private SharedPreference sharedPreference;

  Activity context = this;
 private ListView obj;  
 DBHelper mydb;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list);

    sharedPreference = new SharedPreference();
    String text = null;
    text = sharedPreference.getValue(context);
    //Log.d("MainActivity", text);



    mydb = new DBHelper(this);


    if("Incompleted Task".equals(text))
    {
      ArrayList array_list = mydb.getIncompletedCotacts();

      ArrayAdapter arrayAdapter =      
              new ArrayAdapter(this,android.R.layout.simple_list_item_1, array_list);        
              obj = (ListView)findViewById(R.id.listView1);
              obj.setAdapter(arrayAdapter);

    }
    else if ("Progress Task".equals(text)) 
    {
      ArrayList array_list = mydb.getProcessingCotacts();
      ArrayAdapter arrayAdapter =      
              new ArrayAdapter(this,android.R.layout.simple_list_item_1, array_list);
              obj = (ListView)findViewById(R.id.listView1);
              obj.setAdapter(arrayAdapter);
    }
    else if ("Completed Task".equals(text)) 
    {

      ArrayList array_list = mydb.getCompletedCotacts();
      ArrayAdapter arrayAdapter =      
              new ArrayAdapter(this,android.R.layout.simple_list_item_1, array_list);
              obj = (ListView)findViewById(R.id.listView1);
              obj.setAdapter(arrayAdapter);

    }
    else 
    {
      ArrayList array_list = mydb.getAllCotacts();

      ArrayAdapter arrayAdapter =      
      new ArrayAdapter(this,android.R.layout.simple_list_item_1, array_list);
      obj = (ListView)findViewById(R.id.listView1);
      obj.setAdapter(arrayAdapter);

    }





    obj.setOnItemClickListener(new OnItemClickListener(){

   @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
   long arg3) {
     // TODO Auto-generated method stub
     int id_To_Search = arg2 + 1;
     Bundle dataBundle = new Bundle();
     dataBundle.putInt("id", id_To_Search);
     Intent intent = new Intent(getApplicationContext(),DisplayTask.class);
     intent.putExtras(dataBundle);
     startActivity(intent);
   }
   });
   }
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
   // Inflate the menu; this adds items to the action bar if it is present.
   getMenuInflater().inflate(R.menu.mainmenu, menu);
   return true;
   }
   @Override 
   public boolean onOptionsItemSelected(MenuItem item) 
   { 
      super.onOptionsItemSelected(item); 
     switch(item.getItemId()) 
     { 
     case R.id.item1: 
        Bundle dataBundle = new Bundle();
        dataBundle.putInt("id", 0);
        Intent intent = new Intent(getApplicationContext(),DisplayTask.class);
        intent.putExtras(dataBundle);
        startActivity(intent);
        return true; 

     case R.id.item2: 

         Intent intent1 = new Intent(getApplicationContext(),setting.class);
         startActivity(intent1);

     default: 
        return super.onOptionsItemSelected(item); 

   } 

   } 
   public boolean onKeyDown(int keycode, KeyEvent event) {
  if (keycode == KeyEvent.KEYCODE_BACK) {
     moveTaskToBack(true);
  }
  return super.onKeyDown(keycode, event);
 }

}

这是logcat

11-18 07:13:11.204: E/AndroidRuntime(1919): FATAL EXCEPTION: main
11-18 07:13:11.204: E/AndroidRuntime(1919): Process: com.example.dailytask, PID: 1919
11-18 07:13:11.204: E/AndroidRuntime(1919): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
11-18 07:13:11.204: E/AndroidRuntime(1919):     at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
11-18 07:13:11.204: E/AndroidRuntime(1919):     at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
11-18 07:13:11.204: E/AndroidRuntime(1919):     at android.widget.AbsListView.obtainView(AbsListView.java:2263)
11-18 07:13:11.204: E/AndroidRuntime(1919):     at android.widget.ListView.makeAndAddView(ListView.java:1790)
11-18 07:13:11.204: E/AndroidRuntime(1919):     at android.widget.ListView.fillDown(ListView.java:691)
11-18 07:13:11.204: E/AndroidRuntime(1919):     at android.widget.ListView.fillFromTop(ListView.java:752)
11-18 07:13:11.204: E/AndroidRuntime(1919):     at android.widget.ListView.layoutChildren(ListView.java:1630)
11-18 07:13:11.204: E/AndroidRuntime(1919):     at android.widget.AbsListView.onLayout(AbsListView.java:2091)
11-18 07:13:11.204: E/AndroidRuntime(1919):     at android.view.View.layout(View.java:14817)
11-18 07:13:11.204: E/AndroidRuntime(1919):     at android.view.ViewGroup.layout(ViewGroup.java:4631)
11-18 07:13:11.204: E/AndroidRuntime(1919):     at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055)
11-18 07:13:11.204: E/AndroidRuntime(1919):     at android.view.View.layout(View.java:14817)
11-18 07:13:11.204: E/AndroidRuntime(1919):     at android.view.ViewGroup.layout(ViewGroup.java:4631)
11-18 07:13:11.204: E/AndroidRuntime(1919):     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
11-18 07:13:11.204: E/AndroidRuntime(1919):     at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
11-18 07:13:11.204: E/AndroidRuntime(1919):     at android.view.View.layout(View.java:14817)
11-18 07:13:11.204: E/AndroidRuntime(1919):     at android.view.ViewGroup.layout(ViewGroup.java:4631)
11-18 07:13:11.204: E/AndroidRuntime(1919):     at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:374)
11-18 07:13:11.204: E/AndroidRuntime(1919):     at android.view.View.layout(View.java:14817)
11-18 07:13:11.204: E/AndroidRuntime(1919):     at android.view.ViewGroup.layout(ViewGroup.java:4631)
11-18 07:13:11.204: E/AndroidRuntime(1919):     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
11-18 07:13:11.204: E/AndroidRuntime(1919):     at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
11-18 07:13:11.204: E/AndroidRuntime(1919):     at android.view.View.layout(View.java:14817)
11-18 07:13:11.204: E/AndroidRuntime(1919):     at android.view.ViewGroup.layout(ViewGroup.java:4631)
11-18 07:13:11.204: E/AndroidRuntime(1919):     at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1987)
11-18 07:13:11.204: E/AndroidRuntime(1919):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1744)
11-18 07:13:11.204: E/AndroidRuntime(1919):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
11-18 07:13:11.204: E/AndroidRuntime(1919):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
11-18 07:13:11.204: E/AndroidRuntime(1919):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
11-18 07:13:11.204: E/AndroidRuntime(1919):     at android.view.Choreographer.doCallbacks(Choreographer.java:574)
11-18 07:13:11.204: E/AndroidRuntime(1919):     at android.view.Choreographer.doFrame(Choreographer.java:544)
11-18 07:13:11.204: E/AndroidRuntime(1919):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
11-18 07:13:11.204: E/AndroidRuntime(1919):     at android.os.Handler.handleCallback(Handler.java:733)
11-18 07:13:11.204: E/AndroidRuntime(1919):     at android.os.Handler.dispatchMessage(Handler.java:95)
11-18 07:13:11.204: E/AndroidRuntime(1919):     at android.os.Looper.loop(Looper.java:136)
11-18 07:13:11.204: E/AndroidRuntime(1919):     at android.app.ActivityThread.main(ActivityThread.java:5017)
11-18 07:13:11.204: E/AndroidRuntime(1919):     at java.lang.reflect.Method.invokeNative(Native Method)
11-18 07:13:11.204: E/AndroidRuntime(1919):     at java.lang.reflect.Method.invoke(Method.java:515)
11-18 07:13:11.204: E/AndroidRuntime(1919):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
11-18 07:13:11.204: E/AndroidRuntime(1919):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
11-18 07:13:11.204: E/AndroidRuntime(1919):     at dalvik.system.NativeStart.main(Native Method)
11-18 07:13:11.204: E/AndroidRuntime(1919): Caused by: java.lang.ClassCastException: android.widget.TwoLineListItem cannot be cast to android.widget.TextView
11-18 07:13:11.204: E/AndroidRuntime(1919):     at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:379)
11-18 07:13:11.204: E/AndroidRuntime(1919):     ... 40 more

1 个答案:

答案 0 :(得分:0)

更新的代码 - 你正在使用

ArrayAdapter arrayAdapter = new ArrayAdapter(this,R.layout.items, array_list);

是创建适配器的正确方法,因为它需要制作自定义布局R.layout.items。我使用的是android.R.layout.simple_list_item_1

的默认布局
 if("Incompleted Task".equals(text))
{
  ArrayList array_list = mydb.getIncompletedCotacts();

  ArrayAdapter arrayAdapter =      
          new ArrayAdapter(this,android.R.layout.simple_list_item_1, array_list);         
          obj = (ListView)findViewById(R.id.listView1);
          obj.setAdapter(arrayAdapter);

}
else if ("Progress Task".equals(text)) 
{
  ArrayList array_list = mydb.getProcessingCotacts();
  ArrayAdapter arrayAdapter =      
          new ArrayAdapter(this,android.R.layout.simple_list_item_1, array_list);
          obj = (ListView)findViewById(R.id.listView1);
          obj.setAdapter(arrayAdapter);
}
else if ("Completed Task".equals(text)) 
{

  ArrayList array_list = mydb.getCompletedCotacts();
  ArrayAdapter arrayAdapter =      
          new ArrayAdapter(this,android.R.layout.simple_list_item_1, array_list);
          obj = (ListView)findViewById(R.id.listView1);
          obj.setAdapter(arrayAdapter);

}
else 
{
  ArrayList array_list = mydb.getAllCotacts();

  ArrayAdapter arrayAdapter =      
  new ArrayAdapter(this,android.R.layout.simple_list_item_1, array_list);
  obj = (ListView)findViewById(R.id.listView1);
  obj.setAdapter(arrayAdapter);

}