Android 我的列表、滚动视图和按钮出错
我是Android开发的新手,所以我只想问一下我应该如何正确地做这些事情。我已经做了一个杂货清单应用程序,将显示数量,项目名称和状态(复选框)。以下是我的问题:Android 我的列表、滚动视图和按钮出错,android,listview,button,android-activity,scrollview,Android,Listview,Button,Android Activity,Scrollview,我是Android开发的新手,所以我只想问一下我应该如何正确地做这些事情。我已经做了一个杂货清单应用程序,将显示数量,项目名称和状态(复选框)。以下是我的问题: 列表上的项目或项目名称未显示 标题(数量、项目和状态)和按钮(添加、删除)正在重复,本不应重复 最后,我应该把按钮(additem)的代码放在哪里,以便它进入预期的布局。是在杂货店的活动课还是在杂货店的活动课 如果我有这么多问题,我很抱歉,如果您能帮助回答这些问题,我将不胜感激。谢谢!:) 杂货模型类 public class Gro
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.ArrayList;
导入java.util.List;
导入android.app.AlertDialog;
导入android.app.ListActivity;
导入android.content.DialogInterface;
导入android.content.Intent;
导入android.os.Bundle;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.ArrayAdapter;
导入android.widget.Button;
导入android.widget.Toast;
公共类GroceryStativity扩展ListActivity实现OnClickListener{
/**在首次创建活动时调用*/
创建公共空间(捆绑冰柱){
超级冰柱;
//创建一个字符串数组,将其放入我们的ListActivity
ArrayAdapter adapter=new ArrayAdapterGroceryList(this,getGrocery());
setListAdapter(适配器);
}
私人名单{
列表=新的ArrayList();
列表。添加(获取(“1”、“酱油”);
添加(获取(“2”、“卷心菜”);
添加(获取(“3”、“土豆”);
添加(获取(“4”、“甜椒”);
//首先选择其中一项
list.get(1).setSelected(false);
退货清单;
}
私人杂货店获取(字符串q、字符串i){
返回新杂货店(q,i);
}
创建1时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.add);
按钮addItem\u btn=(按钮)findViewById(R.id.addItem);
addItem_btn.setOnClickListener((OnClickListener)this);
按钮delete_btn=(按钮)findViewById(R.id.deleteItem);
删除_btn.setOnClickListener(此);
}
公共void onClick(视图v){
//TODO自动生成的方法存根
开关(v.getId()){
案例R.id.addItem:
意图a=新意图(这个,addgrovery.class);
星触觉(a);
打破
案例R.id.deleteItem:
AlertDialog.Builder=新建AlertDialog.Builder(此);
Builder.setTitle(“确认删除”);
setMessage(“您确定要删除所选项目吗?”);
Builder.setPositiveButton(“是”,新建DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int which){
Toast.makeText(getApplicationContext(),“您单击了YES”,Toast.LENGTH\u SHORT.show();
}
});
setNegativeButton(“否”,新的DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int which){
Toast.makeText(getApplicationContext(),“您没有点击”,Toast.LENGTH\u SHORT.show();
dialog.cancel();
}
});
Builder.show();
打破
}
}
}
阵列适配器
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;
}
}
import java.util.List;
导入android.app.Activity;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.ArrayAdapter;
导入android.widget.CheckBox;
导入android.widget.CompoundButton;
导入android.widget.TextView;
公共类ArrayAdapterGroceryList扩展了ArrayAdapter{
私人最终名单;
私人最终活动背景;
公共ArrayAdapterGroceryList(活动上下文,列表){
超级(上下文、右布局、杂货店、列表);
this.context=上下文;
this.list=列表;
}
静态类视窗夹{
受保护的文本视图项目数量;
受保护的TextView项目名称;
受保护的chkItem;
公共文本查看文本数量;
公共文本视图文本名称;
公共复选框;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图=空;
if(convertView==null){
LayoutInflater充气器=上下文。getLayoutInflater();
视图=充气机。充气(R.layout.grounder,null);
最终ViewHolder ViewHolder=新的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(新建CompoundButton.OnCheckedChangeListener()){
检查更改后的公共无效(复合按钮视图,布尔值已检查){
杂货店元素=(杂货店)viewHolder.checkbox.getTag();
element.setSelected(buttonView.isChecked());
}
});
view.setTag(viewHolder);
viewHolder.checkbox.setTag(list.get(position));
}否则{
视图=转换视图;
((ViewHolder)view.getTag()).checkbox.setTag(list.get(position));
}
ViewHolder=(ViewHolder)view.getTag();
holder.textQty.setText(list.get(position.getQuantity());
holder.textName.setText(list.get(position.getItem());
holder.checkbox.setChecked(list.get(position.isSelected());
返回视图;
}
}
我的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>
正如我从您的杂货店活动代码中注意到的那样
public void onCreate1(Bundle savedInstanceState) {
及
移除
public void onCreate1(Bundle savedInstanceState) { } method...
并将其中的代码放入onCreate()
public void onCreate1(Bundle savedInstanceState) { } method...
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);
}