我的列表、滚动视图和按钮出现了错误

5

我刚接触Android开发,想问一下如何正确地解决以下问题。我制作了一个杂货清单应用程序,可以显示数量、物品名称和状态(复选框)。以下是我的问题:

这是我的错误截图

  1. 清单上的物品或物品名称没有显示出来。
  2. 标题(QUANTITY,ITEM和STATUS)和按钮(添加,删除)重复了,不应该这样。
  3. 最后,我应该在哪里放置按钮(additem)的代码才能进入其预定布局。它应该放在GroceryListActivity类还是Grocery类中?

如果我有很多问题,对不起,如果您能回答这些问题,我将不胜感激。谢谢!:)

Grocery Model Class

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;
          }

}

购物清单活动

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<Grocery> adapter = new ArrayAdapterGroceryList(this, getGrocery());
    setListAdapter(adapter);
  }

  private List<Grocery> getGrocery() {
    List<Grocery> list = new ArrayList<Grocery>();
        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;
        }
    }

} 

数组适配器

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<Grocery> {

private final List<Grocery> list;
private final Activity context;

public ArrayAdapterGroceryList(Activity context, List<Grocery> 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文件

<?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:background="@layout/bg_list"
android:orientation="vertical"
android:weightSum="1.0">


<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:weightSum="3"
    android:gravity="center"
    android:background="@layout/bg_box">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="QTY"
            android:textColor="#ffffff"
            android:textSize="20dp"
            android:textStyle="bold"
            android:layout_weight="1" />

        <TextView
            android:id="@+id/serialNumberView"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
             android:layout_weight="1" 
             android:text="ITEM"
             android:textStyle="bold"
             android:textColor="#ffffff"
            android:textSize="20dp"/>

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="STATUS"
            android:textStyle="bold"
            android:textColor="#ffffff"
            android:textSize="20dp"
            android:layout_weight="1" />
        </LinearLayout>

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1" >

   <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:orientation="horizontal"
        android:layout_weight="1"
        android:paddingTop="5dp"
        android:gravity="center_horizontal">

      <TextView android:id="@+id/itemQty"
         android:layout_width="30dp"
         android:layout_height="fill_parent"
         android:paddingLeft="20dp"
         android:textColor="#FFFFFF"
         android:textSize="15dp"/>

     <TextView android:id="@+id/itemName"
         android:layout_width="200dp"
         android:layout_height="fill_parent"
         android:textColor="#FFFFFF"
         android:textSize="15dp"/>

     <CheckBox 
        android:id="@+id/chkItem"
        android:layout_width="50dp"
        android:layout_height="fill_parent"/>

   </LinearLayout>

</ScrollView>



 <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_gravity="bottom">

    <Button
        android:id="@+id/addItem"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:text="Add"
        android:layout_weight="1" />

    <Button
        android:id="@+id/deleteItem"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:text="Delete"
        android:layout_weight="1" />
</LinearLayout> 


我不理解你的要求,你想要一个编辑文本框、列表视图、滚动视图和按钮? - shassss
请检查此链接:http://www.vogella.com/articles/AndroidListView/article.html - shassss
谢谢您的回复。关于您发布的链接,我想要有三列,分别包含数量、项目和复选框。底部将有添加和删除按钮。唯一可以滚动的部分将是文本视图。 - secre.swallowtail
嘿,我刚刚发布了一张图片,这样你就能明白我的意思了。 :) - secre.swallowtail
实际上,你走错了...没有必要使用GridView..还有所有这些东西..根据我看到的View Image..简单地使用自定义适配器创建一个ListView,制作其中需要的视图的列表项。然后将arraylist List<Grocery>传递给该适配器..简单吧..只需查看一些关于CustomListView的教程,你会发现它很容易实现,而不是你正在做的复杂操作。 - user370305
1个回答

4

根据您的代码中的GroceryListActivity,我注意到:

public void onCreate1(Bundle savedInstanceState) {

并且

public void onCreate(Bundle icicle) {

removed,

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<Grocery> adapter = new ArrayAdapterGroceryList(this, getGrocery());
    setListAdapter(adapter);
  }

09-03 15:33:50.567: E/AndroidRuntime(1143): java.lang.RuntimeException: 无法启动组件活动 ComponentInfo{com.capstone.pinoygoodies/com.capstone.pinoygoodies.GroceryListActivity}: java.lang.RuntimeException: 您的内容必须具有 id 属性为 'android.R.id.list' 的 ListView。 - secre.swallowtail
我认为它没有获取ArrayList List<Grocery>?我刚刚发布了一张图片。 :) - secre.swallowtail
在ListView中添加复选框和这个一样吗? - secre.swallowtail
<LinearLayout> <ScrollView> <ListView> <TextView> <TextView> <Checkbox> </ListView> </ScrollView> </LinearLayout> 这样对吗? - secre.swallowtail
不,这不对...你需要先看一些关于自定义ListView的教程...你走错了... - user370305

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接