实现效果如图
使用ListView显示信息列表
1.理解ListView的基础使用
ListView.作用:Android系统中显示列表的控件
2.两种适配器 ArrayAdapter SimpleAdapter
(1).数据适配器 作用:把复杂的数据(数组,链表,数据库,集合等)填充在指定视图界面上,是连接数据源和视图界面的桥梁。
(2).ArrayAdapter(数组适配器):用于绑定格式单一的数据
数据源:可以是集合或数组
(3).SimpleAdapter(简单适配器):用于绑定格式复杂的数据
数据源:只能是特定泛型的集合
(4).实现过程:新建适配器——>添加数据源到适配器——>视图加载适配器
(5).数组适配器的实现
MainActivity.java
package com.example.peiming.a01;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
/**
* 主菜单页面
*/
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.songhuodan_btn);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.songhuodan_btn:
//跳转销售送货单页面
Intent intent = new Intent(this, SonghuodanActivity.class);
startActivity(intent);
Toast.makeText(getApplicationContext(), "销售送货单,欢迎你,", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}
SonghuodanActivity.java
package com.example.peiming.a01;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import cz.msebera.android.httpclient.Header;
import entity.BaseSonghuodanEntity;
/**
* 送货单页面
*/
public class SonghuodanActivity extends AppCompatActivity implements Serializable {
private ListView listView;
private SimpleAdapter simpleAdapter;
private List<Map<String,Object>> dataList;
AsyncHttpClient client;//异步加载
private Handler handler = new MyHandler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_songhuodan);
listView = (ListView) findViewById(R.id.listView);
//1.新建数据适配器
/**
* SimpleAdapter(context,data,resource,from,to)
* context : 上下文
* data : 数据源 List<? extends Map<String, ?>> data 一个Map所组成的List集合
* 每一个Map都会去对应ListView列表中的一行
* 每一个Map中的键都必须包含所有在from中所指定的键
* resource :列表项布局文件的ID
* from : Map中的键名
* to : 绑定数据视图中的ID,与from成对应关系
*/
//2.适配器加载数据源
dataList = new ArrayList<Map<String, Object>>();
simpleAdapter = new SimpleAdapter(this,getData(),R.layout.item
,new String[]{"id","songhuodanhao","pingming"},new int[]{R.id.id,R.id.songhuodanhao,R.id.pingming});
//3.视图(ListView)加载适配器(simpleAdapter)
listView.setAdapter(simpleAdapter);
}
private List<Map<String,Object>> getData() {
final List<Map<String,Object>> entityList = new ArrayList<Map<String, Object>>();
client = new AsyncHttpClient();//实例化AsyncHttpClient
String url = "http://192.168.2.199:8080/项目/songhuodanServlet";
client.get(url, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
try {
JSONObject jsonObject = new JSONObject(new String(responseBody));
JSONArray jsonArray = jsonObject.getJSONArray("data");
for (int i=0; i<jsonArray.length(); ++i) {
BaseSonghuodanEntity songhuodan = BaseSonghuodanEntity.json2novel(jsonArray.getJSONObject(i));
if (songhuodan != null) {
Map<String,Object> map = new HashMap<String,Object>();
map.put("id",""+(i+1));
map.put("songhuodanhao",songhuodan.getSonghuodanhao());
map.put("pingming",songhuodan.getPingming());
dataList.add(map);
}
}
entityList.addAll(dataList);
Message message = handler.obtainMessage();
// 这个message.what对应 MyHandler类里面handleMessage方法的msg.what,你赋什么值都可以,对应就好了
message.what = 0x01;
message.obj = entityList;
handler.sendMessage(message);
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(SonghuodanActivity.this, "查看销售送货单失败!", Toast.LENGTH_SHORT).show();
return;
}
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
Toast.makeText(SonghuodanActivity.this, "查看销售送货单失败!", Toast.LENGTH_SHORT).show();
return;
}
});
return entityList;
}
class MyHandler extends Handler {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 0x01) {
dealWithData();
}
}
}
//这里面处理数据
private void dealWithData() {
// 这时候entityList应该是有数据的
// 刷新列表
simpleAdapter.notifyDataSetChanged();
}
}
activity_songhuodan.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="50dp"
android:layout_height="30dp"
android:textSize="20dp"
android:gravity="center"
android:text="序号"
/>
<TextView
android:layout_width="134dp"
android:layout_height="30dp"
android:textSize="20dp"
android:gravity="center"
android:text="送货单号"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="30dp"
android:textSize="20dp"
android:gravity="center"
android:text="物料名称"
/>
</LinearLayout>
<ListView
android:id="@+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" ><!-- 使用线性布局,默认不写的话是线性布局 -->
<TextView
android:layout_width="50dp"
android:textSize="22sp"
android:layout_height="wrap_content"
android:gravity="center"
android:id="@+id/id"
/>
<TextView
android:id="@+id/songhuodanhao"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="22sp" />
<TextView
android:layout_width="fill_parent"
android:textSize="22sp"
android:layout_height="wrap_content"
android:gravity="center"
android:id="@+id/pingming"
/>
</LinearLayout>
例子下载
链接:https://pan.baidu.com/s/1F6ZEojtu0Ly9z-Nabwa-gg
提取码:vfhz