更新ListView中的特定项目

时间:2016-06-19 10:25:50

标签: android listview android-adapter

我想更新(和删除)ListView中特定行,知道ListView中包含的对象的属性(id和名称)。我不知道这个位置,因此我无法使用

public class ListViewAdapter extends ArrayAdapter<ModelTimer> {


    public List<ModelTimer> mListOfTimers;
    private Context mContext;

    public ListViewAdapter(Context context, List<ModelTimer> modelTimerList) {
        super(context, R.layout.item_active_timer, modelTimerList);
        this.mListOfTimers = modelTimerList;
        this.mContext = context;
    }

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

        // Get the data item for this position
        ModelTimer modelTimer = getItem(position);
        // Check if an existing view is being reused, otherwise inflate the view
        ViewHolder holder;// view lookup cache stored in tag

        if (convertView == null) {
            holder = new ViewHolder();
            LayoutInflater inflater = LayoutInflater.from(getContext());
            convertView = inflater.inflate(R.layout.item_active_timer, parent, false);
            holder.tvTime = (TextView) convertView.findViewById(R.id.active_timer_timeLeft);
            holder.tvTitle = (TextView) convertView.findViewById(R.id.active_timer_name);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        // Populate the data into the template view using the data object
        holder.tvTime.setText(modelTimer.expirationTime);
        holder.tvTitle.setText(modelTimer.name);

        // Return the completed view to render on screen
        return convertView;
    }

    @Override
    public int getPosition(ModelTimer item) {
        return super.getPosition(item);
    }

    // View lookup cache
    public class ViewHolder {
        TextView tvTitle;
        TextView tvTime;
    }


}

我正在使用List View的适配器,它正在扩展我的模型类的ArrayAdapter。

如何覆盖方法getPosition? 这是我的ListViewAdapter

doSomething(14754572,782339,2,'')

1 个答案:

答案 0 :(得分:0)

不是最好的方法,但看看这段代码:

 @Override
    public int getPosition(ModelTimer item) {
     int pos = -1;
    for(int i = 0; i < mListOfTimers.size(); i++ )
      if(mListOfTimers.name.equals(item.name) // and check id
      pos = i;
        return pos;
    }