如何更新列表视图,当我点击按钮? [英] How to update the listview when i click the button?

查看:119
本文介绍了如何更新列表视图,当我点击按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    public class MainActivity extends Activity {
    ListView list;
    String[] abc={"1","2","3"};
    MyCustomAdapter adapter;
    Button refresh;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            list=(ListView) findViewById(R.id.list);
             refresh=(Button) findViewById(R.id.refresh);
             adapter=new MyCustomAdapter(MainActivity.this,abc);
            list.setAdapter(adapter);

        }
         private class MyCustomAdapter extends BaseAdapter {
                private final Context context;
                private ArrayList mData = new ArrayList();
                private LayoutInflater mInflater;
                private String[] hashmap;
                ViewHolder holder = null;
                public MyCustomAdapter(Context context,String[] hashMaps) {
                    super();
                    this.context = context;
                    this.hashmap = hashMaps;
                    mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                }
                public int getCount() {

                    return hashmap.length;

                }


                public Object getItem(int position) {
                    return hashmap.length;
                }


                public long getItemId(int position) {

                    return position;
                }

                @Override
                public void notifyDataSetChanged() // Create this function in your adapter class
                {
                    super.notifyDataSetChanged();
                }
                @Override
                public View getView(final int position, View convertView, ViewGroup parent) {
                    System.out.println("getView " + position + " " + convertView);
                   // final ViewHolder holder = null;
                    if (convertView == null) {
                        convertView = mInflater.inflate(R.layout.view1, null);
                        holder = new ViewHolder();
                        holder.textView = (TextView)convertView.findViewById(R.id.text);
                        holder.btn = (Button)convertView.findViewById(R.id.refresh);
                        convertView.setTag(holder);
                    } else {
                        holder = (ViewHolder)convertView.getTag();
                    }
                    holder.textView.setText(hashmap[position]);

                    holder.btn.setOnClickListener(new OnClickListener(){  
                           @Override
                          public void onClick(View v) {
                             RelativeLayout rl = (RelativeLayout)v.getParent();
                            holder.textView = (TextView)rl.getChildAt(position);
                            Log.i("position",String.valueOf(position));
                            holder.textView.setText("10");
                           }
                         });

                    return convertView;

                }

            }

            public static class ViewHolder {
                public TextView textView;
                public Button btn;
            }




    }

view.xml用

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
   android:id="@+id/real"
    >
    <TextView 
                android:id="@+id/text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:text="Text"
                android:layout_marginTop="15dp"
                android:layout_marginLeft="20dp"
                />
    <Button 
                android:id="@+id/refresh"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/text"
                android:text="button"
                 android:layout_marginLeft="20dp"
                />
    </RelativeLayout>

下面在我的ListView,当我点击该按钮值不是我的适配器view.how内改变来解决这个?列表包含每3个值position.so当我在点击位置单击该按钮,该值将被改变,而不是旧值。

Here in my ListView,when i click the button the value is not changed inside my adapter view.how to resolve this? the list contains 3 values of each position.so when i click the button in the clicked position,the value is going to be change instead of old value.

推荐答案

更改getView方法code如下。

Change getView method code as below.

@Override
        public View getView(final int position, View convertView,ViewGroup parent) {
            View vi = convertView;
            final TextView textView;
            if (convertView == null)
                inflater = (LayoutInflater)    context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            vi = inflater.inflate(R.layout.vi, null);

            textView = (TextView) vi.findViewById(R.id.text);
            Button btn = (Button) vi.findViewById(R.id.refresh);
            textView.setText(hashmap[position]);
            btn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.i("position  ", position + "");
                    textView.setText("10");
                }
            });

            return vi;
        }

这篇关于如何更新列表视图,当我点击按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