2013-03-30 68 views
0

我已經將列表項目從BaseAdapter轉換爲ArrayAdapter ListActivity,因爲我在另一個問題中被告知ArrayAdapter更具動態性和更好性,特別是允許刪除項目從列表中進行更新,然後進行更新以反映該刪除。我仍然在運行到同一個問題,我的ArrayAdapter,不過,具體如下:刪除列表項,然後查詢數據庫拋出異常

我得到我的列表數據像這樣:

public void loadAdapter(){ 

    DatabaseHelper helper = new DatabaseHelper(ActivityMain.this); 
    database = helper.getReadableDatabase(); 

    Cursor data = database.query("list_data", fields, null, null, null, 
      null, null); 
    Integer tindex = data.getColumnIndex("listTitle"); 
    Integer iindex = data.getColumnIndex("listType"); 

    itemCount = 0; 
    for (data.moveToFirst(); !data.isAfterLast(); data.moveToNext()) { 
     m_parts.add(new Item(data.getString(tindex), data.getString(iindex))); 
     itemCount++; 
    } 

    data.close(); 

    for (int j = 0; j < 10; j++) { 
     m_parts.add(new Item("", "R")); 
    } 

    m_adapter = new ItemAdapter(ActivityMain.this, R.layout.listview, m_parts); 

    setListAdapter(m_adapter); 
} 

與此自定義適配器:

public class ItemAdapter extends ArrayAdapter<Item> { 

    private ArrayList<Item> objects; 

    public ItemAdapter(Context context, int textViewResourceId, 
      ArrayList<Item> objects) { 
     super(context, textViewResourceId, objects); 
     this.objects = objects; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 

     View v = convertView; 

     if (v == null) { 
      LayoutInflater inflater = (LayoutInflater) getContext() 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      v = inflater.inflate(R.layout.listview, null); 
     } 

     Item i = objects.get(position); 

     if (i != null) { 

      TextView textview = (TextView) v.findViewById(R.id.tv_main); 
      ImageView imageview = (ImageView) v.findViewById(R.id.iv_main); 
      TextView textview2 = (TextView) v.findViewById(R.id.tv_main2); 
      textview.setText(i.getText()); 
      textview2.setText(i.getText()); 

      imageview.setScaleType(ScaleType.FIT_XY); 
      Integer theDrawable; 
      if (i.getImage() != "L") { 
       theDrawable = R.drawable.listview_regular; 
      } else { 
       theDrawable = R.drawable.listview_location; 
      } 
      imageview.setImageResource(theDrawable); 

     } 

     v.setOnClickListener(new OnItemClickListener(position)); 
     v.setOnLongClickListener(new OnItemLongClickListener(position)); 
     return v; 

    } 
} 

的來自longclicklistener的上下文菜單提供刪除選項,其使用此

private void showDialogOnLongClick(final int position) { 

Builder alert = new AlertDialog.Builder(this); 

ArrayList<String> listInfo = getListInfo(position); 
String content = listInfo.get(1); 
String numItems = ""; 

if (content != null && content.indexOf("|~|") > -1) { 
String[] contentSplit = content.split("\\|\\~\\|"); 
numItems = contentSplit.length + " items in list"; 
} else { 
numItems = "No items in list"; 
} 

String listTitle = listInfo.get(0); 
String created = "Created: " + listInfo.get(2); 
String modified = "Modified: " + listInfo.get(3); 
String delete = "Delete"; 
String edit = "Edit"; 
final String[] items = new String[] { created, modified, numItems, 
delete, edit }; 

alert.setTitle(listTitle); 

alert.setItems(items, new DialogInterface.OnClickListener() { 
@Override 
public void onClick(DialogInterface dialog, int which) { 
switch (which) { 

case 3: 
if (deleteList(position)) { 
//listview.invalidate(); 
//Item itemToRemove = m_parts.remove(position); 
//m_adapter.remove(itemToRemove); 
//m_adapter.remove(toRemove); 
//m_adapter.notifyDataSetInvalidated();  <-- These are all things I've tried 
//m_adapter.clear();        in various combinations 
//m_adapter.remove(position); 

Item toRemove = m_adapter.getItem(position); 
m_parts.remove(toRemove); //or, m_parts.remove(position);<-This is what should work 
    m_adapter.notifyDataSetChanged(); 
    loadAdapter(); 
//  runOnUiThread(new Runnable() { 
//  public void run() { 
//   m_adapter.notifyDataSetChanged();  <--I've tried a thread approach 
//    } 
//   }); 
} 
break; 

case 4: 
Intent i = new Intent(ActivityMain.this, 
ShowARegularList.class); 
i.putExtra("list_id", (position + 1) + ""); 
startActivity(i); 
break; 
} 
} 
}); 

alert.setPositiveButton("OK", new DialogInterface.OnClickListener() { 

@Override 
public void onClick(DialogInterface dialog, int which) { 

dialog.dismiss(); 

} 
}); 

alert.show(); 
} 

