当前位置: 首页 > 知识库问答 >
问题:

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

朱华皓
2023-03-14

我是Android开发新手,所以我只想问一下,我该如何把这些事情做好。我已经做了一个杂货清单应用程序,将显示数量,项目名称和状态(复选框)。以下是我的问题:

  1. 列表上的项目或项目名称未显示

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

杂货模型类

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

共有1个答案

罗智志
2023-03-14

正如我从您的GroceryListactive代码中注意到的那样,

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<Grocery> adapter = new ArrayAdapterGroceryList(this, getGrocery());
    setListAdapter(adapter);
  }
 类似资料:
  • 我有一个android布局,它有一个,里面有很多元素。在的底部有一个,然后由适配器填充。 我遇到的问题是,android将从中排除,因为已经具有可滚动的功能。我希望与内容一样长,并且主滚动视图是可滚动的。 我怎样才能达到这种行为呢? 下面是我的主要布局: 然后以编程方式将组件添加到具有ID:的linearlayour中。下面是加载到LinearLayout中的一个视图。就是这个给我卷轴带来麻烦的。

  • 问题内容: 在此之前,我的滚动视图下面几乎有一个值得滚动的页面,下面有一个列表视图,但是一旦填充了列表视图,滚动视图就会移到列表视图的顶部。我该如何解决/防止这种情况发生? 滚动查看XML: 我试着做sv.fullScroll(ScrollView.FOCUS_UP); 和所有这些东西到我的scrollview,但它不起作用 问题答案: 首先,您可能已经听说过它,但以防万一: 切勿在内部放一个,因

  • 我试图实现某种排序工具栏-但没有工具栏(而不是工具栏,它将是一个下拉元素,它本质上是一个RelativeLayout,其正下方有LinearLayout(下拉列表中的扩展项目),一旦按下下拉菜单,它就会在整个布局上悬停。 我考虑过实现折叠工具栏并将下拉列表放入工具栏中,但这可能不是一个好主意,因为下拉列表不是像图像视图这样的静态组件。我还在我的应用程序中使用了ActionBar,因此迁移到工具栏很

  • 公共视图getView(最终int位置,视图转换视图,ViewGroup父){ 这是我的XML文件代码, 我无法取消选中这个单选按钮。我已经发布了我的Java代码和xml代码。感谢提前帮助。

  • 我正在使用< code > Android . support . design . widget 包中的< code > floating action button : 是否可以将该按钮配置为在列表视图向下滚动时使用动画隐藏,并在列表视图向上滚动到顶部时再次显示它?

  • 当发生滚动时,我试图将< code>ListView项目动画化。更具体地说,我正试图模仿iOS 7上iMessage应用程序的滚动动画。我在网上找到一个类似的例子: 为了澄清,我试图在用户滚动时实现对项目的“流体”移动效果,而不是在添加新项目时实现动画。我试图修改我的中的视图,并且我已经研究了源代码,看看我是否可以以某种方式在某个地方附加一个,该器可以调整发送到子视图的绘制坐标(如果这是的设计方式