如何在项目点击时更新listview项目?

时间:2013-07-16 17:24:30

标签: android android-listview

我在每个列表项目中都有按钮,当我点击按钮时我正在调用服务将数据推送到服务器,最后根据响应我想改变像button.i我正在使用下面的代码来改变

在活动类

CustomSimpleAdapter adapter = new CustomSimpleAdapter (
                    this, arr,
                    R.layout.listviewitem, new String[] {
                            "count", "likes"}, new int[] { 
                            R.id.counttxt, 
                            R.id.likes},list);
            list.setAdapter(adapter);

在自定义simpleAdater类

  public View getView(final int position, View convertView, ViewGroup parent) {
            View vi = convertView;
            if (convertView == null) {
                holder = new ViewHolder();
                vi = inflater.inflate(R.layout.listviewitem, null);
                holder.like = (TextView) vi.findViewById(R.id.like); // title
                holder.count= (TextView) vi.findViewById(R.id.counttxt); // artist
                   vi.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

              holder.like.setId(position);
        holder.like.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                    int listItemPosition = v.getId();
//calling web service here based on response i want to increase count size

if(respone.equal("success")
{
updatecount(listItemPosition );
}

            }
        });



private void updatecount(int index) {
        View v = list.getChildAt(index - list.getFirstVisiblePosition());
            someText1 = (TextView) v.findViewById(R.id.counttxt);//here i am getting null
        int i = Integer.parseInt(data.get(index).get("Count"));
        i++;
        someText1.setText(Integer.toString(i) + "count");
    }

当我更新数据时,我得到列表视图项的空值。如果有任何想法请帮帮我。

2 个答案:

答案 0 :(得分:0)

您是否尝试过AdapterView.OnItemClickListener?

http://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html

onItemClick(AdapterView<?> parent, View view, int position, long id) 

view是单击的列表项,因此如果您想要更改它,则可以更改view参数。

答案 1 :(得分:0)

最后我在调用notifyDataSetChanged()

之后通过更新数组列表获得我的解决方案
    map.put("like",  increament by previous value);
    arrlist.set(listItemPosition, map);
    notifyDataSetChanged();