我已经在网上搜索了高低。 尝试了几个技巧和教程,但没有一个真正适合我。
我要做的是使用多个选择列表删除列表中的多个条目。 一切正常,并且列表在xml中设置为multiplechoice,但在尝试更新我的自定义列表时,我似乎无法解决此问题。
我正在使用一个resourcecursoradapter,不知道这是不是问题,但我现在不知所措,所以如果有任何机构可以帮助我那将会很棒。
现在我的活动代码。
package com.ShaHar91.ivlibrary;
import java.io.IOException;
import java.io.InputStream;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.AssetManager;
import android.database.Cursor;
import android.database.SQLException;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.ResourceCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
@SuppressWarnings("deprecation")
public class DeleteMultiPoke extends ListActivity {
public static final String ROW_ID = "row_id"; // Intent extra key
private long rowID;
MyAdapter mListAdapter;
Menu menu;
MenuItem delete;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout);
DatabaseConnector myDbHelper = new DatabaseConnector(
DeleteMultiPoke.this);
try {
myDbHelper.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
DatabaseConnector.openDataBase();
} catch (SQLException sqle) {
throw sqle;
}
Cursor myCur = null;
myCur = myDbHelper.getAllPokes();
mListAdapter = new MyAdapter(DeleteMultiPoke.this, myCur);
setListAdapter(mListAdapter);
}
private class MyAdapter extends ResourceCursorAdapter {
public MyAdapter(Context context, Cursor cur) {
super(context, R.layout.poke_list_remove_item, cur);
}
@Override
public View newView(Context context, Cursor cur, ViewGroup parent) {
LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
return li.inflate(R.layout.poke_list_remove_item, parent, false);
}
@Override
public void bindView(View view, Context context, Cursor cur) {
TextView pokeTv = (TextView) view.findViewById(R.id.pokeTv);
TextView genderTv = (TextView) view.findViewById(R.id.genderTv);
RadioButton hpRb = (RadioButton) view.findViewById(R.id.hpRb);
RadioButton attRb = (RadioButton) view.findViewById(R.id.attRb);
RadioButton defRb = (RadioButton) view.findViewById(R.id.defRb);
RadioButton spAttRb = (RadioButton) view.findViewById(R.id.spAttRb);
RadioButton spDefRb = (RadioButton) view.findViewById(R.id.spDefRb);
RadioButton speedRb = (RadioButton) view.findViewById(R.id.speedRb);
ImageView pokeSprite = (ImageView) view
.findViewById(R.id.pokeSpriteIV);
pokeTv.setText(cur.getString(cur.getColumnIndex("name")));
genderTv.setText(cur.getString(cur.getColumnIndex("gender")));
hpRb.setChecked((cur.getInt(cur.getColumnIndex("hp")) == 0 ? false
: true));
attRb.setChecked((cur.getInt(cur.getColumnIndex("att")) == 0 ? false
: true));
defRb.setChecked((cur.getInt(cur.getColumnIndex("def")) == 0 ? false
: true));
spAttRb.setChecked((cur.getInt(cur.getColumnIndex("sp_att")) == 0 ? false
: true));
spDefRb.setChecked((cur.getInt(cur.getColumnIndex("sp_def")) == 0 ? false
: true));
speedRb.setChecked((cur.getInt(cur.getColumnIndex("speed")) == 0 ? false
: true));
AssetManager assetManager = getAssets();
int imageIndex = cur.getColumnIndex("nat_dex");
try {
InputStream ims = assetManager.open("pokes/"
+ cur.getString(imageIndex) + ".gif");
Drawable d = Drawable.createFromStream(ims, null);
pokeSprite.setImageDrawable(d);
} catch (IOException ex) {
return;
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.poke_menu_remove, menu);
delete = menu.findItem(R.id.deletepokes);
delete.setEnabled(false);
delete.setVisible(false);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.canceldeleting:
finish();
return true;
case R.id.deletepokes:
// create a new AlertDialog Builder
AlertDialog.Builder builder = new AlertDialog.Builder(
DeleteMultiPoke.this);
builder.setTitle(R.string.confirmTitle); // title bar string
builder.setMessage(R.string.confirmMessage); // message to display
// provide an OK button that simply dismisses the dialog
builder.setPositiveButton(R.string.button_delete,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int button) {
final DatabaseConnector databaseConnector = new DatabaseConnector(
DeleteMultiPoke.this);
ListView listView = (ListView) findViewById(android.R.id.list);
SparseBooleanArray checked = listView
.getCheckedItemPositions();
for (int i = checked.size() - 1; i >= 0; i--) {
final int position = checked.keyAt(i);
if (checked.valueAt(i)) {
AsyncTask<Long, Object, Object> deleteTask = new AsyncTask<Long, Object, Object>() {
@Override
protected Object doInBackground(
Long... params) {
databaseConnector.deletePoke(mListAdapter
.getItemId(position));
return null;
}
@Override
protected void onPostExecute(
Object result) {
finish(); // return to the
// BookLibrary Activity
}
};
// delete the AsyncTask to delete book at
// rowID
deleteTask.execute(new Long[] { rowID });
}
}
} // end method onClick
});
builder.setNegativeButton(R.string.button_cancel, null);
builder.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
protected void onResume() {
super.onResume();
mListAdapter.getCursor().requery();
}
@Override
protected void onStop() {
super.onStop();
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
String selected = "";
int cntChoice = l.getCount();
SparseBooleanArray sparseBooleanArray = l.getCheckedItemPositions();
for (int i = 0; i < cntChoice; i++) {
if (sparseBooleanArray.get(i)) {
selected += l.getItemAtPosition(i).toString() + "\n";
}
}
Toast.makeText(DeleteMultiPoke.this,
selected,
Toast.LENGTH_LONG).show();
if (l.getCheckedItemCount() == 0) {
delete.setEnabled(false);
delete.setVisible(false);
} else {
delete.setEnabled(true);
delete.setVisible(true);
}
}
提前Ty,
Christiano Bolla
答案 0 :(得分:0)
您应该在删除任何项目后通过调用列表适配器上的notifyDataSetChanged()
来通知列表基础数据源已更改。