赞
踩
package abeam.example.dddtext;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import DBUtils.DatabaseAdapter;
public class AdminOrderActivity extends Activity {
Set greenselected= new HashSet<>();//存放已经选择的菜
String[] greenname={"仙草奶茶","柠檬奶茶","朗姆奶茶","雪顶咖啡","梅梅奶茶"};
String[] greenprice={"10.00","15.00","25.00","12.00","20.00"};
int[] greenimgs=new int[]{R.drawable.i2,R.drawable.i2,R.drawable.i2,R.drawable.i2,R.drawable.i2};
ListView listview;
//生成动态数组,存放数据
ArrayList> Memulist =
new ArrayList<>();
public static long orderId = 0L;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listmenu);
listview= findViewById(R.id.SelectedItem);
getData();//调用mydata方法,将Memulist写入
//新建一个适配器
MyAdapter myadapter =new MyAdapter(this);
//listview控件装载适配器
listview.setAdapter(myadapter);
}
//getData方法的功能:将数据存放入Memulist
private void getData(){
for(int i=0;i
HashMap map = new HashMap<>();
map.put("ItemName", greenname[i]);
map.put("ItemPrice", greenprice[i]);
map.put("ItemImage", greenimgs[i]);
Memulist.add(map);
}
}
//编写特定的适配器类,必须继承已有的基本适配器类
class MyAdapter extends BaseAdapter{
DatabaseAdapter db = new DatabaseAdapter(getApplicationContext());
private final LayoutInflater mInflater;//获得视图
//与上下文 即当前的活动关联起来
public MyAdapter(Context context){
this.mInflater=LayoutInflater.from(context);
}
@Override
public int getCount() {
return Memulist.size();//size方法是干什么的
}
@Override
public Object getItem(int arg0) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;//准备存放一行的四个控件
if (convertView == null) {
holder= new ViewHolder();
//可以理解为从vlist获取view 之后把view返回给ListView
convertView = mInflater.inflate(R.layout.menulist, null);
holder.image = convertView.findViewById(R.id.imageView1);
holder.name = convertView.findViewById(R.id.textView1);
holder.price = convertView.findViewById(R.id.textView2);
holder.viewBtn = convertView.findViewById(R.id.SelectOne);
convertView.setTag(holder);
}else {
holder = (ViewHolder)convertView.getTag();
}
holder.image.setImageResource(greenimgs[position]);
holder.name.setText((String)Memulist.get(position).get("ItemName"));
holder.price.setText((String)Memulist.get(position).get("ItemPrice"));
holder.viewBtn.setTag(position);
//给Button添加单击事件 添加Button之后ListView将失去焦点
//需要的直接把Button的焦点去掉 android:focusable="false"
holder.viewBtn.setOnClickListener(v -> {
//是否清除了已选择菜单
if(ListActivity.selectedItemFlag){
greenselected.clear();
}
ListActivity.selectedItemFlag = false;
// 如果选择奶茶数量>1,则不可以
if(!MainActivity.adminFlag && greenselected.size()>=1){
Toast.makeText(AdminOrderActivity.this,R.string.SelectError,Toast.LENGTH_LONG).show();
} else {
model.ViewHolder newgreen = new model.ViewHolder();
newgreen.name = Memulist.get(position).get("ItemName").toString();
newgreen.price = Memulist.get(position).get("ItemPrice").toString();
newgreen.image = greenimgs[position];
greenselected.add(newgreen);//将选中的菜单序号存入set中
Log.i("选中:"+Memulist.get(position).get("ItemName").toString(),Memulist.get(position).get("ItemPrice").toString());
//插入DB
db.open();
orderId = db.insertData(position,MainActivity.useridTemp,newgreen.name,newgreen.price,newgreen.image);
Log.e("已插入项目:",newgreen.name);
Log.e("已插入项目:",newgreen.price);
Log.e("已插入项目:",String.valueOf(newgreen.image));
db.close();
//在日志中显示所有已经选中的菜
for(model.ViewHolder it:greenselected){
Log.i("已经选中的:"+it.name,"该菜价格:"+it.price);
}
}
});
return convertView;
}
}
//内部类,与每一行数据对应
static class ViewHolder{
public ImageView image;//菜的图片
public TextView name; //菜名
public TextView price; //菜的单价
public Button viewBtn; //+号按钮
}
public void toList (View theButton){
Intent intent = new Intent(AdminOrderActivity.this,ListActivity.class);
intent.putExtra("list",(Serializable)greenselected);
startActivity(intent);
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。