Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 列表的删除项不存在';行不通_Android - Fatal编程技术网

Android 列表的删除项不存在';行不通

Android 列表的删除项不存在';行不通,android,Android,很长时间以来,我一直在试图找到解决这个问题的办法。问题是代码没有删除列表视图中选定的项目。刷新列表后,它在日志cat中指出这是一个“id”问题。有人能帮我找出原因吗 名单 public class ListedesClient extends ListActivity { TextView selectionc; public int idToModify; ClientDataAdapter dm; String clientId; List<

很长时间以来,我一直在试图找到解决这个问题的办法。问题是代码没有删除列表视图中选定的项目。刷新列表后,它在日志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();
    }
}
公共类ListedClient扩展ListActivity{
文本视图选择c;
公共信息管理;
ClientDataAdapter-dm;
字符串clientId;
列表=新的ArrayList();
列表名称2=空;
阵列列表stg1;
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.listClient);
dm=新的ClientDataAdapter(此);
names2=dm.selectAll();
stg1=新的ArrayList();
对于(字符串[]名称:名称2){
stg=name[1]+“”+name[2]+“”+name[3];//此处有错误
stg1.add(stg);//这里有两个错误
x++;//这里有两个错误
}
ArrayAdapter=新的ArrayAdapter(
这个,android.R.layout.simple\u list\u item\u 1,
stg1);
这个.setListAdapter(适配器);
selectionc=(TextView)findViewById(R.id.selectionc);
}      
public void onListItemClick(最终列表视图父视图、视图v、最终整型位置、最终长id){
selectionc.setText(“Vousêtes sur:+stg1[position]);//stg1[position]中有错误
AlertDialog.Builder=新建AlertDialog.Builder(此)
.setTitle(“删除项”)
.setMessage(“您确定要删除吗?”)
.setPositiveButton(“是”,
新建DialogInterface.OnClickListener(){
@凌驾
公共void onClick(对话框接口arg0,int arg1){
//@-@dm.delete(list.get(position));
dm.删除(id);
ArrayAdapter=(ArrayAdapter)parent.getAdapter();
adapter.remove(stg1[position]);//相同错误
adapter.notifyDataSetChanged();
}
})
.setNegativeButton(“否”,
新建DialogInterface.OnClickListener(){
@凌驾
公共void onClick(对话框接口对话框,
int(其中){
//TODO自动生成的方法存根
返回;
}
});
builder.show();
}
}
数据适配器

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);
        }
    }


}
公共类ClientDataAdapter{
私有静态最终字符串数据库\u NAME=“clientbase.db”;
私有静态最终int数据库_VERSION=1;
静态最终字符串表\u CLIENT=“clienttable”;
私有静态语境;
静态sqlitedc数据库;
私有SQLiteStatement insertStmt;
私有静态最终字符串INSERT=“插入到”
+表“客户+”(完整名称、地址、电话号码)值(?,?);
公共ClientDataAdapter(上下文){
ClientDataAdapter.context=上下文;
OpenHelper OpenHelper=新的OpenHelper(ClientDataAdapter.context);
ClientDataAdapter.dc=openHelper.getWritableDatabase();
this.insertStmt=ClientDataAdapter.dc.compileStatement(INSERT);
}
公共长插入(字符串名称完成、字符串地址客户端、字符串数字电话){
this.insertStmt.bindString(1,nom_complett);
this.insertStmt.bindString(2,adresse_客户端);
this.insertStmt.bindString(3,数字电话);
返回此.insertStmt.executeInsert();
}
public void deleteAll(){
dc.delete(TABLE_CLIENT,null,null);
}
公共阵列列表selectAll()
{
ArrayList=新建ArrayList();
Cursor Cursor=dc.query(TABLE_CLIENT,新字符串[]{“\u id”,“nom_complete”,“adresse_CLIENT”,“numero_telephone”},
空,空,空,空,“名称完整asc”);
int x=0;
if(cursor.moveToFirst()){
做{
String[]b1=新字符串[]{cursor.getString(0),cursor.getString(1),cursor.getString(2),cursor.getString(3)};
列表。添加(b1);
x=x+1;
}while(cursor.moveToNext());
}
if(cursor!=null&!cursor.isClosed()){
cursor.close();
} 
cursor.close();
退货清单;
}
公共布尔删除(长rowId){
返回dc.delete(TABLE_CLIENT,BaseColumns._ID+“=”+rowId,null)>0;
}
私有静态类OpenHelper扩展了SQLiteOpenHelper{
OpenHelper(上下文){
super(上下文、数据库名称、null、数据库版本);
}
@凌驾
public void onCreate(SQLiteDatabase dc){
dc.execSQL(“创建表”+表\客户端+”(\u id整数主键,名称\完整文本不为空,地址\客户端文本不为空,数字\电话文本不为空);
}
@凌驾
public void onUpgrade(SQLiteDatabase dc、int-oldVersion、int-newVersion){
dc.execSQL(“如果存在删除表”+表\客户端);
onCreate(dc);
}
}
}

您使用_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
用于
\u 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();

                 }
         })
字符串[]不是提交给ArrayAdapter,而是传递ArrayList

 int x = 0;
 String stg = "";
 stg1 = new ArrayList<String>();
 for (String[] name : names2) {
     stg = name[1]+"         "+name[2]+ "         "+name[3];
     stg1.add(stg);
     x++;
  }
intx=0;
字符串stg=“”;
stg1=新的ArrayList();
对于(字符串[]名称:名称2){
stg=名称[1]+“”+名称[2]+“”+名称[3];
stg1.添加(stg);
x++;
}

当然,stg1必须是一个数组列表

AbstractList
,它的
Itereator
不支持删除

 int x = 0;
 String stg = "";
 stg1 = new ArrayList<String>();
 for (String[] name : names2) {
     stg = name[1]+"         "+name[2]+ "         "+name[3];
     stg1.add(stg);
     x++;
  }
在(:)的
中迭代时,不能修改该列表。将感兴趣的项目或其索引保存在另一个L中