Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error with my List, ScrollView and buttons

I'm new to Android development so I just want to ask about how am I suppose to do these things right. I have made a grocery list app which will show the quantity, item name and status (checkbox). Here are my problems:

Here is my error screenshot

  1. The items or item names on the list is not showing up.
  2. The header (QUANTITY, ITEM and STATUS) and buttons (add, delete) are repeating which supposed to be not.
  3. And lastly, where am I supposed to put the codes of button(additem) for it to go to its intended layout. Is it going to be in GroceryListActivity class or Grocery class?

I'm sorry if I have so many questions and I'll be grateful if you could help answer these questions. Thank you! :)

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

}

GroceryList Activity

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

} 

Array Adapter

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

my XML file

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

like image 416
secre.swallowtail Avatar asked Oct 06 '22 18:10

secre.swallowtail


1 Answers

As I noticed from your GroceryListActivity code,

public void onCreate1(Bundle savedInstanceState) {

and

public void onCreate(Bundle icicle) {

removed,

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

and put the code from it to onCreate()

Something like,

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);
  }
like image 152
user370305 Avatar answered Oct 13 '22 02:10

user370305