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

在Android中使用ListView时,数据不会刷新,android,android-listview,Android,Android Listview,我的应用程序中有一个列表视图。每行都有一个文本视图和按钮。当我点击按钮时,buton文本将发生变化。这是密码 主要活动 public class MainActivity extends Activity { ArrayAdapter<RowObj> apt; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setCo

我的应用程序中有一个
列表视图
。每行都有一个
文本视图
按钮
。当我点击按钮时,buton文本将发生变化。这是密码

主要活动

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;
    }

}
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;
}
}
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.setbtnalable(“确定”);


非常感谢。

在适配器中添加行按钮单击是
notifyDataSetChanged()终于。

您忘记向适配器指定数据已更改。您可以通过调用methode来实现这一点:

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";
            }
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";
            }