这是我的listview,我的问题是为什么我试图点击我的ImageButton(在listview中),但textview(在listview中)永远不会改变我想要的值。这需要我整个上午,有人可以帮助我,谢谢。
public class ViewAdapter extends BaseAdapter implements Filterable {
LayoutInflater mInflater;
public ViewAdapter() {
mInflater = LayoutInflater.from(getActivity());
}
@Override
public int getCount() {
return pl.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.listproduct, null);
}
final TextView ProductCode = (TextView) convertView.findViewById(R.id.productcode);
ProductCode.setText("" + pl.get(position).getProductCode());
final TextView Qty = (TextView) convertView.findViewById(R.id.qty);
Qty.setText("" + pl.get(position).getQty());
final TextView Description = (TextView) convertView.findViewById(R.id.description);
Description.setText("" + pl.get(position).getProductName());
final String p = Description.getText().toString();
final TextView Price = (TextView) convertView.findViewById(R.id.price);
Price.setText("" + pl.get(position).getProductPrice());
final ImageButton minus = (ImageButton) convertView.findViewById(R.id.minus);
minus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Qty.setText("testing")// textview never change to "testing"
}
});
notifyDataSetChanged();
return convertView;
}
答案 0 :(得分:0)
基本上你应该使用RecyclerView,但如果你仍想使用listView,请使用以下链接中的view-holder方法:
你应该永远不要在getView方法中调用notifyDataSetChanged()
作为你的列表视图滚动的多次调用
答案 1 :(得分:0)
请尝试使用持有人的代码,如下所示
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_entry, null);
holder = new ViewHolder();
holder.nameTextView = (TextView) convertView.findViewById(R.id.person_name);
holder.surnameTextView = (TextView) convertView.findViewById(R.id.person_surname);
holder.personImageView = (ImageView) convertView.findViewById(R.id.person_image);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
Person person = getItem(position);
holder.nameTextView.setText(person.getName());
holder.surnameTextView.setText(person.getSurname());
//holder.personImageView.setImageBitmap(person.getImage());
return convertView;
}
然后点击以下
holder.personImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
holder.nameTextView.setText("testing")// textview never change to "testing"
}
});
答案 2 :(得分:0)
Use this code :
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.listproduct, null);
}
final TextView ProductCode = (TextView) convertView.findViewById(R.id.productcode);
ProductCode.setText("" + pl.get(position).getProductCode());
final TextView Qty = (TextView) convertView.findViewById(R.id.qty);
final TextView Description = (TextView) convertView.findViewById(R.id.description);
Description.setText("" + pl.get(position).getProductName());
final String p = Description.getText().toString();
final TextView Price = (TextView) convertView.findViewById(R.id.price);
Price.setText("" + pl.get(position).getProductPrice());
ImageButton minus = (ImageButton) convertView.findViewById(R.id.minus);
minus.setTag(position);
minus.setOnClickListener(clickButton);
return convertView;
}
private View.OnClickListener clickButton = new View.OnClickListener() {
@Override
public void onClick(View v) {
Object tag =v.getTag();
if(tag!=null)
{
int pos = (Integer)tag;
Qty.setText(pl.get(position).getQty());
notifyDataSetChanged();
}
}
} ;
答案 3 :(得分:0)
据我所知,这个问题无法解决,因为一旦你在自定义 listView 中填充数据,你就重新创建了它,或者再次用文本调用自定义 listView 显示你想改变!