我已经获得了基于" ORDER MANAGEMENT"开发的应用程序,我正在开发此应用程序的最基本任务,即"带有ORDER按钮的项目列表,当按钮是单击,出现一个带有项目标题的对话框,要求用户输入要订购的项目数量。"现在我列出了哪些任务已经完成。" 显示项目列表。 每个项目都包含ORDER BUTTON,点击它以显示dilog。它也有效,Dialog使用Edittext询问数量,在确认对话框操作时我也将edittext的值作为字符串获取并将其保存在变量中。 现在我面临的问题是,当我点击列表视图中的倒数第二个和最后一个项目时,它会分别返回第一个和第二个项目的ID,即1和2. ID' s应该是我已分配的内容制作数据主义者。我知道我的适配器有一些问题,但我无法解决它。
这是项目类Item.java
package com.example.learnone;
public class Item {
private String name,partnumber,description;
private double price,cost;
private int id, qtyStart,qtySold,qtyPerPack,qtyordered;
public Item(){}
public Item(int id, double price, String name, int qtyordered){
super();
this.id = id;
this.price = price;
this.name = name;
this.qtyordered = qtyordered;
}
public Item(double price, String name, int qtyordered){
super();
this.price = price;
this.name = name;
this.qtyordered = qtyordered;
}
public Item(String name){
super();
this.name = name;
}
public int getQtyordered() {
return qtyordered;
}
public void setQtyordered(int qtyordered) {
this.qtyordered = qtyordered;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPartnumber() {
return partnumber;
}
public void setPartnumber(String partnumber) {
this.partnumber = partnumber;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public double getCost() {
return cost;
}
public void setCost(double cost) {
this.cost = cost;
}
public int getQtyStart() {
return qtyStart;
}
public void setQtyStart(int qtyStart) {
this.qtyStart = qtyStart;
}
public int getQtySold() {
return qtySold;
}
public void setQtySold(int qtySold) {
this.qtySold = qtySold;
}
public int getQtyPerPack() {
return qtyPerPack;
}
public void setQtyPerPack(int qtyPerPack) {
this.qtyPerPack = qtyPerPack;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
这是AdapterItem.java
public class AdapterItem extends ArrayAdapter<Item> {
Context context;
List<Item> itemList;
int layoutResID;
int[] quantity;
public AdapterItem(Context context, int resource,
List<Item> objects) {
super(context, resource, objects);
this.context = context;
this.itemList = objects;
this.layoutResID = resource;
}
private static class ViewHolder{
TextView viewItemName,viewItemPrice,viewItemQuantity;
Button viewQuantityAdder;
int pos;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
final Item item = getItem(position);
final ViewHolder holder;
if(convertView == null){
holder = new ViewHolder();
final LayoutInflater inflater = ((Activity) context).getLayoutInflater();
view = inflater.inflate(layoutResID, parent, false);
holder.viewItemName = (TextView) view.findViewById(R.id.item_name);
holder.viewItemPrice = (TextView) view.findViewById(R.id.item_price);
holder.viewItemQuantity = (TextView) view.findViewById(R.id.item_quantity);
holder.viewQuantityAdder = (Button) view.findViewById(R.id.item_quantity_adder);
holder.viewQuantityAdder.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//Toast.makeText(getContext(), item.getName().toString(), Toast.LENGTH_SHORT).show();
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle(item.getName().toString());
final View dialogView = View.inflate(getContext(),R.layout.dialog_quantity, null);
builder.setView(dialogView)
.setPositiveButton("TO CART", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
EditText newQuantity = (EditText) dialogView.findViewById(R.id.takeQuantity);
item.setQtyordered(Integer.parseInt(newQuantity.getText().toString()));
Toast.makeText(getContext(), "ID: " + Integer.toString(item.getId()) + " Quantity: " +Integer.parseInt(newQuantity.getText().toString()), Toast.LENGTH_LONG).show();
//Toast.makeText(getContext(), Integer.toString(item.getQtyordered()), Toast.LENGTH_SHORT).show();
//notifyDataSetChanged();
}
})
.setNeutralButton("RESET", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
}
})
.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
});
view.setTag(holder);
}else{
holder = (ViewHolder) view.getTag();
}
holder.viewItemName.setText(item.getName());
holder.viewItemPrice.setText(Integer.toString(item.getId()));
holder.viewItemQuantity.setText("QUANTITY: " + Integer.toString(item.getQtyordered()));
return view;
}
}
这是一个MainActivity.java
public class MainActivity extends Activity {
FragmentManager fragmentManager;
FragmentTransaction fragmentTransaction;
FragmentItemList fragmentItemList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentManager = getFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentItemList = new FragmentItemList();
fragmentTransaction.add(R.id.content_frame, fragmentItemList);
fragmentTransaction.commit();
}
public static class FragmentItemList extends Fragment{
ProgressBar pb;
List<Item> dataList;
public FragmentItemList() {
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
View rootView = inflater.inflate(R.layout.fragment_items_list, container, false);
pb = (ProgressBar) rootView.findViewById(R.id.progressBar1);
pb.setVisibility(ProgressBar.INVISIBLE);
ListView itemListView = (ListView) rootView.findViewById(R.id.itemListView);
/*
List<String> testList = new ArrayList<String>();
testList.add("Sprocket Kit");
testList.add("Engine Bolt Kit");
ArrayAdapter<String> itemsAdapter =
new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, testList);
itemListView.setAdapter(itemsAdapter);
*/
dataList = new ArrayList<Item>();
dataList.add(new Item(1, 40.0, "SPROCKETS BOLTS KIT", 0));
dataList.add(new Item(2, 190.0, "ENGINE BOLTS KIT", 0));
dataList.add(new Item(3, 25.0, "ENGINE AXEL KIT", 0));
dataList.add(new Item(4, 4.0, "LONG CAP SHOW NUT", 0));
dataList.add(new Item(5, 2.5, "CAP NUT", 0));
dataList.add(new Item(6, 0.75, "WELD NUTS M6", 0));
dataList.add(new Item(7, 1.10, "WELD NUTS M8", 0));
AdapterItem itemsAdapter =
new AdapterItem(getActivity(), R.layout.item_row, dataList);
itemListView.setAdapter(itemsAdapter);
return rootView;
}
}
}
我正在寻找好的建议和指导。 感谢。