有时候项目中可能会用到表格的形式来展示信息,这样会比较直观的看到数据,也会很清晰的坐到数据对比,之前有一个项目用到了,就来记录一下:
首先就是布局,页面很简单:
不知道我什么贴不出来,页面就是include一个表格的头部,然后ScrollView嵌套一个自定义的listview
include嵌入的就是表格的头部,嵌入之后可以固定他的长度,宽度,比较方便,在外部写可以设置大小,颜色和线条之类的,我写的这个只是3个textview 之间加了view,顶部以及底部添加线条。
上面还有一个自定义的listview,因为scrollview里面添加listview的时候,不是自定义的时候只显示一条,所以我们为了解决这个问题,就自定义一个listview。
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView;
/**
* 解决ScrollView中嵌套ListView,ListView只显示一行问题
*/
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) {
//根据模式据算每个child的高度和宽度
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
接下来就是Activity里面的代码设置,构建一个Bean类存放数据,再构建一个适配器加载存放的数据,放到listview里面,完美的加载数据并显示.Bean类就不贴出来了,下面是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;
/**
* 数据
*/
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();
}
/*获取运单号信息*/
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页面和主页面include的页面是同一个页面。Activity只需要把接口取到的数据add到list里面,然后放到适配器就好,然后适配器再展示数据,整体来说还是比较简单的,主要是最近两天比较空闲,所以就想着写点东西记录一下。