Android的列表视图选择项目视图、项目、列表、Android

2023-09-07 09:55:43 作者:卧槽尓玛 \\\\ 给俄滚

我有最低十项食物进入列表视图。

我要选择一个或多个项目的食品。

和pressed Order按钮。

然后告诉我总价格选择项食品。

怎么可能请告诉我?

MenuListItem:

 包com.example.project_hotel_management;进口的java.util.ArrayList;进口的java.util.List;进口android.app.Activity;进口android.os.Bundle;进口android.view.View;进口android.widget.AdapterView;进口android.widget.AdapterView.OnItemClickListener;进口android.widget.ListView;进口android.widget.Toast;公共类MenuListItem延伸活动{    LV的ListView;    MyAdapter适配器;    @覆盖    保护无效的onCreate(捆绑savedInstanceState){        // TODO自动生成方法存根        super.onCreate(savedInstanceState);    的setContentView(R.layout.menuitemlist_layout);    LV =(ListView控件)findViewById(R.id.listView1);    适配器=新MyAdapter(在此,getListItem());    lv.setAdapter(适配器);    lv.setOnItemClickListener(新OnItemClickListener(){        @覆盖        公共无效onItemClick(适配器视图<>为arg0,ARG1观,诠释ARG2,                长ARG3){            // TODO自动生成方法存根            Toast.makeText(getApplicationContext(),D,Toast.LENGTH_LONG).show();        }    });    }    公众的ArrayList<&列表项GT; getListItem(){        ArrayList的<&列表项GT; alllist =新的ArrayList<&列表项GT;();        列表项L1 =新的ListItem(beef_burguer,1,R.drawable.beef_burguer);        列表项L2 =新的ListItem(bacon_cheese_burger,2,R.drawable.bacon_cheese_burger);        列表项13 =新的ListItem(弗林斯,3,R.drawable.frings);        列表项14 =新的ListItem(burger_kingse,4,R.drawable.burger_kingse);        列表项15 =新的ListItem(mint_oreo,5,R.drawable.mint_oreo);        列表项16 =新的ListItem(sourdough_bread,6,R.drawable.sourdough_bread);        列表项17 =新的ListItem(三明治,7,R.drawable.sandwich);        列表项18 =新的ListItem(sandwich_loaded,8,R.drawable.sandwich_loaded);        列表项L9 =新的ListItem(芥末,9,R.drawable.mustard);        列表项L10 =新的ListItem(cheese_burger,10,R.drawable.cheese_burger);        alllist.add(L1);        alllist.add(12);        alllist.add(13);        alllist.add(14);        alllist.add(15);        alllist.add(16);        alllist.add(17);        alllist.add(18);        alllist.add(L9);        alllist.add(L10);        返回alllist;    }    公共无效订单(视图v){        字符串结果=;        清单<整数GT; resultList = adapter.getCheckedItemPosition();        的for(int i = 0; I< resultList.size();我++){            结果+ =将String.valueOf(resultList.get(I))+\\ n;        }        Toast.makeText(getApplicationContext(),结果,Toast.LENGTH_LONG).show();    }} 

MyAdapter:

 包com.example.project_hotel_management;进口的java.util.ArrayList;进口的java.util.HashMap;进口的java.util.List;进口android.app.Activity;进口android.content.ClipData.Item;进口android.view.LayoutInflater;进口android.view.View;进口android.view.ViewGroup;进口android.widget.ArrayAdapter;进口android.widget.CheckBox;进口android.widget.ImageView;进口android.widget.TextView;进口android.widget.Toast;公共类MyAdapter扩展ArrayAdapter<&列表项GT; {    活动背景;    ArrayList的<&列表项GT;的listItem;    HashMap的<整数,布尔> myChecked =新的HashMap<整数,布尔>();    公共MyAdapter(Activity上下文,ArrayList的<&列表项GT;的listItem){        超(背景下,R.layout.adapter_layout,的listItem);         this.context =背景;         this.listItem =的listItem;       的for(int i = 0; I< listItem.size();我++){        myChecked.put(I,FALSE);    }    }    公共无效toggleChecked(INT位置){        如果(myChecked.get(位置)){            myChecked.put(位置,FALSE);            Toast.makeText(的getContext(),假,Toast.LENGTH_LONG).show();        }其他{            myChecked.put(位置,真正的);            Toast.makeText(的getContext(),假,Toast.LENGTH_LONG).show();        }        notifyDataSetChanged();    }    公开名单<整数GT; getCheckedItemPosition(){        清单<整数GT; checkedItemPosition =新的ArrayList<整数GT;();        的for(int i = 0; I< myChecked.size();我++){            checkedItemPosition.add(ⅰ);        }        返回checkedItemPosition;    }//公开名单<整数GT; getCheckedItem(){//列表<整数GT; checkedItem =新的ArrayList<整数GT;();////的for(int i = 0; I< myChecked.size();我++){// checkedItem.add();//}//返回checkedItem;//}    @覆盖    公共查看getView(INT位置,查看convertView,父母的ViewGroup){        查看查看= convertView;            LayoutInflater inflat = context.getLayoutInflater();            鉴于= inflat.inflate(R.layout.adapter_layout,空,假);            TextView的txtName的=(TextView中)view.findViewById(R.id.txtName);            TextView的txtPrice =(TextView中)view.findViewById(R.id.txtPrice);            ImageView的图像=(ImageView的)view.findViewById(R.id.imageView1);            列表项L = listItem.get(位置);            Toast.makeText(的getContext(),将String.valueOf(listItem.get(位置)),Toast.LENGTH_LONG             )。显示();            txtName.setText(l.getName());            txtPrice.setText(将String.valueOf(l.getPrice()));            image.setImageResource(l.getImage());            复选框chekboxs =(复选框)view.findViewById(R.id.checkBox1);            布尔chekBox = myChecked.get(位置);            如果(chekBox!= NULL){                chekboxs.setChecked(chekBox);            }        返回视图。    }} 
安卓开发试验1

列表项:

 包com.example.project_hotel_management;公共类列表项{    字符串名称;    整数价格;    整数形象;    公共列表项(字符串名称,整价格,整型图像){        超();        this.name =名称;        this.price =价格;        this.image =图像;    }    公共字符串的getName(){        返回名称;    }    公共无效setname可以(字符串名称){        this.name =名称;    }    公共整数用getPrice(){        返回的价格;    }    公共无效setPrice(整数价){        this.price =价格;    }    公共整数的getImage(){        返回形象;    }    公共无效setImage(整数图片){        图像=图像;    }    @覆盖    公共字符串的toString(){        返回列表项[NAME =+名字+,价格=+价格+,图像=                +图像+];    }} 

我用这一点,但它不工作:

  lv.setOnItemClickListener(新OnItemClickListener(){@覆盖公共无效onItemClick(适配器视图<>为arg0,ARG1查看,INT位置,        长ARG3){    // TODO自动生成方法存根    。列表项LS = getListItem()获得(位置);    Toast.makeText(getApplicationContext(),将String.valueOf(ls.getPrice()),Toast.LENGTH_LONG).show();}}); 

解决方案

您可以简单地将的android:choiceMode =multipleChoice在你的列表视图。还可以实现的选择器来定义所选择的项目将如何被突出显示。一个选择的一个例子:

 <?XML版本=1.0编码=UTF-8&GT?;<选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>    <项目机器人:可绘制=@绘制/ list_item_bg_normal机器人:state_activated =FALSE/>    <项目机器人:可绘制=@绘制/ list_item_bg_ pressed机器人:STATE_ pressed =真/>    <项目机器人:可绘制=@绘制/ list_item_bg_ pressed机器人:state_activated =真/>< /选择> 

一旦在的ListView 一个或多个项目被选中,可以通过获取选定的项目:

  INT LEN = listView.getCount();SparseBooleanArray检查= listView.getCheckedItemPositions();的for(int i = 0; I< LEN,我++)如果(checked.get(ⅰ)){字符串项= cont_list.get(I)/ *计算价格* /} 

I have minimum Ten item food into Listview.

I want select one or more item food .

And pressed Order button.

Then show me total price for select item food.

How possible please tell me?

MenuListItem:

package com.example.project_hotel_management;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

public class MenuListItem extends Activity{

    ListView lv;
    MyAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
    setContentView(R.layout.menuitemlist_layout);

    lv=(ListView)findViewById(R.id.listView1);

    adapter=new MyAdapter(this , getListItem());
    lv.setAdapter(adapter);

    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "d", Toast.LENGTH_LONG).show();

        }
    });
    }

    public ArrayList<ListItem> getListItem() {
        ArrayList<ListItem> alllist=new ArrayList<ListItem>();

        ListItem l1=new ListItem("beef_burguer", 1, R.drawable.beef_burguer);
        ListItem l2=new ListItem("bacon_cheese_burger", 2, R.drawable.bacon_cheese_burger);
        ListItem l3=new ListItem("frings", 3, R.drawable.frings);
        ListItem l4=new ListItem("burger_kingse", 4, R.drawable.burger_kingse);
        ListItem l5=new ListItem("mint_oreo", 5, R.drawable.mint_oreo);
        ListItem l6=new ListItem("sourdough_bread", 6, R.drawable.sourdough_bread);
        ListItem l7=new ListItem("sandwich", 7, R.drawable.sandwich);
        ListItem l8=new ListItem("sandwich_loaded", 8, R.drawable.sandwich_loaded);
        ListItem l9=new ListItem("mustard", 9, R.drawable.mustard);
        ListItem l10=new ListItem("cheese_burger", 10, R.drawable.cheese_burger);

        alllist.add(l1);
        alllist.add(l2);
        alllist.add(l3);
        alllist.add(l4);
        alllist.add(l5);
        alllist.add(l6);
        alllist.add(l7);
        alllist.add(l8);
        alllist.add(l9);
        alllist.add(l10);



        return alllist;
    }

    public void order(View v) {

        String result="";
        List<Integer> resultList=adapter.getCheckedItemPosition();
        for (int i = 0; i < resultList.size(); i++) {
            result+=String.valueOf(resultList.get(i))+"\n";
        }
        Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();

    }

}

MyAdapter:

package com.example.project_hotel_management;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.ClipData.Item;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MyAdapter extends ArrayAdapter<ListItem>{

    Activity context;
    ArrayList<ListItem> listItem;

    HashMap<Integer, Boolean> myChecked=new HashMap<Integer, Boolean>();

    public MyAdapter(Activity context, ArrayList<ListItem> listItem) {
        super(context, R.layout.adapter_layout, listItem);
         this.context=context;
         this.listItem=listItem;

       for (int i = 0; i < listItem.size(); i++) {
        myChecked.put(i, false);
    }

    }


    public void toggleChecked(int position) {
        if(myChecked.get(position)){
            myChecked.put(position, false);
            Toast.makeText(getContext(), "false", Toast.LENGTH_LONG).show();
        }else{
            myChecked.put(position, true);
            Toast.makeText(getContext(), "false", Toast.LENGTH_LONG).show();
        }
        notifyDataSetChanged();
    }

    public List<Integer> getCheckedItemPosition() {
        List<Integer> checkedItemPosition=new ArrayList<Integer>();

        for (int i = 0; i < myChecked.size(); i++) {
            checkedItemPosition.add(i);
        }
        return checkedItemPosition;
    }

//  public List<Integer> getCheckedItem() {
//      List<Integer> checkedItem=new ArrayList<Integer>();
//      
//      for (int i = 0; i < myChecked.size(); i++) {
//          checkedItem.add();
//      }
//      return checkedItem;
//  }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view=convertView;

            LayoutInflater inflat=context.getLayoutInflater();
            view=inflat.inflate(R.layout.adapter_layout, null, false);
            TextView txtName=(TextView) view.findViewById(R.id.txtName);
            TextView txtPrice=(TextView) view.findViewById(R.id.txtPrice);
            ImageView image=(ImageView) view.findViewById(R.id.imageView1);

            ListItem l=listItem.get(position);
            Toast.makeText(getContext(), String.valueOf(listItem.get(position)), Toast.LENGTH_LONG
             ).show();

            txtName.setText(l.getName());
            txtPrice.setText(String.valueOf(l.getPrice()));
            image.setImageResource(l.getImage());

            CheckBox chekboxs=(CheckBox) view.findViewById(R.id.checkBox1);

            Boolean chekBox=myChecked.get(position);

            if(chekBox!=null){
                chekboxs.setChecked(chekBox);
            }


        return view;
    }
}

