我的列表,ScrollView和按钮出错

我是Android开发的新手,所以我只想问一下我是怎么想做这些事情的。 我已经制作了一个购物清单应用程序,它将显示数量,项目名称和状态(checkbox)。 这是我的问题:

这是我的错误截图

  1. 列表中的项目或项目名称未显示。
  2. 标题(QUANTITY,ITEM和STATUS)和按钮(添加,删除)正在重复,应该不是。
  3. 最后,我应该在哪里放置按钮代码(additem)以使其达到预期的布局。 它会在GroceryListActivity类还是Grocery类中?

如果我有这么多问题,我很抱歉,如果你能帮忙回答这些问题,我将不胜感激。 谢谢! 🙂

杂货模型类

public class Grocery{ private String quantity; private String item; private boolean selected; //quantity public Grocery(String quantity, String item) { this.quantity = quantity; selected = false; } public String getQuantity() { return quantity; } public void setQuantity(String quantity) { this.quantity = quantity; } //item public Grocery(String item) { this.item = item; selected = false; } public String getItem() { return item; } public void setItem(String item) { this.item = item; } //chckbox public boolean isSelected() { return selected; } public void setSelected(boolean selected) { this.selected = selected; } } 

GroceryList活动

 import java.util.ArrayList; import java.util.List; import android.app.AlertDialog; import android.app.ListActivity; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.Toast; public class GroceryListActivity extends ListActivity implements OnClickListener{ /** Called when the activity is first created. */ public void onCreate(Bundle icicle) { super.onCreate(icicle); // Create an array of Strings, that will be put to our ListActivity ArrayAdapter adapter = new ArrayAdapterGroceryList(this, getGrocery()); setListAdapter(adapter); } private List getGrocery() { List list = new ArrayList(); list.add(get("1", "Soy Sauce")); list.add(get("2", "Cabbage")); list.add(get("3", "Potato")); list.add(get("4", "Bell Pepper")); // Initially select one of the items list.get(1).setSelected(false); return list; } private Grocery get(String q, String i) { return new Grocery(q, i); } public void onCreate1(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.addgrocery); Button addItem_btn = (Button) findViewById(R.id.addItem); addItem_btn.setOnClickListener((OnClickListener) this); Button delete_btn = (Button) findViewById(R.id.deleteItem); delete_btn.setOnClickListener(this); } public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.addItem: Intent a = new Intent(this, AddGrocery.class); startActivity(a); break; case R.id.deleteItem: AlertDialog.Builder Builder = new AlertDialog.Builder(this); Builder.setTitle("Confirm Delete."); Builder.setMessage("Are you sure you want to delete selected items?"); Builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(), "You clicked on YES", Toast.LENGTH_SHORT).show(); } }); Builder.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show(); dialog.cancel(); } }); Builder.show(); break; } } } 

arrays适配器

 import java.util.List; import android.app.Activity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.TextView; public class ArrayAdapterGroceryList extends ArrayAdapter { private final List list; private final Activity context; public ArrayAdapterGroceryList(Activity context, List list) { super(context, R.layout.grocery, list); this.context = context; this.list = list; } static class ViewHolder { protected TextView itemQty; protected TextView itemName; protected CheckBox chkItem; public TextView textQty; public TextView textName; public CheckBox checkbox; } @Override public View getView(int position, View convertView, ViewGroup parent) { View view = null; if (convertView == null) { LayoutInflater inflator = context.getLayoutInflater(); view = inflator.inflate(R.layout.grocery, null); final ViewHolder viewHolder = new ViewHolder(); viewHolder.textQty = (TextView) view.findViewById(R.id.itemQty); viewHolder.textName = (TextView) view.findViewById(R.id.itemName); viewHolder.checkbox = (CheckBox) view.findViewById(R.id.chkItem); viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { Grocery element = (Grocery) viewHolder.checkbox.getTag(); element.setSelected(buttonView.isChecked()); } }); view.setTag(viewHolder); viewHolder.checkbox.setTag(list.get(position)); } else { view = convertView; ((ViewHolder) view.getTag()).checkbox.setTag(list.get(position)); } ViewHolder holder = (ViewHolder) view.getTag(); holder.textQty.setText(list.get(position).getQuantity()); holder.textName.setText(list.get(position).getItem()); holder.checkbox.setChecked(list.get(position).isSelected()); return view; } } 

我的XML文件

                

正如我从你的GroceryListActivity代码中注意到的那样,

 public void onCreate1(Bundle savedInstanceState) { 

 public void onCreate(Bundle icicle) { 

去除,

 public void onCreate1(Bundle savedInstanceState) { } method... 

并将其中的代码放到onCreate()

就像是,

 public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.addgrocery); Button addItem_btn = (Button) findViewById(R.id.addItem); addItem_btn.setOnClickListener((OnClickListener) this); Button delete_btn = (Button) findViewById(R.id.deleteItem); delete_btn.setOnClickListener(this); // Create an array of Strings, that will be put to our ListActivity ArrayAdapter adapter = new ArrayAdapterGroceryList(this, getGrocery()); setListAdapter(adapter); }