如何引用listView中的项目

时间:2015-01-28 15:17:01

标签: android listview android-listview

我有一个带有自定义布局的Listview(TextView,Checkbox和imageView),自定义ListView设置为扩展BaseaAapter的适配器。

getView()方法发布在下面。

我想要做的是,当触摸imageView时,应删除ListView中的该项并the adapter should be notified about the change。在我使用的mainActivity中" OnItemClickListener"但我不知道如何引用扩展getView()

类中Baseadapter方法中存在的imageView

如何从mainActivity引用getView()方法并基于clickAction删除项目并通知listView的adapeter?

换句话说,如果我在listView中有5个项目,如果点击项目编号3的imageView,则应删除整个项目3,并通过调用" notifyDatasetChanged(" notifyDatasetChanged)通知适配器( )"

OnItemClickListener

private OnItemClickListener listViewitemListener = new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // TODO Auto-generated method stub
    }
};

getView

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    if (convertView == null) {
        LayoutInflater layoutinflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = layoutinflater.inflate(R.layout.list_items_layout, null);
    }

    TextView tv = (TextView) convertView.findViewById(R.id.tvlist_topic);
    CheckBox cb = (CheckBox) convertView.findViewById(R.id.cbList_hook);
    ImageView iv = (ImageView) convertView.findViewById(R.id.ivList_delete);

    tv.setText(this.arrayList.get(position));
    return convertView;
}

1 个答案:

答案 0 :(得分:-1)

我不确定我理解你的问题,但如果我这样做了,你可以这样解决:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    if (convertView == null) {
        LayoutInflater layoutinflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = layoutinflater.inflate(R.layout.list_items_layout, null);
    }

    TextView tv = (TextView) convertView.findViewById(R.id.tvlist_topic);
    CheckBox cb = (CheckBox) convertView.findViewById(R.id.cbList_hook);
    ImageView iv = (ImageView) convertView.findViewById(R.id.ivList_delete);

    tv.setText(this.arrayList.get(position));

    iv.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            v.setVisibility(View.INVISIBLE); //or View.GONE
        }
    }
    return convertView;
}

编辑:如果要删除整个列表项,请改用:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    if (convertView == null) {
        LayoutInflater layoutinflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = layoutinflater.inflate(R.layout.list_items_layout, null);
    }

    TextView tv = (TextView) convertView.findViewById(R.id.tvlist_topic);
    CheckBox cb = (CheckBox) convertView.findViewById(R.id.cbList_hook);
    ImageView iv = (ImageView) convertView.findViewById(R.id.ivList_delete);

    tv.setText(this.arrayList.get(position));

    iv.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            arrayList.remove(position);
            notifyDatasetChanged();
        }
    }
    return convertView;
}