我正在使用自定义listview适配器来捕获相同域类Food的多个对象。这堂课是:
public class Food {
public String Item;
public String Description;
public int count;
}
我的自定义适配器是:
public class FoodAdapter extends ArrayAdapter<Food> {
private final Context context;
private final ArrayList<Food> itemsArrayList;
EditText item
EditText desc
EditText count;
public ArrayList<Food> getItemsArrayList() {
return itemsArrayList;
}
public FoodAdapter(Context context, ArrayList<Food> itemsArrayList) {
super(context, R.layout.fr_row, itemsArrayList);
//super(context, itemsArrayList);
this.context = context;
this.itemsArrayList = itemsArrayList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// 1. Create inflater
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// 2. Get rowView from inflater
View rowView = inflater.inflate(R.layout.fr_row, parent, false);
// 3. Get the two text view from the rowView
item= (EditText) rowView.findViewById(R.id.f_item);
desc= (EditText) rowView.findViewById(R.id.f_desc);
count = (EditText) rowView.findViewById(R.id.f_count);
return rowView;
}
}
我的ActivityThat协调这个listview是:
package com.example.foodie;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.ListView;
public class FoodActivity extends AppCompatActivity {
ListView lv;
FoodAdapter adapter;
ArrayList<Food> fList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_frmorning);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
setContentView(R.layout.activity_frmorning);
lv = (ListView) findViewById(R.id.list);
fList = new ArrayList<Food>();
adapter =new FoodAdapter(this, fList);
lv.setAdapter(adapter);
}
public void addItems(View v) {
fList.add(new Food());
adapter.notifyDataSetChanged();
}
}
此addItems是布局上的一个按钮(在FoodActivity中)。当我点击添加时,我得到了新行。但是,添加新行后,行会丢失所有内容。他们都是空白的。是什么导致了这种行为以及如何解决?另外,如何将EditText字段绑定到ArrayList中的数据,以便当我调用getItemsArrayList()
时,我得到与屏幕显示对应的正确ArrayList?
答案 0 :(得分:0)
当你不使用Holder时,getView()方法会多次调用findViewById(),因为你的行将不在View中。因此,如果List中有1000行,而990行将不在View中,则990次将再次被称为findViewById()。
Holder设计模式用于View缓存 - Holder(任意)对象保存每行的子窗口小部件,当行超出View时,将不会调用findViewById()但View将被回收并且将从中获取窗口小部件支架
public class FoodAdapter extends ArrayAdapter<Food>
{
private final Context context;
private final ArrayList<Food> itemsArrayList;
public FoodAdapter(Context context, ArrayList<Food> itemsArrayList) {
{
super(context, itemsArrayList);
this.context = context;
this.itemsArrayList = itemsArrayList;
}
@Override
public int getCount()
{
return itemsArrayList.size();
}
@Override
public Object getItem(int position)
{
return itemsArrayList.get(position);
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null)
{
holder = new ViewHolder();
LayoutInflater inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.fr_row, parent, false);
holder.text = (EditText ) convertView.findViewById(R.id.edittext);
convertView.setTag(holder);
} else
{
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(itemsArrayList.get(position));
return convertView;
}
class ViewHolder
{
EditText item
EditText desc
EditText count;
}
}`enter code here`