ListItem:

package com.example.project_hotel_management;

public class ListItem {

    String name;
    Integer price;
    Integer image;



    public ListItem(String name, Integer price, Integer image) {
        super();
        this.name = name;
        this.price = price;
        this.image = image;
    }


    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getPrice() {
        return price;
    }
    public void setPrice(Integer price) {
        this.price = price;
    }
    public Integer getImage() {
        return image;
    }
    public void setImage(Integer image) {
        image = image;
    }
    @Override
    public String toString() {
        return "ListItem [name=" + name + ", price=" + price + ", Image="
                + image + "]";
    }
}

I use this but it's not working:

lv.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
        long arg3) {
    // TODO Auto-generated method stub
    ListItem ls=getListItem().get(position);
    Toast.makeText(getApplicationContext(), String.valueOf(ls.getPrice()), Toast.LENGTH_LONG).show();


}


});

解决方案

You can simply set the android:choiceMode="multipleChoice" on your listview. You can also implement a selector to define how the selected item will be highlighted. An example of a selector:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/list_item_bg_normal" android:state_activated="false"/>
    <item android:drawable="@drawable/list_item_bg_pressed" android:state_pressed="true"/>
    <item android:drawable="@drawable/list_item_bg_pressed" android:state_activated="true"/>

</selector>

Once one or more items in the ListView are selected, you can get the selected items by:

int len = listView.getCount();
SparseBooleanArray checked = listView.getCheckedItemPositions();
for (int i = 0; i < len; i++)
if (checked.get(i)) {
String item = cont_list.get(i);
/* calculate the price  */
}