我有两个gridView,两个ArrayList数据和两个不同的ArrayAdapter,但为什么我看到它们都是相同的数据 - “2”?
data1.add("1");
ArrayAdapter<String> listAdapter1 = new ArrayAdapter<String>(this, R.layout.item3, R.id.editText1, data1) {
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item3, null);
}
final String theData = getItem(position);
final EditText editText = (EditText) convertView.findViewById(R.id.editText1);
editText.setText(theData);
editText.addTextChangedListener(new MyTextWatcher(position));
return convertView;
}
};
gvMain1.setAdapter(listAdapter1);
data2.add("2");
ArrayAdapter<String> listAdapter2 = new ArrayAdapter<String>(this, R.layout.item3, R.id.editText1, data2) {
@Override
public View getView(final int position, View convertView, ViewGroup parent){
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item3, null);
}
final String theData = getItem(position);
final EditText editText = (EditText) convertView.findViewById(R.id.editText1);
editText.setText(theData);
editText.addTextChangedListener(new MyTextWatcher(position));
return convertView;
}
};
gvMain2.setAdapter(listAdapter2);
答案 0 :(得分:0)
我创建了类:
public class MyArrayAdapter extends ArrayAdapter<String> {
private final Activity context;
private final ArrayList<String> names;
public MyArrayAdapter(Activity context, ArrayList<String> names) {
super(context, R.layout.item3, names);
this.context = context;
this.names = names;
}
static class ViewHolder {
public EditText editText;
}
public ArrayList<String> getData()
{
return this.names;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.item3, null, true);
holder = new ViewHolder();
holder.editText = (EditText) rowView.findViewById(R.id.editText1);
rowView.setTag(holder);
} else {
holder = (ViewHolder) rowView.getTag();
}
holder.editText.setText(names.get(position));
holder.editText.addTextChangedListener(new MyTextWatcher(position));
return rowView;
}
class MyTextWatcher implements TextWatcher
{
private int position;
public MyTextWatcher(int position)
{
this.position = position;
}
public void afterTextChanged(Editable s)
{
names.set(position, s.toString());
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,int arg3)
{
}
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,int arg3)
{
}
}
}
从我的活动中调用它:
final MyArrayAdapter myAd1=new MyArrayAdapter(this,data1);
final MyArrayAdapter myAd2=new MyArrayAdapter(this,data2);
gvMain1.setAdapter(myAd1);
gvMain2.setAdapter(myAd2);