我一直试图找到这个问题的解决方案很长一段时间。问题是代码不会删除列表视图中选择的项目。在刷新列表后,它在log cat中说它是“id”的问题。任何人都可以帮我找出原因吗?
听众
public class ListedesClient extends ListActivity {
TextView selectionc;
public int idToModify;
ClientDataAdapter dm;
String clientId;
List<String[]> list = new ArrayList<String[]>();
List<String[]> names2 =null ;
ArrayList<String> stg1;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.listeclients);
dm = new ClientDataAdapter(this);
names2 = dm.selectAll();
stg1 = new ArrayList<String>();
for (String[] name : names2) {
stg = name[1]+" "+name[2]+ " "+name[3]; // error here
stg1.add(stg);// error here two
x++;// error here two
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this,android.R.layout.simple_list_item_1,
stg1);
this.setListAdapter(adapter);
selectionc=(TextView)findViewById(R.id.selectionc);
}
public void onListItemClick(final ListView parent, View v, final int position, final long id) {
selectionc.setText("Vous êtes sur : " + stg1[position]); //error here in stg1[position]
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setTitle("Delete Item")
.setMessage("are you sure to delete?")
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// @-@ dm.delete(list.get(position));
dm.delete(id);
ArrayAdapter adapter = (ArrayAdapter)parent.getAdapter();
adapter.remove(stg1[position]);// same error
adapter.notifyDataSetChanged();
}
})
.setNegativeButton("No",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
return;
}
});
builder.show();
}
}
dataAdapter ...
public class ClientDataAdapter {
private static final String DATABASE_NAME = "clientbase.db";
private static final int DATABASE_VERSION = 1;
static final String TABLE_CLIENT = "clienttable";
private static Context context;
static SQLiteDatabase dc;
private SQLiteStatement insertStmt;
private static final String INSERT = "insert into "
+ TABLE_CLIENT + " (nom_complet,adresse_client,numero_telephone) values (?,?,?)";
public ClientDataAdapter(Context context) {
ClientDataAdapter.context = context;
OpenHelper openHelper = new OpenHelper(ClientDataAdapter.context);
ClientDataAdapter.dc = openHelper.getWritableDatabase();
this.insertStmt = ClientDataAdapter.dc.compileStatement(INSERT);
}
public long insert(String nom_complet,String adresse_client,String numero_telephone) {
this.insertStmt.bindString(1, nom_complet);
this.insertStmt.bindString(2, adresse_client);
this.insertStmt.bindString(3, numero_telephone);
return this.insertStmt.executeInsert();
}
public void deleteAll() {
dc.delete(TABLE_CLIENT, null, null);
}
public ArrayList<String[]> selectAll()
{
ArrayList<String[]> list = new ArrayList<String[]>();
Cursor cursor = dc.query(TABLE_CLIENT, new String[] { "_id","nom_complet","adresse_client","numero_telephone"},
null, null, null, null, "nom_complet asc");
int x=0;
if (cursor.moveToFirst()) {
do {
String[] b1=new String[]{cursor.getString(0),cursor.getString(1),cursor.getString(2),cursor.getString(3)};
list.add(b1);
x=x+1;
} while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
cursor.close();
return list;
}
public boolean delete(long rowId) {
return dc.delete(TABLE_CLIENT, BaseColumns._ID + "=" + rowId, null) > 0;
}
private static class OpenHelper extends SQLiteOpenHelper {
OpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase dc) {
dc.execSQL("CREATE TABLE " + TABLE_CLIENT + " (_id INTEGER PRIMARY KEY, nom_complet TEXT NOT NULL, adresse_client TEXT NOT NULL, numero_telephone TEXT NOT NULL)");
}
@Override
public void onUpgrade(SQLiteDatabase dc, int oldVersion, int newVersion) {
dc.execSQL("DROP TABLE IF EXISTS " + TABLE_CLIENT);
onCreate(dc);
}
}
}
答案 0 :(得分:2)
你使用_id作为列,但你没有。您的创建查询是
@Override
public void onCreate(SQLiteDatabase dc) {
dc.execSQL("CREATE TABLE " + TABLE_CLIENT + " (id INTEGER PRIMARY KEY, nom_complet TEXT NOT NULL, adresse_client TEXT NOT NULL, numero_telephone TEXT NOT NULL)");
}
或者您更改了onCreate(id
中的_id
)内的查询,或者在删除查询中使用id
public void onListItemClick(final ListView parent, View v, final int position, final long id) {
selectionc.setText("Vous êtes sur : " + stg1[position]);
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setTitle("Delete Item")
.setMessage("are you sure to delete?")
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// @-@ dm.delete(list.get(position));
dm.delete(id);
ArrayAdapter adapter = (ArrayAdapter)parent.getAdapter();
adapter.remove(stg1.get(position));
adapter.notifyDataSetChanged();
}
})
String []传递一个ArrayList
,而不是提交给ArrayAdapter int x = 0;
String stg = "";
stg1 = new ArrayList<String>();
for (String[] name : names2) {
stg = name[1]+" "+name[2]+ " "+name[3];
stg1.add(stg);
x++;
}
当然stg1必须是一个ArrayList
答案 1 :(得分:0)
AbstractList
的{{1}}不支持删除。
在Itereator
中进行迭代时,无法修改该列表。将有趣的项目或其索引保存在另一个列表中,在完成迭代后将其删除。