将事件绑定到ListView的子节点并根据android中的Click操作更改ListView

时间:2014-10-28 06:08:51

标签: android listview android-listview

我有一个ListView和一个适配器来处理ListView。我想将Click事件绑定到ListView的子节点(比如说'Button')。只要单击按钮,我就需要在ListView视图上应用更改。

例如,

活动:

Class SampleActivity extends Activity{
  ListView listView;
  SampleAdapter adapter;
  List<SampleObject> sampleObjects;

  onCreate(){
    sampleObjects = new ArrayList<SampleObject>;
    listView = findViewById(R.id.listView);
    SampleAdapter adapter = new SampleAdapter(context, R.layout.list_view_item, sampleObjects);
    listView.setAdapter(adapter)
  }

}

ListViewItem的布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="5.0dip">

    <TextView
        android:id="@+id/sampleName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="3.0dip"
        android:layout_weight="1" />

    <Button
        android:id="@+id/sampleButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btnText" />

</LinearLayout>

适配器类

public class SampleAdapter extends ArrayAdapter<SampleObject>{
   public SampleAdapter(Context context, Integer resourceId, List<SampleObject> objects ){
       super(context, resourceId, objects);
   }

   @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater myInflater = (LayoutInflater) getContext().getSystemService(getContext().LAYOUT_INFLATER_SERVICE);
            convertView = myInflater.inflate(R.layout.friends_list_aitem, null);
        }
        SampleObject currentSampleObject = getItem(position);
        TextView name = (TextView) convertView.findViewById(R.id.sampleName);
        name.setText(currentSampleObject.getName());
        Button button = (Button) convertView.findViewById(R.id.sampleButton);
        button.setOnClickListener(new OnClickListener(){
            public void onClick(View view){
                // Here I want make Some Changes on the name of currentSampleObject. and I want to apply the changes on the View of ListItem
            }
        });
    }
}

1 个答案:

答案 0 :(得分:0)

实际上我们可以通过一个名为notifyDataSetChanged()的方法实现这一目的。

适配器get方法应该是,

public View getView(final int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        LayoutInflater myInflater = (LayoutInflater) getContext().getSystemService(getContext().LAYOUT_INFLATER_SERVICE);
        convertView = myInflater.inflate(R.layout.friends_list_aitem, null);
    }
    SampleObject currentSampleObject = getItem(position);
    TextView name = (TextView) convertView.findViewById(R.id.sampleName);
    name.setText(currentSampleObject.getName());
    Button button = (Button) convertView.findViewById(R.id.sampleButton);
    button.setOnClickListener(new OnClickListener(){
        public void onClick(View view){
            // make changes on the name of currentSampleObject.
            SampleAdapter.this.notifyDataSetChanged();
        }
    });
}