删除了最后一个Listview项而不是选中的项

时间:2014-08-14 17:14:12

标签: java android listview custom-adapter items

我有一个带有项目的listView。 当我长按其中一个时,会出现一个删除按钮。但是当我按下它时,它是删除的最后一个项目,而不是我按下的那个......

这是我的代码:

appList0 = new ArrayList<Appreciation>();
String[] app0 = settings.getString("App0", "aucune remarque").toString().split(";");          
for (String app : app0) 
    appList0.add(new Appreciation(app));
dataAdapter0 = new MyCustomAdapter(this,R.layout.affichageitem, appList0);
ListView listView0 = (ListView) findViewById(R.id.listView0);
listView0.setAdapter(dataAdapter0);

和适配器

private class MyCustomAdapter extends ArrayAdapter<Appreciation> {

          private ArrayList<Appreciation> appList;

          public MyCustomAdapter(Context context, int textViewResourceId,ArrayList<Appreciation> appList) {
              super(context, textViewResourceId, appList);
              this.appList = new ArrayList<Appreciation>();
              this.appList.addAll(appList);
          }

          private class ViewHolder {
           TextView text;
           ImageButton button;
          }

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

              ViewHolder holder=null;
              LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

               if (convertView == null) {                              
                   convertView = vi.inflate(R.layout.affichageitem, null);                
                   holder = new ViewHolder();
                   holder.text = (TextView) convertView.findViewById(R.id.textview);
                   holder.button = (ImageButton) convertView.findViewById(R.id.editButton);
                   convertView.setTag(holder);
               }
               else {
                   holder = (ViewHolder) convertView.getTag();
                   holder.button.setVisibility(View.INVISIBLE);
               }


               holder.text.setOnClickListener( new View.OnClickListener() { 
                     public void onClick(View v) { 
                          TextView tv = (TextView) v ; 
                          //To DO
                         } 
                }); 

               holder.button.setOnClickListener( new View.OnClickListener() { 
                     public void onClick(View v) { 
                            ListView listView0=(ListView)findViewById(R.id.listView0); 

                            Log.e("DELETING : ",appList0.get(position).getValue());  
                            appList0.remove(position);   //DELETING HERE
                            dataAdapter0.notifyDataSetChanged();

                            ImageButton iB = (ImageButton) v ; 
                            iB.setVisibility(View.INVISIBLE);
                     }          
                });

               holder.text.setOnLongClickListener( new View.OnLongClickListener() { 
                     public boolean onLongClick(View v) { 
                          TextView tv = (TextView) v ;
                          ImageButton editButton = (ImageButton) ((ViewGroup) v.getParent()).findViewById(R.id.editButton);
                          editButton.setVisibility(View.VISIBLE);
                          return true;
                         } 
                }); 

               Appreciation currentApp = appList.get(position);
               holder.text.setText(currentApp.getValue());
               holder.button.setTag(currentApp);          
               return convertView;

          }

         }

2 个答案:

答案 0 :(得分:1)

由于getView()针对每个项目运行,因此您需要在按钮上设置标记,以便它可以获得正确的位置。

之前的事情

// use position for the tag param
holder.button.setTag(position);
holder.button.setOnClickListener( new View.OnClickListener() {

然后从点击的View中检索标记并使用

 holder.button.setOnClickListener( new View.OnClickListener() { 
     public void onClick(View v) { 
     int position = ((Button)v).getTag();  // cast the view to Button and get the tag

      Log.e("DELETING : ",appList0.get(position).getValue());  
       appList0.remove(position);   //DELETING HERE
       dataAdapter0.notifyDataSetChanged();

答案 1 :(得分:0)

我认为您在使用appList0和appList时遇到一些问题。尝试这些更改可能有所帮助:

                    Log.e("DELETING : ",appList0.get(position).getValue());  
                    appList0.remove(position);   //DELETING HERE
                    dataAdapter0.notifyDataSetChanged();

                    Log.e("DELETING : ",appList.get(position).getValue());  
                    appList.remove(position);   //DELETING HERE
                    MyCustomAdapter.this.notifyDataSetChanged();

变化

      public MyCustomAdapter(Context context, int textViewResourceId,ArrayList<Appreciation> appList) {
          super(context, textViewResourceId, appList);
          this.appList = new ArrayList<Appreciation>();
          this.appList.addAll(appList);
      }

      public MyCustomAdapter(Context context, int textViewResourceId,ArrayList<Appreciation> appList) {
          super(context, textViewResourceId, appList);
          this.appList = appList;
      }