我有列表视图,其中包括四个文本视图。我的看法如下
我想在对话框值的帮助下根据特定项目手动设置数量我的代码如下
public class OrderFragment extends Fragment {
Spinner mSpinnerMeal;
OrderAdapter mOrderAdpter;
ArrayList<ParserOrder>mArrayList;
ListView mListView;
TextView mTextViewSubmit;
Dialog mDialog;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View mView=inflater.inflate(R.layout.order, container, false);
mSpinnerMeal=(Spinner)mView.findViewById(R.id.order_spn_meal_type);
mArrayList=new ArrayList<ParserOrder>();
StaticValue.noOfFragment=2;
mDialog=new Dialog(getActivity(),android.R.style.Theme_NoTitleBar|android.R.style.Theme_Holo|android.R.style.Theme_Translucent);
mDialog.setCancelable(false);
mListView=(ListView)mView.findViewById(R.id.order_listview);
mTextViewSubmit=(TextView)mView.findViewById(R.id.order_txt_submit);
mTextViewSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
mOrderAdpter=new OrderAdapter();
mListView.setAdapter(mOrderAdpter);
return mView;
}
public class OrderAdapter extends BaseAdapter
{
ArrayList<ParserOrder >mList;
FragmentActivity mActivity;
ViewHolder mViewHolder;
LayoutInflater inflater;
public OrderAdapter() {
// TODO Auto-generated constructor stub
inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return RestaurantApplication.mParserOrder.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@SuppressWarnings("unused")
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
mViewHolder=new ViewHolder();
convertView = inflater.inflate(R.layout.row_order, parent, false);
mViewHolder.mTextViewItenam=(TextView)convertView.findViewById(R.id.row_order_iteamname);
mViewHolder.mTextViewPrice=(TextView)convertView.findViewById(R.id.row_order_price);
mViewHolder.mTextViewTotal=(TextView)convertView.findViewById(R.id.row_order_txt_total);
mViewHolder.mTextViewQty=(TextView)convertView.findViewById(R.id.row_order_edt_qty);
mViewHolder.mTextViewItenam.setText(RestaurantApplication.mParserOrder.get(position).getIteamname());
mViewHolder.mTextViewPrice.setText(RestaurantApplication.mParserOrder.get(position).getIteamprice());
mViewHolder.mTextViewQty.setId(position);
mViewHolder.mTextViewQty.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mDialog.setContentView(R.layout.dialog_qty);
final EditText mEditTextQty=(EditText)mDialog.findViewById(R.id.dialog_edt);
TextView mTextViewOk=(TextView)mDialog.findViewById(R.id.dialog_txt_ok);
TextView mTextViewCancel=(TextView)mDialog.findViewById(R.id.dialog_txt_cancle);
mTextViewOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mViewHolder.mTextViewQty.setText(mEditTextQty.getText().toString().trim());
mDialog.dismiss();
}
});
mTextViewCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mDialog.dismiss();
}
});
mDialog.show();
}
});
mViewHolder.mTextViewQty.setTag(position);
if (convertView==null) {
mViewHolder.mTextViewQty.getTag();
}
return convertView;
}
}
public class ViewHolder
{
TextView mTextViewItenam;
TextView mTextViewPrice;
TextView mTextViewQty;
TextView mTextViewTotal;
}
}
现在的问题是当我运行此代码并单击任何数量的文本视图时,它只会在文本视图的最后一次设置值,当我再次单击任何文本视图并尝试在文本中设置值时,它不允许在任何文本框中设置该值想法我该如何解决这个问题?
编辑代码
根据您的建议,我将代码更改如下
public class OrderFragment extends Fragment {
Spinner mSpinnerMeal;
OrderAdapter mOrderAdpter;
ArrayList<ParserOrder>mArrayList;
ListView mListView;
TextView mTextViewSubmit;
Dialog mDialog;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View mView=inflater.inflate(R.layout.order, container, false);
mSpinnerMeal=(Spinner)mView.findViewById(R.id.order_spn_meal_type);
mArrayList=new ArrayList<ParserOrder>();
StaticValue.noOfFragment=2;
mDialog=new Dialog(getActivity(),android.R.style.Theme_NoTitleBar|android.R.style.Theme_Holo|android.R.style.Theme_Translucent);
mDialog.setCancelable(false);
mListView=(ListView)mView.findViewById(R.id.order_listview);
mTextViewSubmit=(TextView)mView.findViewById(R.id.order_txt_submit);
mTextViewSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
mOrderAdpter=new OrderAdapter();
mListView.setAdapter(mOrderAdpter);
return mView;
}
public class OrderAdapter extends BaseAdapter
{
ArrayList<ParserOrder >mList;
FragmentActivity mActivity;
ViewHolder mViewHolder;
LayoutInflater inflater;
public OrderAdapter() {
// TODO Auto-generated constructor stub
inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return RestaurantApplication.mParserOrder.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@SuppressWarnings("unused")
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
mViewHolder=new ViewHolder();
convertView = inflater.inflate(R.layout.row_order, parent, false);
mViewHolder.mTextViewItenam=(TextView)convertView.findViewById(R.id.row_order_iteamname);
mViewHolder.mTextViewPrice=(TextView)convertView.findViewById(R.id.row_order_price);
mViewHolder.mTextViewTotal=(TextView)convertView.findViewById(R.id.row_order_txt_total);
mViewHolder.mTextViewQty=(TextView)convertView.findViewById(R.id.row_order_edt_qty);
mViewHolder.mTextViewItenam.setText(RestaurantApplication.mParserOrder.get(position).getIteamname());
mViewHolder.mTextViewPrice.setText(RestaurantApplication.mParserOrder.get(position).getIteamprice());
mViewHolder.mTextViewQty.setId(position);
mViewHolder.mTextViewQty.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mDialog.setContentView(R.layout.dialog_qty);
final EditText mEditTextQty=(EditText)mDialog.findViewById(R.id.dialog_edt);
final TextView mTextViewOk=(TextView)mDialog.findViewById(R.id.dialog_txt_ok);
mTextViewOk.setTag(position);
TextView mTextViewCancel=(TextView)mDialog.findViewById(R.id.dialog_txt_cancle);
mTextViewOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos=Integer.parseInt(mTextViewOk.getTag().toString());
System.out.println("Position "+pos);
mOrder.setIt_id(RestaurantApplication.mParserOrder.get(position).getIt_id());
mOrder.setCid(RestaurantApplication.mParserOrder.get(position).getCid());
mOrder.setIteamname(RestaurantApplication.mParserOrder.get(position).getIteamname());
mOrder.setIteamprice(RestaurantApplication.mParserOrder.get(position).getIteamprice());
mOrder.setSid(RestaurantApplication.mParserOrder.get(position).getSid());
mOrder.setQty(mEditTextQty.getText().toString().trim());
RestaurantApplication.mParserOrder.remove(position);
RestaurantApplication.mParserOrder.add(position,mOrder);
mViewHolder.mTextViewQty.setText(RestaurantApplication.mParserOrder.get(position).getQty());
mDialog.dismiss();
mOrderAdpter.notifyDataSetChanged();
}
});
mTextViewCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mDialog.dismiss();
}
});
mDialog.show();
}
});
mViewHolder.mTextViewQty.setTag(position);
if (convertView==null) {
mViewHolder.mTextViewQty.getTag();
}
return convertView;
}
}
public class ViewHolder
{
TextView mTextViewItenam;
TextView mTextViewPrice;
TextView mTextViewQty;
TextView mTextViewTotal;
}
}
在编辑的代码中,当我第一次更改数量时,我会正常工作,但是当我更改任何其他项目数量时,它会更改所有其他值,它知道它是如何发生的吗?以及如何计算每一行的总值以及如何获得所有项目的总值?