我有一个基于RelatieLayout的ListView,有几个TextView和一个EditText。其中一个文本字段根据EditText字段中的值进行更新。
我的问题是在滚动listView时保留editText字段的值。我对BaseAdapter的扩展看起来像这样:
public class ListViewAdapter extends BaseAdapter {
static class ItemViewHolder {
TextView
itemName,
itemPrize,
itemTotalPrize,
EditText
itemAmount;
}
static class CatViewHolder {
TextView
categoryName;
Button
categoryInfo;
}
ArrayList<ListItem> itemsList;
HashMap<Integer, String> volume = new HashMap<Integer, String>();
public ListViewAdapter(ArrayList<ListItem> itemsList) {
this.itemsList = itemsList;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return this.itemList.size();
}
@Override
public ListItem getItem(int position) {
// TODO Auto-generated method stub
return this.itemList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
if (getItem(position).isCategory()) return 0;
else return 1;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (!getItem(position).isCategory()) {
String[] string;// = {"", ""};
final Item item = (Item) getItem(position);
if (row == null) {
row = inflater.inflate(R.layout.item, parent, false);
viewHolder = new ItemViewHolder();
viewHolder.itemName = (TextView) row.findViewById(R.id.itemName);
viewHolder.itemPrize = (TextView) row.findViewById(R.id.itemPrize);
viewHolder.itemTotalPrize = (TextView) row.findViewById(R.id.itemTotalPrize);
viewHolder.itemAmount = (EditText) row.findViewById(R.id.itemAmount);
row.setTag(viewHolder);
} else viewHolder = (DrugViewHolder) row.getTag();
viewHolder.itemName.setText(item.getName());
viewHolder.itemPrize.setText(String.valeuOf(item.getPrize()));
viewHolder.itemAmount.addTextChangedListener(new AmountTextWatcher(item, position, row));
viewHolder.itemAmount.setText(volume.get(item.getId)));
viewHodler.itemTotalPrize.setText(String.valueOf(item.getPrize() * volume.get(item.getId));
} else {
Category category = (Category) getItem(position);
if (row == null) {
row = inflater.inflate(R.layout.category, parent, false);
catViewHolder = new CatViewHolder();
catViewHolder.categoryName = (TextView) row.findViewById(R.id.categoryName);
catViewHolder.categoryInfo = (Button) row.findViewById(R.id.categoryInfo);
row.setTag(catViewHolder);
} else catViewHolder = (CatViewHolder) row.getTag();
catViewHolder.categoryName.setText(category.getCatName());
}
return row;
}
@Override
public void AmountTextWatcher_interface(String amount, int position) {
volume.put(position, amount);
}
编辑,添加了ListItem-structure
ListItem类,如下所示:
package com.buy.ng;
public interface ListItem {
public boolean isCategory();
}
我的两个对象,item和cateogory实现ListItem和isCategory()在item和cateogry中都被覆盖。 Item返回false,类别返回true。
我的AmountTextWatcher看起来像这样:
class AmountTextWatcher implements TextWatcher {
public AmountTextWatcher_Interface textChangedListener = null;
interface AmountTextWatcher_Interface {
public abstract void AmountTextWatcher_interface(String amount, int position);
}
private static final String TAG = "amountTextWatcher";
private TextView textView_TotalPrize,
private Item item;
public PrizeTextWatcher(Item item, int position, View row) {
this.row = row;
this.item = item;
this.textView_TotalPrize = (TextView) row.findViewById(R.id.itemTotalPrize);
}
@Override
public void afterTextChanged(Editable s) {
String amount = s.toString().replace(",", ".").trim();
int dummy;
if (amount.length() == 0 || amount == null ) amount = "0";
try {
dummy = Integer.valueOf(amount);
} catch (NumberFormatException npe) {
amount= "0";
EditText editText = (EditText) view.findViewById(R.id.itemAmount);
editText.setText("");
}
this.textView_prize.setText(Intege.valueOf(amount) * item.getPrize());
textChangedListener.PrizeTextWatcher_interface(amount, item.getId());
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
}
我尝试了几种方法,但是我无法获得要在ListView滚动上保留的EditText字段数量的值。
我试过HashMap amount = new HashMap();并使用在EditText,volume.put(item.getId(),amount)中输入的值填充它,但我似乎无法正确使用它。
尝试使用此PrizeTextWatcher的接口,该接口具有amount和item.getId()作为变量。这个接口在ListViewAdapter中实现,它修改了这个适配器中的HashMap卷,如下所示:
@Override
public void AmountTextWatcher_interface(String amount, int id) {
volume.put(id, amount);
}
任何建议或我这样做都错了吗?作为Android中的一个相对新手,我发现这很难实现......
答案 0 :(得分:0)
更新此代码
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
答案 1 :(得分:0)
选择使用这种ListView的某种有效工作版本来回答我自己的问题。有一些userexperience-问题,但这个BaseAdapter处理edittext-fields。
我的问题中的代码与我的答案中的代码之间的主要区别是什么?
三个ViewHolders
我想我可以从不需要editText字段的视图中删除EditText字段。错误。或至少它适用于三个ViewHolders。
<强> OnFocusChange 强>
使用OnFocusChange而不是addTextChangedListener。直接从这里修改adpater-dataset,瞧!
<强> requestFocusFromTouch 强>
viewHolder.itemAmount.requestFocusFromTouch();触摸时给出editText焦点。
在清单中,添加
android:windowSoftInputMode="adjustPan"
参加您的活动。
在XML-Listview中,添加
android:descendantFocusability="afterDescendants"
有些人认为&#34; beforeDescendants &#34;应该在XML-ListView中使用。我的示例代码中没有任何改变。
adpater看起来像这样:
public class ListViewAdapter extends BaseAdapter {
static class ItemViewHolderEditText {
TextView
itemName,
itemPrize,
itemTotalPrize,
EditText
itemAmount;
}
static class ItemViewHolder {
TextView
itemName,
itemPrize,
itemTotalPrize,
}
static class CatViewHolder {
TextView
categoryName;
}
ArrayList<ListItem> itemsList;
Item item;
public ListViewAdapter(ArrayList<ListItem> itemsList) {
this.itemsList = itemsList;
}
@Override
public int getCount() {
return this.itemsList.size();
}
@Override
public ListItem getItem(int position) {
return this.itemsList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getViewTypeCount() {
return 3;
}
@Override
public int getItemViewType(int position) {
if (getItem(position).isCategory()) return 0;
else {
item = (Item) getItem(position);
if (item.hasEditText()) return 1;
return 2;
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (!getItem(position).isCategory()) {
item = (Item) getItem(position);
if (item.hasEditText()) {
if (row == null) {
row = inflater.inflate(R.layout.item_edittext, parent, false);
viewHolder = new ItemViewHolderEditText();
viewHolder.itemName = (TextView) row.findViewById(R.id.itemName);
viewHolder.itemPrize = (TextView) row.findViewById(R.id.itemPrize);
viewHolder.itemTotalPrize = (TextView) row.findViewById(R.id.itemTotalPrize);
viewHolder.itemAmount = (EditText) row.findViewById(R.id.itemAmount);
row.setTag(viewHolder);
} else viewHolder = (ItemViewHolderEditText) row.getTag();
viewHolder.itemName.setText(item.getName());
viewHolder.itemPrize.setText(String.valeuOf(item.getPrize()));
viewHolder.itemAmount.setId(position);
viewHolder.itemAmount.requestFocusFromTouch();
viewHolder.itemAmount.selectAll();
viewHolder.itemAmount.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus){
final EditText amount = (EditText) v;
Item item = (Item) itemsList.get(position);
item.setTotalPrize(Double.valueOf(amount.getText().toString().trim()));
itemsList.set(v.getId(), item);
}
}
});
viewHolder.itemAmount.setText(volume.get(item.getId)));
viewHodler.itemTotalPrize.setText(String.valueOf(item.getPrize() * volume.get(item.getId));
} else {
if (row == null) {
row = inflater.inflate(R.layout.item, parent, false);
viewHolder = new ItemViewHolder();
viewHolder.itemName = (TextView) row.findViewById(R.id.itemName);
viewHolder.itemPrize = (TextView) row.findViewById(R.id.itemPrize);
viewHolder.itemTotalPrize = (TextView) row.findViewById(R.id.itemTotalPrize);
row.setTag(viewHolder);
} else viewHolder = (ItemViewHolder) row.getTag();
viewHolder.itemName.setText(item.getName());
viewHolder.itemPrize.setText(String.valeuOf(item.getPrize()));
viewHodler.itemTotalPrize.setText(item.getTotalPrize());
}
} else {
Category category = (Category) getItem(position);
if (row == null) {
row = inflater.inflate(R.layout.category, parent, false);
catViewHolder = new CatViewHolder();
catViewHolder.categoryName = (TextView) row.findViewById(R.id.categoryName);
catViewHolder.categoryInfo = (Button) row.findViewById(R.id.categoryInfo);
row.setTag(catViewHolder);
} else catViewHolder = (CatViewHolder) row.getTag();
catViewHolder.categoryName.setText(category.getCatName());
}
return row;
}