主界面解析数据、设置监听========================================
package com.bwie.test;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import javax.net.ssl.HttpsURLConnection;
import com.bwie.bean.Bean;
import com.bwie.bean.Data;
import com.google.gson.Gson;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
public class MainActivity extends ActionBarActivity {
// 设置路径
private String path = "http://121.42.8.95:8090/AndroidServer/yk.json";
private Button bt;
private ListView lv;
private List<Data> list;
private MyAdapter adapter;
boolean i = true;
// 主线程接收子线程发送来的消息
Handler handler = new Handler() {
public void handleMessage(Message msg) {
// 得到Bean数据
Bean bean = (Bean) msg.obj;
// 封装到集合
list = bean.getData();
adapter = new MyAdapter(MainActivity.this, list);
// 适配器
lv.setAdapter(adapter);
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 找到控件ID
bt = (Button) findViewById(R.id.bt1);
lv = (ListView) findViewById(R.id.lv);
// 解析数据
getData();
}
private void getData() {
new Thread() {
public void run() {
try {
// 得到URL对象并获取地址
URL url = new URL(path);
// 得到HttpURLConnection对象
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
// 得到输入流
InputStream input = conn.getInputStream();
// 解析
Gson gson = new Gson();
// 得到数据
Bean bean = gson.fromJson(new InputStreamReader(input),
Bean.class);
// 发送消息到主线程
Message msg = Message.obtain();
msg.obj = bean;
handler.sendMessage(msg);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
}.start();
}
// 全选
public void xuanze(View v) {
// 判断是否选择
if (i) {
for (Data d : list) {
d.setCheck(true);
}
i = false;
bt.setText("取消");
} else {
for (Data d : list) {
d.setCheck(false);
}
i = true;
bt.setText("全选");
}
// 刷新
adapter.notifyDataSetChanged();
}
// 进行选择删除
public void delete(View v) {
// 循环遍历
for (int j = 0; j < list.size(); j++) {
Data d = list.get(j);
// 判断是否选择
if (d.isCheck()) {
// 删除
list.remove(j);
j--;
}
}
// 刷新
adapter.notifyDataSetChanged();
}
}
主界面布局==================================================
<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"
android:orientation="vertical"
tools:context="com.bwie.test.MainActivity" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginBottom="10dp"
android:text="类别"
android:layout_marginTop="10dp"
android:textSize="20sp"
/>
<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="5dp"
android:onClick="xuanze"
android:text="全选"
/>
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#cccccc"
/>
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="5"
></ListView>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:drawableTop="@drawable/ic_launcher"
android:text="删除"
android:onClick="delete"
android:layout_weight="1"
/>
</LinearLayout>
主界面设配====================================================================
package com.bwie.test;
import java.util.List;
import com.bwie.bean.Data;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
//进行适配
public class MyAdapter extends BaseAdapter {
// 封装属性
Context context;
List<Data> list;
// 得到有参构造方法
public MyAdapter(Context context, List<Data> list) {
this.context = context;
this.list = list;
}
// 获取条目数
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
// 获取内容
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
// 填充页面
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
viewHolder vh;
// 判断是否为空
if (convertView == null) {
vh = new viewHolder();
// 填充页面
convertView = View.inflate(context, R.layout.item, null);
// 找到控件ID
vh.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
vh.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
vh.cb = (CheckBox) convertView.findViewById(R.id.cb);
convertView.setTag(vh);
} else {
vh = (viewHolder) convertView.getTag();
}
// 获取内容
vh.tv_time.setText(list.get(position).getChannelName());
vh.tv_time.setText(list.get(position).getCreateDate());
vh.cb.setChecked(list.get(position).isCheck());
// 设置CheckBox的监听
vh.cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
// 判断是否选择
if (isChecked) {
list.get(position).setCheck(true);
} else {
list.get(position).setCheck(false);
}
}
});
return convertView;
}
class viewHolder {
TextView tv_name, tv_time;
CheckBox cb;
}
}
设配器的Item===================================================
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Name"
/>
<TextView
android:id="@+id/tv_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_below="@+id/tv_name"
android:text="Time"
/>
<CheckBox
android:id="@+id/cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:layout_centerVertical="true"
/>
</RelativeLayout>
Bean包================================================================
package com.bwie.bean;
import java.util.List;
public class Bean {
private List<Data>data;
public List<Data> getData() {
return data;
}
public void setData(List<Data> data) {
this.data = data;
}
@Override
public String toString() {
return "Bean [data=" + data + "]";
}
}
package com.bwie.bean;
public class Data {
private String channelId;
private String channelKey;
private String channelLogo;
private String channelName;
private String channelVersion;
private String createDate;
private String descr;
private String uid;
private boolean isCheck;
public boolean isCheck() {
return isCheck;
}
public void setCheck(boolean isCheck) {
this.isCheck = isCheck;
}
public String getChannelId() {
return channelId;
}
public void setChannelId(String channelId) {
this.channelId = channelId;
}
public String getChannelKey() {
return channelKey;
}
public void setChannelKey(String channelKey) {
this.channelKey = channelKey;
}
public String getChannelLogo() {
return channelLogo;
}
public void setChannelLogo(String channelLogo) {
this.channelLogo = channelLogo;
}
public String getChannelName() {
return channelName;
}
public void setChannelName(String channelName) {
this.channelName = channelName;
}
public String getChannelVersion() {
return channelVersion;
}
public void setChannelVersion(String channelVersion) {
this.channelVersion = channelVersion;
}
public String getCreateDate() {
return createDate;
}
public void setCreateDate(String createDate) {
this.createDate = createDate;
}
public String getDescr() {
return descr;
}
public void setDescr(String descr) {
this.descr = descr;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
@Override
public String toString() {
return "Data [channelId=" + channelId + ", channelKey=" + channelKey
+ ", channelLogo=" + channelLogo + ", channelName="
+ channelName + ", channelVersion=" + channelVersion
+ ", createDate=" + createDate + ", descr=" + descr + ", uid="
+ uid + ", isCheck=" + isCheck + "]";
}
}