在Android中使用ListView时,数据不会刷新

时间:2013-12-17 10:23:36

标签: android android-listview

我的应用中有一个ListView。每行都有TextViewButton。当我单击按钮时,预计文本会发生变化。这是代码

MainActivity

public class MainActivity extends Activity {

ArrayAdapter<RowObj> apt;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    List<RowObj> lstObj = new ArrayList<RowObj>();
    lstObj.add(new RowObj("Hello 1", "Buton 1"));
    lstObj.add(new RowObj("Hello 2", "Buton 2"));
    lstObj.add(new RowObj("Hello 3", "Buton 3"));
    lstObj.add(new RowObj("Hello 4", "Buton 4"));

    apt = new RowObjAdapter(this, R.layout.row_object, lstObj);
    lstv1.setAdapter(apt);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
    }

}

RowObj.java

package com.example.listview;

    public class RowObj {
String txtTitle = "";

String btnLable = "";
public RowObj(String text, String btn_txt) {
    // TODO Auto-generated constructor stub
    this.txtTitle = text;
    this.btnLable = btn_txt;
}

public String getTxtTitle() {
    return txtTitle;
}
public void setTxtTitle(String txtTitle) {
    this.txtTitle = txtTitle;
}
public String getBtnLable() {
    return btnLable;
}
public void setBtnLable(String btnLable) {
    this.btnLable = btnLable;
}
 }

RowObjAdapter.java

public class RowObjAdapter extends ArrayAdapter<RowObj>{

// Array list data
List<RowObj> lstRow;
// store the resource (typically row_obj.xml)
LayoutInflater inflater = null;
// store the context (as an inflated layout) -- id layout to make a view in list view
int resource;

public RowObjAdapter(Activity  context, int resource, List<RowObj> objects) {
    super(context, resource, objects);
    this.resource = resource;
    this.lstRow = objects;
    this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return lstRow.size();
}


@Override
public long getItemId(int arg0) {
    // TODO Auto-generated method stub
    return arg0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    RowObjHolder viewHolder;
    if(convertView == null) {
        //convertView = inflater.inflate(R.layout.row_object, null);
        convertView = this.inflater.inflate(resource, parent, false);
        viewHolder = new RowObjHolder();
        viewHolder.btnLable = (Button) convertView.findViewById(R.id.button1);
        viewHolder.txtName = (TextView) convertView.findViewById(R.id.textView1);
        convertView.setTag(viewHolder);
        viewHolder.btnLable.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Button btn = (Button)v;
                RowObj obj1 = (RowObj) btn.getTag();
                obj1.setBtnLable("ok");

                Log.i("pOSITION -- ", obj1.getBtnLable());
                //lstRow.get(position).btnLable = "Checked";
            }
        });
        //
        Log.i("pOSITION", "Value of btnLable is :" +    lstRow.get(position).btnLable);
    } else {
        viewHolder = (RowObjHolder) convertView.getTag();
    }

    RowObj obj = lstRow.get(position);
    Log.i("pOSITION222", "Value of btnLable is :" + lstRow.get(position).btnLable);
    viewHolder.txtName.setText(obj.getTxtTitle());
    viewHolder.btnLable.setText(obj.getBtnLable());
    viewHolder.btnLable.setTag(obj);

    return convertView;
}
}

RowObjHolder.java

public class RowObjHolder {
    TextView txtName;
    Button btnLable;
}

Row_object.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/button1"
    android:layout_alignBottom="@+id/button1"
    android:layout_alignParentLeft="true"
    android:layout_weight="3"
    android:text="TextView" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_marginRight="18dp"
    android:layout_weight="1"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:text="Button" />

</RelativeLayout>

问题是当我点击button1时,button1的文字没有改变(我使用obj1.setBtnLable("ok");)。

非常感谢。

2 个答案:

答案 0 :(得分:2)

在适配器按钮中添加行,最后点击notifyDataSetChanged();

答案 1 :(得分:1)

您忘了向适配器指定数据已更改。你可以通过调用方法来做到这一点:

notifyDataSetChanged();

以下是代码:

@Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Button btn = (Button)v;
                RowObj obj1 = (RowObj) btn.getTag();
                obj1.setBtnLable("ok");
                notifyDataSetChanged();
                Log.i("pOSITION -- ", obj1.getBtnLable());
                //lstRow.get(position).btnLable = "Checked";
            }