其中,在3變化與

// Delete single list item data 
public boolean deleteList(int id) { 
    id++; 
    DatabaseHelper helper = new DatabaseHelper(this); 
    database = helper.getWritableDatabase(); 

    // ContentValues values = new ContentValues(); 
    // values.put("_id", id); 

    database.delete("list_data", "_id =" + id, null); 
    database.close(); 

    // text = text.removeElementStr(); 
    // itemCount--; 
    return true; 

} 

上述作品數據庫以從列表中刪除的項目,並且在視覺上封閉間隙。但是,當點擊刪除項目的「舊」點(這引發了編輯所選項目的新意圖)時,在查詢db的新活動(logcat中的最後一行,97)中引發了一個異常:

final Integer thisListID = Integer.parseInt(listIDstr); 
    final DatabaseHelper helper = new DatabaseHelper(this); 
    database = helper.getReadableDatabase(); 

    Cursor cursor = database.query("list_data", new String[] { "listTitle", 
      "listContent", "dateCreated", "dateModified" }, "_id = " + thisListID 
      + "", null, null, null, null); 

    ArrayList<String> listInfo = new ArrayList<String>(); 

    if (cursor != null && cursor.moveToFirst()) { 
     listInfo.add(cursor.getString(cursor.getColumnIndex("listTitle"))); 
     listInfo.add(cursor.getString(cursor.getColumnIndex("listContent"))); 
     listInfo.add(cursor.getString(cursor.getColumnIndex("dateCreated"))); 
     listInfo.add(cursor.getString(cursor.getColumnIndex("dateModified"))); 
    } 

    cursor.close(); 
    strListContent = listInfo.get(1).trim(); 

與logcat的

java.lang.RuntimeException: Unable to start activity... 
java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2049) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2083) 
at android.app.ActivityThread.access$600(ActivityThread.java:134) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1233) 
... 
at com.baked.listanywhere.ShowARegularList.onCreate(ShowARegularList.java:97) 

索引列表項小於刪除一個的罰款;那些索引較大的人在其內容中只有一個。我懷疑我的查詢邏輯錯誤,查詢不存在的索引......但似乎我應該可以重新繪製列表並且有一個反映數據庫的索引列表。我真正想做的是消除名單上的任何記憶,然後重新填充它,但我似乎無法做到這一點...任何幫助將不勝感激!

+0

我的想法在這一點上:因爲我查詢的主鍵不再存在 - 我可以用rownum查詢(沒有工作);或創建一個臨時表,將值加載到它中,並檢索第n行......看起來像一個kludge;有任何想法嗎? – kwishnu

回答

1

嗯,沒有人插嘴,我已經在this question查詢

Cursor cursor = database.rawQuery("SELECT * FROM list_data ORDER BY _id LIMIT 1 OFFSET '"+ thisListID +"'", null); 

感謝WOJTEK解決了這個問題。而且,是的,

case 3: 
if (deleteList(position)) { 

    Item toRemove = m_adapter.getItem(position); 
    m_parts.remove(toRemove); 
    m_adapter.notifyDataSetInvalidated(); 

    loadAdapter(); 

break; 
} 

工作正常,即使我可以發誓這是問題!