我尝试通过点击按钮编辑自定义列表视图中的数据。在Listener里面我编辑这行中的数据
data.get(position).setName("data");
数据被编辑,但是当我移动带有编辑数据的列表视图超出窗口范围时,我看到了变化,当我向后移动时,我看到了变化。我的目标是在单击按钮时查看此时的更改。这该怎么做?? 在每行的ListView中,我有3个textview和1个按钮,我想编辑textview。
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
public class UserCustomAdapter extends ArrayAdapter<User> {
int layoutResourceId;
ArrayList<User> data = new ArrayList<User>();
private DataSource datasource;
private Context context;
private Comment comment;
private String[] dane;
private UserHolder holder;
private User user;
public UserCustomAdapter(Context context, int layoutResourceId,
ArrayList<User> data) {super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new UserHolder();
holder.textName = (TextView) row.findViewById(R.id.textView1);
holder.textPrice = (TextView) row.findViewById(R.id.textView2);
holder.textAttribute = (TextView) row.findViewById(R.id.textView3);
holder.btnEdit = (Button) row.findViewById(R.id.button1);
row.setTag(holder);
} else {
holder = (UserHolder) row.getTag();
}
user = data.get(position);
holder.textName.setText(user.getName());
holder.textPrice.setText(user.getPrice());
holder.textAttribute.setText(user.getAttribute());
holder.btnEdit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
/* datasource = new DataSource(getContext());
datasource.open();
comment = datasource.getComment("1");
dane = comment.toString().split(" ");
*/
data.get(position).setName("data");
System.out.println(position);
}
});
return row;
}
static class UserHolder {
TextView textName;
TextView textPrice;
TextView textAttribute;
Button btnEdit;
}
}
答案 0 :(得分:4)
更新后使用notifyDataSetChanged方法