有時(shí)候項(xiàng)目中可能會(huì)用到表格的形式來(lái)展示信息置吓,這樣會(huì)比較直觀的看到數(shù)據(jù)菱阵,也會(huì)很清晰的坐到數(shù)據(jù)對(duì)比乳蓄,之前有一個(gè)項(xiàng)目用到了他爸,就來(lái)記錄一下:
首先就是布局聂宾,頁(yè)面很簡(jiǎn)單:
不知道我什么貼不出來(lái),頁(yè)面就是include一個(gè)表格的頭部诊笤,然后ScrollView嵌套一個(gè)自定義的listview
include嵌入的就是表格的頭部系谐,嵌入之后可以固定他的長(zhǎng)度,寬度讨跟,比較方便纪他,在外部寫可以設(shè)置大小,顏色和線條之類的晾匠,我寫的這個(gè)只是3個(gè)textview 之間加了view茶袒,頂部以及底部添加線條。
上面還有一個(gè)自定義的listview凉馆,因?yàn)閟crollview里面添加listview的時(shí)候弹谁,不是自定義的時(shí)候只顯示一條,所以我們?yōu)榱私鉀Q這個(gè)問(wèn)題句喜,就自定義一個(gè)listview预愤。
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView;
/**
* 解決ScrollView中嵌套ListView,ListView只顯示一行問(wèn)題
*/
public class WListView extends ListView {
? ? public WListView(Context context) {
? ? ? ? super(context);
? ? }
? ? public WListView(Context context, AttributeSet attrs) {
? ? ? ? super(context, attrs);
? ? }
? ? public WListView(Context context, AttributeSet attrs, int defStyleAttr) {
? ? ? ? super(context, attrs, defStyleAttr);
? ? }
? ? @Override
? ? protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
? ? ? ? //根據(jù)模式據(jù)算每個(gè)child的高度和寬度
? ? ? ? int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
? ? ? ? super.onMeasure(widthMeasureSpec, expandSpec);
? ? }
}
接下來(lái)就是Activity里面的代碼設(shè)置咳胃,構(gòu)建一個(gè)Bean類存放數(shù)據(jù)植康,再構(gòu)建一個(gè)適配器加載存放的數(shù)據(jù),放到listview里面展懈,完美的加載數(shù)據(jù)并顯示.Bean類就不貼出來(lái)了销睁,下面是Activity和Adapter的具體代碼:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.ztms_usb.Framgment.inter_face.KeyPath;
import com.example.ztmsandroid_USB.R;
import com.ztmsandroid.data.Data;
import com.ztmsandroid.utils.SharedPreferencesUtils;
import com.ztmsandroid.utils.ui.ProgressDialogEx;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
/**
* 數(shù)據(jù)
*/
public class Data_Table extends Activity {
? ? private String current_timestamp, username, key;
? ? private ProgressDialogEx progressDlgEx;
? ? private Handler mHandler = new Handler();
? ? private String sensor;
? ? private ListView tableListView;
? ? private List list;
? ? private String time, temp, hum, v, id;
? ? private TextView dayin, sensorr;
? ? private ImageView back;
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.datatable);
? ? ? ? Intent intent = getIntent();
? ? ? ? sensor = intent.getStringExtra("sensor");
? ? ? ? id = intent.getStringExtra("id");
? ? ? ? progressDlgEx = new ProgressDialogEx(this, mHandler);
? ? ? ? sensorr = (TextView) this.findViewById(R.id.sensor);
? ? ? ? sensorr.setText("Sensor:" + sensor);
? ? ? ? back = (ImageView) this.findViewById(R.id.back);
? ? ? ? back.setOnClickListener(new View.OnClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View v) {
? ? ? ? ? ? ? ? finish();
? ? ? ? ? ? }
? ? ? ? });
? ? ? ? ViewGroup tableTitle = (ViewGroup) findViewById(R.id.table_title);
? ? ? ? tableListView = (ListView) findViewById(R.id.list);
? ? ? ? show();
? ? }
? ? /*獲取運(yùn)單號(hào)信息*/
? ? private void show() {
? ? ? ? // TODO Auto-generated method stub
? ? ? ? username = SharedPreferencesUtils.getString(Data_Table.this, "userName", "");// 獲取用戶名
? ? ? ? key = Data.getInstance().key;
? ? ? ? current_timestamp = Data.getInstance().current_timestamp;
? ? ? ? new Thread() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void run() {
? ? ? ? ? ? ? ? progressDlgEx.simpleModeShowHandleThread();
? ? ? ? ? ? ? ? OkHttpClient okHttpClient = new OkHttpClient();
? ? ? ? ? ? ? ? String format = String.format(KeyPath.Path.head + KeyPath.Path.lastdatas, "c" + sensor, username, key, current_timestamp);
? ? ? ? ? ? ? ? Request build1 = new Request.Builder().url(format).build();
? ? ? ? ? ? ? ? okHttpClient.newCall(build1).enqueue(new Callback() {
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onFailure(Call call, IOException e) {
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onResponse(Call call, Response response) throws IOException {
? ? ? ? ? ? ? ? ? ? ? ? String string = response.body().string();
? ? ? ? ? ? ? ? ? ? ? ? if (string != null) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? final JSONArray jsonarray = new JSONArray(string);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? list = new ArrayList();
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? mHandler.post(new Runnable() {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? public void run() {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? for (int i = 0; i < jsonarray.length(); i++) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? JSONObject jsonObject = jsonarray.getJSONObject(i);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? time = jsonObject.get("time").toString();
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? temp = jsonObject.get("temp").toString();
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? hum = jsonObject.get("hum").toString();
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? v = jsonObject.get("v").toString();
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? list.add(new bean(time, temp, hum, v));
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } catch (JSONException e) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? TableAdapter adapter = new TableAdapter(Data_Table.this, list);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? tableListView.setAdapter(adapter);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? });
? ? ? ? ? ? ? ? ? ? ? ? ? ? } catch (JSONException e) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? ? ? ? ? ? ? ? ? } finally {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? progressDlgEx.closeHandleThread();
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? });
? ? ? ? ? ? }
? ? ? ? }.start();
? ? }
}
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.example.ztmsandroid_USB.R;
import java.util.List;
public class TableAdapter extends BaseAdapter {
? ? private List list;
? ? private LayoutInflater inflater;
? ? public TableAdapter(Context context, List list) {
? ? ? ? this.list = list;
? ? ? ? inflater = LayoutInflater.from(context);
? ? }
? ? @Override
? ? public int getCount() {
? ? ? ? int ret = 0;
? ? ? ? if (list != null) {
? ? ? ? ? ? ret = list.size();
? ? ? ? }
? ? ? ? return ret;
? ? }
? ? @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) {
? ? ? ? bean goods = (bean) this.getItem(position);
? ? ? ? ViewHolder viewHolder;
? ? ? ? if (convertView == null) {
? ? ? ? ? ? viewHolder = new ViewHolder();
? ? ? ? ? ? convertView = inflater.inflate(R.layout.list_item, null);
? ? ? ? ? ? viewHolder.goodId = (TextView) convertView.findViewById(R.id.text_id);
? ? ? ? ? ? viewHolder.goodName = (TextView) convertView.findViewById(R.id.text_goods_name);
? ? ? ? ? ? viewHolder.goodCodeBar = (TextView) convertView.findViewById(R.id.text_codeBar);
? ? ? ? ? ? viewHolder.goodNum = (TextView) convertView.findViewById(R.id.text_num);
? ? ? ? ? ? convertView.setTag(viewHolder);
? ? ? ? } else {
? ? ? ? ? ? viewHolder = (ViewHolder) convertView.getTag();
? ? ? ? }
? ? ? ? viewHolder.goodId.setText(goods.getTime());
? ? ? ? viewHolder.goodId.setTextSize(13);
? ? ? ? viewHolder.goodName.setText(goods.getTemp());
? ? ? ? viewHolder.goodName.setTextSize(13);
? ? ? ? viewHolder.goodCodeBar.setText(goods.getHum());
? ? ? ? viewHolder.goodCodeBar.setTextSize(13);
? ? ? ? viewHolder.goodNum.setText(goods.getV());
? ? ? ? viewHolder.goodNum.setTextSize(13);
? ? ? ? return convertView;
? ? }
? ? public static class ViewHolder {
? ? ? ? public TextView goodId;
? ? ? ? public TextView goodName;
? ? ? ? public TextView goodCodeBar;
? ? ? ? public TextView goodNum;
? ? }
}
適配器加載的Layout頁(yè)面和主頁(yè)面include的頁(yè)面是同一個(gè)頁(yè)面供璧。Activity只需要把接口取到的數(shù)據(jù)add到list里面,然后放到適配器就好冻记,然后適配器再展示數(shù)據(jù)睡毒,整體來(lái)說(shuō)還是比較簡(jiǎn)單的,主要是最近兩天比較空閑冗栗,所以就想著寫點(diǎn)東西記錄一下演顾。