java - 删除了最后一个 ListView 项目而不是选定的项目

标签 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;

          }

         }

最佳答案

由于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();

关于java - 删除了最后一个 ListView 项目而不是选定的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25313788/

相关文章:

java - 运行应用程序时布局不显示

python - Kivy:在对 ListAdapter 数据进行排序时无法更新 ListView

java - 通过 JPA 保存 List<String> 时出现问题?

java - Ajax PUT 请求接收空响应

java - 使用 IntelliJ 构建 Gradle android 应用程序失败(未找到 java 问题,但设置正确)

java - GoogleAppEngine 错误 403

java - 应用程序强制关闭。短信广播接收器

android - 无效的应用程序 ID - chromecast

android - mysql 获取的数据不显示在 android 的 ListView 上

java - 应用程序停止使用 listview arrayadapter