私は4つのテキストビューを含むリストビューを持っています。私の見解は以下の通りです
ダイアログ値の助けを借りて、特定のアイテムごとに数量を手動で設定したい私のコードは以下の通りです
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;
}
}
今の問題は、このコードを実行して任意の数量のテキストビューをクリックすると、テキストビューの最後に値が1回だけ設定され、テキストビューをもう一度クリックしてテキストに値を設定しようとすると、テキストボックスにその値を設定できないことです。どうすればこの問題を解決できますか?
コードを編集
あなたの提案に従って、以下のようにコードを変更します
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;
}
}
編集されたコードでは、初めて数量を変更すると正常に動作しますが、他のアイテムの数量を変更すると、他のすべての値が変更され、それがどのように発生するかがわかりますか? また、各行の合計値を計算する方法と、すべてのアイテムの総合計値を取得する方法は?