我使用BaseAdapter将listview充实了TextView,editText和Button的列表。当我使用ArrayList填充此列表视图时,textView具有正确的字符串但是当我在EditText上放入条目时,相同的值会在备用的第6行重复。我有onclick事件监听器在对象和EditView中显示相应的值,但它显示随机对象。
public void RemoveItemFromOrder(View v){
MenuItemsAdapter.ViewHolder holder = (MenuItemsAdapter.ViewHolder)v.getTag();
int quantity = Integer.parseInt(holder.getEditNumber().getText().toString());
//Toast.makeText(getApplicationContext(), item.getItem(),Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), holder.getMenus().getItem()+" "+quantity, Toast.LENGTH_SHORT).show();
}
menulist
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/txtMenu"
android:layout_width="0sp"
android:layout_height="fill_parent"
android:layout_weight="3"
android:gravity="center_vertical"
android:text="Chaunim momo item"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="@+id/editQty"
android:saveEnabled="false"
android:layout_width="0sp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:inputType="numberDecimal"
android:hint="Qty"
android:textAppearance="?android:attr/textAppearanceSmall" />
<ImageButton
android:id="@+id/butDel"
android:layout_width="@dimen/width_button"
android:layout_height="fill_parent"
android:contentDescription="Delete"
android:onClick="RemoveItemFromOrder"
android:src="@android:drawable/ic_menu_delete" />
主要活动布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.sandip.gre.meromenu.Menu">
<include
android:id="@+id/app_bar"
layout="@layout/menutoolbar" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="30dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Table: "
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="30dp" />
<Spinner
android:id="@+id/spinnerTable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/dummylayout"
android:textAlignment="center"
android:textSize="30dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/listOrders"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/dummylayout"
android:paddingLeft="20dp"
/>
<ListView
android:id="@+id/listMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/app_bar"
android:divider="#f6b2b2"
android:dividerHeight="1dp"
android:paddingStart="10dp"
android:paddingLeft="10dp"
android:paddingRight="2dp"
android:paddingEnd="2dp"/>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="@id/listMenu"
android:layout_marginTop="50dp"
android:text="Place Order" />
</LinearLayout>
</LinearLayout>
适配器类:
package com.example.sandip.gre.meromenu.database;
import android.app.Activity;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import com.example.sandip.gre.meromenu.R;
import java.util.ArrayList;
public class MenuItemsAdapter extends BaseAdapter{
private Context context;
private ArrayList<Menus> menuItems;
public MenuItemsAdapter(Context context, ArrayList<Menus> menuItems){
this.context = context;
this.menuItems = menuItems; }
public class ViewHolder{
Menus menus;
TextView txtOrder;
ImageButton butCancel;
EditText editNumber;
public TextView getTxtOrder() {
return txtOrder;
}
public Menus getMenus() {
return menus;
}
public EditText getEditNumber() {
return editNumber;
}
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return menuItems.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return menuItems.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder = null;
LayoutInflater mInflater = ((Activity) context).getLayoutInflater();
if (convertView == null) {
convertView = mInflater.inflate(R.layout.menulist, parent, false);
holder = new ViewHolder();
TextView txtOrder;
Button butCancel;
EditText editNumber;
holder.menus = menuItems.get(position);
holder.txtOrder = (TextView) convertView.findViewById(R.id.txtMenu);
holder.butCancel = (ImageButton) convertView.findViewById(R.id.butDel);
//holder.butCancel.setTag(holder.menus);
holder.butCancel.setTag(holder);
holder.editNumber = (EditText) convertView.findViewById(R.id.editQty);
setValueTextListeners(holder);
convertView.setTag(holder);
}
else{
holder = (ViewHolder) convertView.getTag();
}
holder.txtOrder.setText(menuItems.get(position).getItem());
return convertView;
}
//if we have to instantly change data during data change in editText use
private void setValueTextListeners(final ViewHolder holder) {
holder.editNumber.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
try {
//holder.menus.setValue(Double.parseDouble(s.toString()));
} catch (NumberFormatException e) {
Log.e("Error", "error reading double value: " + s.toString());
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
}
答案 0 :(得分:0)
我也遇到了这个问题,问题可能在你的基础适配器类中,使用下面的适配器,
package com.example.sandip.gre.meromenu.database;
import android.app.Activity;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import com.example.sandip.gre.meromenu.R;
import java.util.ArrayList;
public class MenuItemsAdapter extends BaseAdapter{
private Context context;
private ArrayList<Menus> menuItems;
public MenuItemsAdapter(Context context, ArrayList<Menus> menuItems){
this.context = context;
this.menuItems = menuItems;
mInflater = ((Activity) context).getLayoutInflater();
}
public class ViewHolder{
Menus menus;
TextView txtOrder;
ImageButton butCancel;
EditText editNumber;
public TextView getTxtOrder() {
return txtOrder;
}
public Menus getMenus() {
return menus;
}
public EditText getEditNumber() {
return editNumber;
}
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return menuItems.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return menuItems.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
LayoutInflater mInflater;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder = null;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.menulist, parent, false);
}
holder = new ViewHolder();
TextView txtOrder;
Button butCancel;
EditText editNumber;
holder.menus = menuItems.get(position);
holder.txtOrder = (TextView) convertView.findViewById(R.id.txtMenu);
holder.butCancel = (ImageButton) convertView.findViewById(R.id.butDel);
//holder.butCancel.setTag(holder.menus);
holder.butCancel.setTag(holder);
holder.editNumber = (EditText) convertView.findViewById(R.id.editQty);
setValueTextListeners(holder);
holder.txtOrder.setText(menuItems.get(position).getItem());
return convertView;
}
//if we have to instantly change data during data change in editText use
private void setValueTextListeners(final ViewHolder holder) {
holder.editNumber.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
try {
//holder.menus.setValue(Double.parseDouble(s.toString()));
} catch (NumberFormatException e) {
Log.e("Error", "error reading double value: " + s.toString());
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
}
还有一件事,我认为你不需要ViewHolder ......!