2013-03-30 47 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; 
} 

工作正常,即使我可以发誓这是问题!