您的位置:首页 > 移动开发 > Android开发

Android-简单的商城购物车Demo

2016-10-17 19:13 453 查看


购物车包含两块1;listview +textview 2;listview自定义适配器

点击一下加或者减就用Handle sent传回主线程计算总价;

handle中 sentMessage方法中传一个getsum方法,getsum方法返回sum;

getsum中获取数量和单价

也就是说每点击一次加或者减都要调用getsum方法获取数量和单价 得出总价,在总页面中settext获得总价

显示。

加或者减按钮监听中传一个position位置信息,这样解决每一个listview item项中焦点获取问题。

MainActivity :


<pre name="code" class="java">package com.example.cyx.easygouwu;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;

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

public class MainActivity extends AppCompatActivity {
private ListView listView;
private TextView textView;
private List<Shop> list=new ArrayList<>();
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 0) {
textView.setText("总价格:"+msg.obj.toString());
}
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.main_list);
textView = (TextView) findViewById(R.id.main_tv);
list.add(new Shop("a",100,0));
list.add(new Shop("b",20,0));
list.add(new Shop("c",60,0));
list.add(new Shop("d",5,0));
list.add(new Shop("e",10,0));
Myadapt adapt = new Myadapt(MainActivity.this, list, handler);
listView.setAdapter(adapt);
}
}



适配器:

package com.example.cyx.easygouwu;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

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

/**
* Created by cyx on 2016/10/17.
*/
class Myview {
public TextView textView1;
public TextView textView2;
public Button button1;
public Button button2;
public EditText editText;
}

public class Myadapt extends BaseAdapter {
private Context context;
private List<Shop> list;
private LayoutInflater inflater;
private Handler handler;
private Myview myview;

public Myadapt(Context context, List<Shop> list, Handler handler) {
thi
4000
s.context = context;
this.list = list;
this.handler = handler;
inflater = LayoutInflater.from(context);
}

public int getSum() {
int sum = 0;
for (int i = 0; i < list.size(); i++) {
int price = list.get(i).getPrice();
int num = list.get(i).getNum();
sum += price * num;
}
return sum;
}

public void add(int position) {
int num = list.get(position).getNum();
num++;
list.get(position).setNum(num);
Message message = new Message();

message.what = 0;
message.obj = getSum();

handler.sendMessage(message);
this.notifyDataSetChanged();
}

public void jian(int position) {
int num = list.get(position).getNum();
if (num > 0) {
num--;
list.get(position).setNum(num);
}
Message message = new Message();

message.what = 0;
message.obj = getSum();

handler.sendMessage(message);
this.notifyDataSetChanged();
}

public class getclick implements View.OnClickListener {
private int position;

public getclick(int position) {
this.position = position;
}

@Override
public void onClick(View v) {
int id = v.getId();
if (id == myview.button2.getId()) {
add(position);
} else if (id == myview.button1.getId()) {
jian(position);
}
}
}

@Override
public int getCount() {
return list.size();
}

@Override
public Object getItem(int position) {
return list.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
myview = new Myview();
if (convertView == null) {

convertView = inflater.inflate(R.layout.adaptlayout, null);
myview.textView1 = (TextView) convertView.findViewById(R.id.adapt_tv1);
myview.textView2 = (TextView) convertView.findViewById(R.id.adapt_tv2);
myview.button1 = (Button) convertView.findViewById(R.id.adapt_btn1);
myview.button2 = (Button) convertView.findViewById(R.id.adapt_btn2);
myview.editText = (EditText) convertView.findViewById(R.id.adapt_et);

myview.button1.setOnClickListener(new getclick(position));
myview.button2.setOnClickListener(new getclick(position));

convertView.setTag(myview);

} else {
myview = (Myview) convertView.getTag();
}

myview.textView1.setText("名称:"+list.get(position).getName());
myview.textView2.setText("单价:"+String.valueOf(list.get(position).getPrice()));
myview.editText.setText(String.valueOf(list.get(position).getNum()));

return convertView;
}
}


布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.cyx.easygouwu.MainActivity"
android:orientation="vertical">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/main_list">

</ListView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/main_tv"
/>
</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">

<TextView
android:id="@+id/adapt_tv1"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1" />

<TextView
android:id="@+id/adapt_tv2"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1" />

<Button
android:id="@+id/adapt_btn1"
android:layout_width="47dip"
android:layout_height="47dip"
android:background="@drawable/jian"/>

<EditText
android:id="@+id/adapt_et"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:enabled="false"
/>

<Button
android:id="@+id/adapt_btn2"
android:layout_width="47dip"
android:layout_height="47dip"
android:background="@drawable/add"/>

</LinearLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: