我有一个带有textview和按钮的列表视图。
我需要在列表视图中单击按钮时更新文本视图,并隐藏按钮。
单击按钮时,它会更新textview值。但是,当我向下滚动并向上移动时,textview内容将更改回上一个值,并且按钮可见。
Customadapter.java
public class CustomAdapter extends BaseAdapter {
ArrayList cusList = new ArrayList();
LayoutInflater inflater;
Context context;
public BookAdapter(Context context, ArrayList cusList) {
this.cusList = cusList;
this.context = context;
inflater = LayoutInflater.from(this.context);
}
@Override
public int getCount() {
return cusList.size();
}
@Override
public Object getItem(int position) {
return cusList.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list, parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
Bookdata data = (Bookdata) getItem(position);
viewHolder.mytv.setText(data.getName());
viewHolder.mybtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
v.setVisibility(View.GONE);
viewHolder.setText("Clicked");
}
});
return convertView;
}
private class ViewHolder {
TextView mytv;
Button mybtn;
public ViewHolder(View item) {
mytv = (TextView)item.findViewById(R.id.mytv);
mybtn = (Button)item.findViewById(R.id.mybtn);
}
}
}
Bookdata.java
public class Bookdata {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Listview代码:
ListView books= (ListView) findViewById(R.id.books);
ArrayList bookList = new ArrayList();
for (int i = 0; i < 10; i++) {
Bookdata data = new Bookdata();
data.setName("position "+i);
bookList.add(data);
}
CustomAdapter allbookAda= new CustomAdapter(context, bookList);
books.setAdapter(allbookAda);
allbookAda.notifyDataSetChanged();
答案 0 :(得分:1)
为每个listitem维护一个标志,以检查哪个列表项已更新。
基本上在模型类中使用一个标志值。
public class Bookdata {
private String name;
private boolean flag;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
}
在您的getview方法中添加以下更改
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list, parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
Bookdata data = (Bookdata) getItem(position);
viewHolder.mytv.setText(data.getName());
if(data.getFlag){
viewHolder.mybtn.setVisibility(View.GONE);
}
viewHolder.mybtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
v.setVisibility(View.GONE);
viewHolder.setText("Clicked");
cusList.get(position).setFlag(true);
}
});
return convertView;
}
答案 1 :(得分:1)
我希望这会对你有所帮助。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list, parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
final Bookdata data = (Bookdata) getItem(position);
viewHolder.mytv.setText(data.getName());
if(data.getName().equals("Clicked")){
viewHolder.mybtn.setVisibility(View.GONE);
}
viewHolder.mybtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
data.setStatus("Clicked");
v.setVisibility(View.GONE);
viewHolder.setText("Clicked");
}
});
return convertView;
}
答案 2 :(得分:0)
这是一种常见的行为,因为listview使用循环视图,上面的答案是正确的但我更喜欢使用DataBinding更清洁的方法