Android 当选(假); 数据.添加(d); } 公共ArrayList getData(){ 返回数据; } public int getCount(){//获取listview中的元素数 返回data.size(); } 公共对象getItem(int-position){//此方法按位置返回对象 返回位置; } 公共长getItemId(int位置){//按位置获取项目id 返回位置; } 公共视图getView(){ 返回vi; } public View getView(int-position,View-convertView,ViewGroup-parent){//getView方法是用我们的个性化行填充列表视图的方法 vi=转换视图; 最终视图持有者=新的视图持有者(); if(convertView==null){ vi=充气机。充气(R.layout.custom_row_view,空); //listview中的每个项都使用xml“listview\u行”的设计 holder.name=(复选框)vi.findViewById(R.id.name); holder.price=(TextView)vi.findViewById(R.id.price);//您可以输入任何想要的内容,按钮、单选按钮、图像等。 holder.quantity=(TextView)vi.findViewById(R.id.quantity); 持有人姓名 .setOnCheckedChangeListener(新建CompoundButton.OnCheckedChangeListener(){ @凌驾 检查更改后的公共无效(复合按钮视图, 布尔值(已检查){ DataModel元素=(DataModel)holder.name .getTag(); element.setSelected(buttonView.isChecked()); } }); vi.setTag(支架); holder.name.setTag(data.get(position)); ViewHolder vholder=(ViewHolder)vi.getTag(); vholder.name.setChecked(data.get(position.isSelected()); HashMap hash=new HashMap();//我们需要一个HashMap来存储任何项目的数据 hash=data.get(position.getData(); vholder.name.setText(hash.get(“品牌”)+“”+hash.get(“名称”);//我们对行中的项目进行个性化设置。 vholder.price.setText(“$”+hash.get(“price”); vholder.quantity.setText(“数量:+hash.get(“数量”)); }否则{ vi=转换视图; ((ViewHolder)vi.getTag()).name.setTag(data.get(position)); } if(holder.name==null){ ViewHolder vholder=(ViewHolder)vi.getTag(); vholder.name.setChecked(data.get(position.isSelected()); HashMap hash=new HashMap();//我们需要一个HashMap来存储任何项目的数据 hash=data.get(position.getData(); vholder.name.setText(hash.get(“品牌”)+“”+hash.get(“名称”);//我们对行中的项目进行个性化设置。 vholder.price.setText(“$”+hash.get(“price”); vholder.quantity.setText(“数量:+hash.get(“数量”)); } 返回vi; } } 类视图持有者{ 复选框名称; 文本视图价格; 文本视图数量; 公共复选框getName(){ 返回名称; } public void setName(复选框名称){ this.name=名称; } 公共文本视图getPrice(){ 退货价格; } 公共无效设置价格(文本视图价格){ 这个价格=价格; } 公共文本视图getQuantity(){ 退货数量; } 公共无效设置数量(文本视图数量){ 这个。数量=数量; } }
布局文件夹中还需要一些xml文件,它们的外观如下: 您需要一个页脚视图,该视图将告诉您的列表何时加载新项目:Android 当选(假); 数据.添加(d); } 公共ArrayList getData(){ 返回数据; } public int getCount(){//获取listview中的元素数 返回data.size(); } 公共对象getItem(int-position){//此方法按位置返回对象 返回位置; } 公共长getItemId(int位置){//按位置获取项目id 返回位置; } 公共视图getView(){ 返回vi; } public View getView(int-position,View-convertView,ViewGroup-parent){//getView方法是用我们的个性化行填充列表视图的方法 vi=转换视图; 最终视图持有者=新的视图持有者(); if(convertView==null){ vi=充气机。充气(R.layout.custom_row_view,空); //listview中的每个项都使用xml“listview\u行”的设计 holder.name=(复选框)vi.findViewById(R.id.name); holder.price=(TextView)vi.findViewById(R.id.price);//您可以输入任何想要的内容,按钮、单选按钮、图像等。 holder.quantity=(TextView)vi.findViewById(R.id.quantity); 持有人姓名 .setOnCheckedChangeListener(新建CompoundButton.OnCheckedChangeListener(){ @凌驾 检查更改后的公共无效(复合按钮视图, 布尔值(已检查){ DataModel元素=(DataModel)holder.name .getTag(); element.setSelected(buttonView.isChecked()); } }); vi.setTag(支架); holder.name.setTag(data.get(position)); ViewHolder vholder=(ViewHolder)vi.getTag(); vholder.name.setChecked(data.get(position.isSelected()); HashMap hash=new HashMap();//我们需要一个HashMap来存储任何项目的数据 hash=data.get(position.getData(); vholder.name.setText(hash.get(“品牌”)+“”+hash.get(“名称”);//我们对行中的项目进行个性化设置。 vholder.price.setText(“$”+hash.get(“price”); vholder.quantity.setText(“数量:+hash.get(“数量”)); }否则{ vi=转换视图; ((ViewHolder)vi.getTag()).name.setTag(data.get(position)); } if(holder.name==null){ ViewHolder vholder=(ViewHolder)vi.getTag(); vholder.name.setChecked(data.get(position.isSelected()); HashMap hash=new HashMap();//我们需要一个HashMap来存储任何项目的数据 hash=data.get(position.getData(); vholder.name.setText(hash.get(“品牌”)+“”+hash.get(“名称”);//我们对行中的项目进行个性化设置。 vholder.price.setText(“$”+hash.get(“price”); vholder.quantity.setText(“数量:+hash.get(“数量”)); } 返回vi; } } 类视图持有者{ 复选框名称; 文本视图价格; 文本视图数量; 公共复选框getName(){ 返回名称; } public void setName(复选框名称){ this.name=名称; } 公共文本视图getPrice(){ 退货价格; } 公共无效设置价格(文本视图价格){ 这个价格=价格; } 公共文本视图getQuantity(){ 退货数量; } 公共无效设置数量(文本视图数量){ 这个。数量=数量; } },android,listview,Android,Listview,布局文件夹中还需要一些xml文件,它们的外观如下: 您需要一个页脚视图,该视图将告诉您的列表何时加载新项目: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:gravity="center_horizontal" android:padding
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:gravity="center_horizontal"
android:padding="3dp"
android:layout_height="fill_parent">
<TextView
android:id="@id/android:empty"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center"
android:padding="5dp"
android:text="Add more grocery items..."/>
由BaseAdapter填充的自定义行视图:
public class ListViewAdapter extends BaseAdapter {//To create an adapter we have to extend BaseAdapter instead of Activity, or whatever.
private ListActivity activity;
private View vi;
private ArrayList<DataModel> data;
private static LayoutInflater inflater=null;
public ListViewAdapter(ListActivity a, GroceryList g) {
activity = a;
data = new ArrayList<DataModel>();
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
groceries = g;
}
public void add(HashMap<String,String> a){
DataModel d = new DataModel(data.size());
d.setData(a);
d.setSelected(false);
data.add(d);
}
public ArrayList<DataModel> getData() {
return data;
}
public int getCount() { //get the number of elements in the listview
return data.size();
}
public Object getItem(int position) { //this method returns on Object by position
return position;
}
public long getItemId(int position) { //get item id by position
return position;
}
public View getView() {
return vi;
}
public View getView(int position, View convertView, ViewGroup parent) { //getView method is the method which populates the listview with our personalized rows
vi=convertView;
final ViewHolder holder = new ViewHolder();
if(convertView==null) {
vi = inflater.inflate(R.layout.custom_row_view, null);
//every item in listview uses xml "listview_row"'s design
holder.name = (CheckBox)vi.findViewById(R.id.name);
holder.price = (TextView)vi.findViewById(R.id.price); // You can enter anything you want, buttons, radiobuttons, images, etc.
holder.quantity = (TextView)vi.findViewById(R.id.quantity);
holder.name
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
DataModel element = (DataModel) holder.name
.getTag();
element.setSelected(buttonView.isChecked());
}
});
vi.setTag(holder);
holder.name.setTag(data.get(position));
ViewHolder vholder = (ViewHolder) vi.getTag();
vholder.name.setChecked(data.get(position).isSelected());
HashMap<String, String> hash = new HashMap<String, String>(); //We need a HashMap to store our data for any item
hash = data.get(position).getData();
vholder.name.setText(hash.get("brand") + " " + hash.get("name")); //We personalize our row's items.
vholder.price.setText("$" + hash.get("price"));
vholder.quantity.setText("Quantity: " + hash.get("quantity"));
} else {
vi = convertView;
((ViewHolder) vi.getTag()).name.setTag(data.get(position));
}
if (holder.name == null) {
ViewHolder vholder = (ViewHolder) vi.getTag();
vholder.name.setChecked(data.get(position).isSelected());
HashMap<String, String> hash = new HashMap<String, String>(); //We need a HashMap to store our data for any item
hash = data.get(position).getData();
vholder.name.setText(hash.get("brand") + " " + hash.get("name")); //We personalize our row's items.
vholder.price.setText("$" + hash.get("price"));
vholder.quantity.setText("Quantity: " + hash.get("quantity"));
}
return vi;
}
}
class ViewHolder {
CheckBox name;
TextView price;
TextView quantity;
public CheckBox getName() {
return name;
}
public void setName(CheckBox name) {
this.name = name;
}
public TextView getPrice() {
return price;
}
public void setPrice(TextView price) {
this.price = price;
}
public TextView getQuantity() {
return quantity;
}
public void setQuantity(TextView quantity) {
this.quantity = quantity;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<CheckBox
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox"
android:focusable="false"
android:textSize="25dip"
android:onClick="checkOff"
/>
<TextView
android:id="@+id/quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dip"
android:text="Lastname"
android:textSize="15dip" />
<TextView
android:id="@+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dip"
android:text="Lastname"
android:textSize="15dip" />
</LinearLayout>
还有一个父视图,我的称为create_screery_list,因为我正在编写一个screery list编辑器:这个视图必须包含一个具有正确id的ListView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="400dp" >
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ListView>
</LinearLayout>
<AbsoluteLayout
android:layout_width="match_parent"
android:layout_height="72dp" >
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="105dp"
android:layout_y="0dp"
android:onClick="deleteItem"
android:text="@string/deleteItem" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="8dp"
android:layout_y="0dp"
android:onClick="goToAddItemScreen"
android:text="@string/addItem" />
<Button
android:id="@+id/button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="221dp"
android:layout_y="0dp"
android:onClick="scanner"
android:text="@string/scanCode" />
</AbsoluteLayout>
</LinearLayout>
然后添加了两个Runnable以创建一个永不过时的杂货店列表:)祝您编程愉快…它实际上是isChecked方法上的一个空指针,但这是因为我的getTag方法工作不正常。当您调用getListView().getTag(i)时,它将从整个ListView而不是单个行获取标记,我猜你是想这么做。而且,如果您没有调用ListView.setTag(),它将始终返回null。我实际上还没有执行检查过的ListView,所以我编写了一个用于多次删除的简单测试应用程序。让我知道你是否想让我分享一些代码或建议。那太好了,迈克。我现在在上一节课的时候回顾一下教程,看看明天我是否能解决这个问题。我想我已经解决了。。任何人如果发现这个需要帮助,如果他们给我发电子邮件给david,就可以得到我的一些代码。hulse@live.com
public class ListViewAdapter extends BaseAdapter {//To create an adapter we have to extend BaseAdapter instead of Activity, or whatever.
private ListActivity activity;
private View vi;
private ArrayList<DataModel> data;
private static LayoutInflater inflater=null;
public ListViewAdapter(ListActivity a, GroceryList g) {
activity = a;
data = new ArrayList<DataModel>();
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
groceries = g;
}
public void add(HashMap<String,String> a){
DataModel d = new DataModel(data.size());
d.setData(a);
d.setSelected(false);
data.add(d);
}
public ArrayList<DataModel> getData() {
return data;
}
public int getCount() { //get the number of elements in the listview
return data.size();
}
public Object getItem(int position) { //this method returns on Object by position
return position;
}
public long getItemId(int position) { //get item id by position
return position;
}
public View getView() {
return vi;
}
public View getView(int position, View convertView, ViewGroup parent) { //getView method is the method which populates the listview with our personalized rows
vi=convertView;
final ViewHolder holder = new ViewHolder();
if(convertView==null) {
vi = inflater.inflate(R.layout.custom_row_view, null);
//every item in listview uses xml "listview_row"'s design
holder.name = (CheckBox)vi.findViewById(R.id.name);
holder.price = (TextView)vi.findViewById(R.id.price); // You can enter anything you want, buttons, radiobuttons, images, etc.
holder.quantity = (TextView)vi.findViewById(R.id.quantity);
holder.name
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
DataModel element = (DataModel) holder.name
.getTag();
element.setSelected(buttonView.isChecked());
}
});
vi.setTag(holder);
holder.name.setTag(data.get(position));
ViewHolder vholder = (ViewHolder) vi.getTag();
vholder.name.setChecked(data.get(position).isSelected());
HashMap<String, String> hash = new HashMap<String, String>(); //We need a HashMap to store our data for any item
hash = data.get(position).getData();
vholder.name.setText(hash.get("brand") + " " + hash.get("name")); //We personalize our row's items.
vholder.price.setText("$" + hash.get("price"));
vholder.quantity.setText("Quantity: " + hash.get("quantity"));
} else {
vi = convertView;
((ViewHolder) vi.getTag()).name.setTag(data.get(position));
}
if (holder.name == null) {
ViewHolder vholder = (ViewHolder) vi.getTag();
vholder.name.setChecked(data.get(position).isSelected());
HashMap<String, String> hash = new HashMap<String, String>(); //We need a HashMap to store our data for any item
hash = data.get(position).getData();
vholder.name.setText(hash.get("brand") + " " + hash.get("name")); //We personalize our row's items.
vholder.price.setText("$" + hash.get("price"));
vholder.quantity.setText("Quantity: " + hash.get("quantity"));
}
return vi;
}
}
class ViewHolder {
CheckBox name;
TextView price;
TextView quantity;
public CheckBox getName() {
return name;
}
public void setName(CheckBox name) {
this.name = name;
}
public TextView getPrice() {
return price;
}
public void setPrice(TextView price) {
this.price = price;
}
public TextView getQuantity() {
return quantity;
}
public void setQuantity(TextView quantity) {
this.quantity = quantity;
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:gravity="center_horizontal"
android:padding="3dp"
android:layout_height="fill_parent">
<TextView
android:id="@id/android:empty"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center"
android:padding="5dp"
android:text="Add more grocery items..."/>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<CheckBox
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox"
android:focusable="false"
android:textSize="25dip"
android:onClick="checkOff"
/>
<TextView
android:id="@+id/quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dip"
android:text="Lastname"
android:textSize="15dip" />
<TextView
android:id="@+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dip"
android:text="Lastname"
android:textSize="15dip" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="400dp" >
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ListView>
</LinearLayout>
<AbsoluteLayout
android:layout_width="match_parent"
android:layout_height="72dp" >
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="105dp"
android:layout_y="0dp"
android:onClick="deleteItem"
android:text="@string/deleteItem" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="8dp"
android:layout_y="0dp"
android:onClick="goToAddItemScreen"
android:text="@string/addItem" />
<Button
android:id="@+id/button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="221dp"
android:layout_y="0dp"
android:onClick="scanner"
android:text="@string/scanCode" />
</AbsoluteLayout>
</LinearLayout>