我正在开发一个具有自定义布局项目列表的应用程序。我已经使用它需要的所有方法实现了Product类,我已经开始为我的ArrayList编写CustomAdapter类。每行包含4个EditText框,2个inputType =“number”和2个常规框,以及2个ImageView。其他功能(图像onclick,从列表中删除项目等工作)然而当我专注于EditText时,适配器会自动刷新(或者我认为)列表元素,我不能在文本框中写任何东西。奇怪的是,数字框工作,他们完成相同的工作。我在某处看到ListViews中的EditTexts存在问题,但我发现很难相信我无法在我的应用程序中实现这样的功能。
这是我的自定义适配器:
public class CustomAdapter extends BaseAdapter implements OnClickListener {
/*********** Declare Used Variables *********/
private Activity activity;
private ArrayList data;
private static LayoutInflater inflater=null;
public Resources res;
Product tempValues=null;
int i=0;
String TAG = "CustomAdapter";
public int currentItem = 0;
/************* CustomAdapter Constructor *****************/
public CustomAdapter(Activity a, ArrayList d,Resources resLocal) {
/********** Take passed values **********/
activity = a;
data=d;
res = resLocal;
/*********** Layout inflator to call external xml layout () ***********/
inflater = ( LayoutInflater )activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
/******** What is the size of Passed Arraylist Size ************/
public int getCount() {
if(data.size()<=0)
return 1;
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
/********* Create a holder Class to contain inflated xml file elements *********/
public static class ViewHolder{
public EditText productET;
public EditText priceET;
public EditText obsET;
public EditText quantET;
public ImageView checked;
public ImageView unchecked;
public ImageView delete;
}
/****** Depends upon data size called for each row , Create each ListView row *****/
public View getView(final int position, View convertView, ViewGroup parent) {
View vi = convertView;
final ViewHolder holder;
if(convertView==null){
/****** Inflate tabitem.xml file for each row ( Defined below ) *******/
vi = inflater.inflate(R.layout.row3, null);
/****** View Holder Object to contain tabitem.xml file elements ******/
holder = new ViewHolder();
holder.productET = (EditText) vi.findViewById(R.id.product);
holder.priceET = (EditText) vi.findViewById(R.id.price);
holder.obsET = (EditText) vi.findViewById(R.id.obs);
holder.quantET = (EditText) vi.findViewById(R.id.quantity);
holder.checked = (ImageView) vi.findViewById(R.id.checked);
holder.unchecked = (ImageView) vi.findViewById(R.id.unchecked);
holder.delete = (ImageView) vi.findViewById(R.id.delete);
/************ Set holder with LayoutInflater ************/
vi.setTag( holder );
}
else {
holder=(ViewHolder)vi.getTag();
}
if(data.size()<=0)
{
}
else
{
/***** Get each Model object from Arraylist ********/
tempValues=null;
tempValues = ( Product ) data.get( position );
/************ Set Model values in Holder elements ***********/
holder.productET.setText( tempValues.getName() );
holder.priceET.setText( String.valueOf(tempValues.getPrice()) );
holder.obsET.setText( tempValues.getInfo() );
holder.quantET.setText( String.valueOf(tempValues.getQuntity()) );
holder.checked.setImageResource(res.getIdentifier("com.mit.shoppinglist:drawable/checked",null,null));
holder.unchecked.setImageResource(res.getIdentifier("com.mit.shoppinglist:drawable/unchecked",null,null));
holder.delete.setImageResource(res.getIdentifier("com.mit.shoppinglist:drawable/delete",null,null));
/**************/
if(tempValues.getStatus()){
holder.checked.setVisibility(View.VISIBLE);
holder.unchecked.setVisibility(View.INVISIBLE);
}
else {
holder.checked.setVisibility(View.INVISIBLE);
holder.unchecked.setVisibility(View.VISIBLE);
}
/******** Set Item Click Listner for LayoutInflater for each row *******/
holder.checked.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
holder.checked.setVisibility(View.INVISIBLE);
holder.unchecked.setVisibility(View.VISIBLE);
MainActivity sct = (MainActivity)activity;
sct.changeStatus(position, false);
}
});
holder.unchecked.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
holder.checked.setVisibility(View.VISIBLE);
holder.unchecked.setVisibility(View.INVISIBLE);
MainActivity sct = (MainActivity)activity;
sct.changeStatus(position, true);
}
});
holder.delete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
MainActivity sct = (MainActivity)activity;
sct.removeItem(position);
}
});
vi.setOnClickListener(new OnItemClickListener( position ));
}
return vi;
}
@Override
public void onClick(View v) {
Log.v(TAG, "=====Row button clicked=====");
}
/********* Called when Item click in ListView ************/
private class OnItemClickListener implements OnClickListener{
private int mPosition;
OnItemClickListener(int position){
mPosition = position;
currentItem = position;
}
@Override
public void onClick(View arg0) {
MainActivity sct = (MainActivity)activity;
/**** Call onItemClick Method inside CustomListViewAndroidExample Class ( See Below )****/
sct.onItemClick(mPosition);
}
}
}
和我的MainClass
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main)
CustomListView = this;
lv = (ListView) findViewById(R.id.productList);
Resources res = getResources();
adapter=new CustomAdapter( CustomListView, al,res );
lv.setAdapter( adapter );
produs = new Product("bread", "just white bread", 0, 2, false);
al.add(produs);
produs = new Product("weat bread", "can be white", 0, 5, true);
al.add(produs);
produs = new Product("potatoes", "fresh", 0, 2, true);
al.add(produs);
adapter.notifyDataSetChanged();
public void removeItem(int poz) {
al.remove(poz);
adapter.notifyDataSetChanged();
}
public void changeStatus(int poz, Boolean status) {
al.get(poz).setStatus(status);
adapter.notifyDataSetChanged();
}
}