ListView总是从CustomAdaptor中删除最后一行

问题描述 投票:0回答:1

我的ListView在每行上包含更多按钮,它将弹出带有删除选项的列表。当我单击删除选项时,它会删除最后一个项目。在CustomAdaptor中,我从ArrayList中删除项目后尝试了notifyDataSetChanged()。在这里,它总是删除最后一个项目,而与项目位置无关。

CustomAdaptor代码

public class CustomAdapter extends BaseAdapter {
Context context;
List<String> lrowItems ;
ArrayList<String> listItems;
String className;
CalHelper calHelper;
Settings settings;
LayoutInflater inflter;
public CustomAdapter(Context applicationContext, ArrayList<String> listItems) {
    this.context = applicationContext;
    this.listItems = listItems;
    this.className = context.getClass().getSimpleName();
    this.calHelper  = new CalHelper(applicationContext);
    this.settings  = new Settings(applicationContext);
    inflter = (LayoutInflater.from(applicationContext));
}

@Override
public int getCount() {
    if(listItems != null) {
        return listItems.size();
    }else{
        return 0;
    }
}

@Override
public Object getItem(int position) {
    return listItems.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

//With following overrides Prevent listview item will not mix randomly
//and convertView provided is of the appropriate type.
@Override
public int getViewTypeCount() {
    if(getCount() > 0){ //for no records in returning view
        return getCount();
    }else{
        return super.getViewTypeCount();
    }
}

@Override
public int getItemViewType(int position) {
    return position;
}

/* private view holder class for holding calculation history*/
private class HistoryViewHolder {
    TextView more;
    TextView expression;
    TextView result;
    TextView shortNote;
    TextView expTime;
}

/* private view holder class for holding GST calculations history*/
private class GstHistoryViewHolder {
    TextView amount;
    TextView gst;
    TextView total;
    TextView expTime;
    ImageView share;
}

/* private view holder class */
private class BMIViewHolder {
    TextView bmiScore;
    TextView bmiDate;
    TextView bmiWeight;
}

@Override
public View getView(final int position, View convertView, ViewGroup viewGroup) {
    LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    //final int pos = position;
    if(className.equals("MainActivity")) {
        final HistoryViewHolder holder = new HistoryViewHolder();
        final String expString = listItems.get(position);
        final String expre = expString.substring(0, expString.indexOf(":"));
        final String expreTime = expString.substring(expString.indexOf(":") + 1, expString.length());
        final String result = expre.substring(expre.indexOf("=")+1);
        String timePattern = calHelper.calHistoryTimePattern(expreTime);
        SimpleDateFormat localDateFormat = new SimpleDateFormat(timePattern);

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.listview_row, null);
            holder.more = (TextView) convertView
                    .findViewById(R.id.more);
            holder.expression = (TextView) convertView.findViewById(R.id.expression);
            holder.result = (TextView) convertView.findViewById(R.id.result);
            holder.shortNote = (TextView) convertView.findViewById(R.id.shortNote);
            holder.expTime = (TextView) convertView.findViewById(R.id.expTime);
            holder.expression.setText(expre.substring(0, expre.indexOf("=")));
            holder.result.setText(result);
            String time = localDateFormat.format(new Date(expreTime));
            holder.expTime.setText(time);
            holder.shortNote.setText(settings.getHistoryNote(context,expString));

            convertView.setTag(holder);

            final TextView btnMore = (TextView) holder.more;
            btnMore.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showPopup(v, position, expString, expre, expreTime, holder.shortNote);
                }
            });

        }
    }


    return convertView;
}


public void showPopup(View v, final int position, final String expString, final String expression, final String expTime, final TextView shortNote) {
    final PopupMenu popup = new PopupMenu(context, v);

    //truncate title with ellipsis for more characters
    final String noteTitle = expression.length()>10? expression.substring(0, 10) + "..." : expression;
    MenuInflater inflater = popup.getMenuInflater();
    inflater.inflate(R.menu.history_menu, popup.getMenu());
    //registering popup with OnMenuItemClickListener
    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        public boolean onMenuItemClick(MenuItem item) {
            Intent intent = new Intent(context, MainActivity.class);
            int itenId = item.getItemId();
            switch (itenId){
                case R.id.mnuInsertExpression:
                    //some code here
                break;
                case R.id.mnuHisNote:
                    //some code here                        
                    break;
                case  R.id.mnuCopyHisRecord:
                    calHelper.copyToClipboard(expression);
                    break;
                case R.id.mnuShareHisRecord:
                    calHelper.shareText(expression);
                    break;
                case  R.id.mnuDeleteHisRecord:  //This is where I am deleting item
                    System.out.println("Pos:"+position);
                      listItems.remove(position);
                      notifyDataSetChanged();
                    Collections.reverse(listItems); //sort descending by time again
                    settings.clearSharedPre(context,expString); //remove note attached to this expression from ShredPref
                    settings.saveArrayList(context, listItems, "EXP_HISTORY");
                    intent.putExtra("HISTORY_RECORD_DELETED", true);
                    Toast.makeText(context,"Record removed from history", Toast.LENGTH_SHORT).show();
                    context.startActivity(intent);
                    break;
            }
            return true;
        }
    });
    popup.show();
}

非常感谢您的帮助。

android android-listview listviewitem custom-adapter notifydatasetchanged
1个回答
0
投票

这是因为您正在使用getView参数的位置,所以它将始终是您使用它时的最后一个索引。

相反,您需要将该位置添加到视图中,可能是您通常在convertView中所做的(标签可以是您想要的任何东西,在创建每个项目视图时在getView方法中执行convertView.setTag(position),然后在showpopup中的delete方法中,您可以执行(int)view.getTag()获取要从数据集中删除的位置。

© www.soinside.com 2019 - 2024. All rights reserved.