我參考了官方的代碼listview Adapter用法,但是移植過來listview文字就變成灰了,並且能選中 下面貼出代碼。已經DEBUG 子線程返回data沒問題。
這是運行圖片。
package com.wastrel.activity;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import com.wastrel.provider.Tabletbl;
import com.wastrel.provider.TabletblDAO;
import com.wastrel.util.HttpUtil;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
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.TextView;
public class ChaZhuoActivity extends ListActivity {
private Handler myhander = null;
// private ListView tablelist = null;
// private TextView tableid = null;
private List<Tabletbl> data = new ArrayList<Tabletbl>();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO 自動生成的方法存根
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_chazhuo);
// tablelist = (ListView) findViewById(R.id.tablelist);
// tableid = (TextView) findViewById(R.id.tablenum);
myhander = new Handler() {
@SuppressWarnings("unchecked")
@Override
public void handleMessage(Message msg) {
// TODO 自動生成的方法存根
switch (msg.what) {
case 1:
data = (List<Tabletbl>) msg.obj;
TableListAdapter tblAdapter = new TableListAdapter(data,
getApplicationContext());
setListAdapter(tblAdapter);
break;
default:
break;
}
}
};
new Thread(updatetable).start();
}
Runnable updatetable = new Runnable() {
@Override
public void run() {
// TODO 自動生成的方法存根
String urlStr = HttpUtil.Base_URL + "servlet/UpdateTableServlet";
try {
// 實例化URL對象
URL url = new URL(urlStr);
// 打開連接
URLConnection conn = url.openConnection();
// 獲得輸入流
InputStream in = conn.getInputStream();
// 實例化DocumentBuilderFactory
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
// 實例化DocumentBuilder
DocumentBuilder builder = factory.newDocumentBuilder();
// 獲得Document
Document doc = builder.parse(in);
// 獲得節點列表
NodeList nl = doc.getElementsByTagName("table");
TabletblDAO tabletblDAO = new TabletblDAO(
getApplicationContext());
if (nl.getLength() > 0) {
tabletblDAO.DeleteData();
}
// 循環將數據保存到菜譜表
List<Tabletbl> list = new ArrayList<Tabletbl>();
for (int i = 0; i < nl.getLength(); i++) {
// 實例化ContentValues
// 解析XML文件獲得桌號id
int id = Integer.parseInt(doc.getElementsByTagName("id")
.item(i).getFirstChild().getNodeValue());
int num = Integer.parseInt(doc.getElementsByTagName("num")
.item(i).getFirstChild().getNodeValue());
// 名稱
String name = doc.getElementsByTagName("name").item(i)
.getFirstChild().getNodeValue();
int flag = Integer.parseInt(doc
.getElementsByTagName("flag").item(i)
.getFirstChild().getNodeValue());
String description = doc
.getElementsByTagName("description").item(i)
.getFirstChild().getNodeValue();
/*
* values.put("id", id); values.put("num", num);
* values.put("name", name); values.put("flag", flag);
* values.put("description", description);
*/
list.add(new Tabletbl(id, num, name, flag, description));
// 添加到ContenValues對象
// 插入到數據庫
// tabletblDAO.AddTabletblDAO(values);
}
Message msg = myhander.obtainMessage();
msg.what = 1;
msg.obj = list;
myhander.sendMessage(msg);
} catch (Exception e) {
e.printStackTrace();
}
}
};
private static class TableListAdapter extends BaseAdapter {
public List<Tabletbl> mlist = null;
private Context context;
public TableListAdapter(List<Tabletbl> list, Context c) {
mlist = list;
context = c;
}
@Override
public int getCount() {
// TODO 自動生成的方法存根
return mlist.size();
}
@Override
public Object getItem(int position) {
// TODO 自動生成的方法存根
return position;
}
@Override
public long getItemId(int position) {
// TODO 自動生成的方法存根
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO 自動生成的方法存根
ViewHolder holder;
// When convertView is not null, we can reuse it directly, there is
// no
// need
// to reinflate it. We only inflate a new View when the convertView
// supplied
// by ListView is null.
if (convertView == null) {
LayoutInflater mInflater = LayoutInflater.from(context);
convertView = mInflater.inflate(R.layout.chazhuo_list, null);
// Creates a ViewHolder and store references to the two children
// views
// we want to bind data to.
holder = new ViewHolder();
holder.idtext = (TextView) convertView
.findViewById(R.id.chazhuo_id);
holder.nametext = (TextView) convertView
.findViewById(R.id.chazhuo_name);
holder.flagtext = (TextView) convertView
.findViewById(R.id.chazhuo_flag);
holder.remarktext = (TextView) convertView
.findViewById(R.id.chazhuo_remark);
convertView.setTag(holder);
} else {
// Get the ViewHolder back to get fast access to the TextView
// and the ImageView.
holder = (ViewHolder) convertView.getTag();
}
// Bind the data efficiently with the holder.
holder.idtext.setText(String.valueOf(mlist.get(position).getId()));
holder.nametext.setText(mlist.get(position).getName());
if (mlist.get(position).getFlag() == 0)
holder.flagtext.setText(context
.getString(R.string.chazhuo_kongxian));
else if (mlist.get(position).getFlag() == 1) {
holder.flagtext.setText(context
.getString(R.string.chazhuo_youren));
} else {
holder.flagtext.setText(context
.getString(R.string.chazhuo_unkown));
}
holder.remarktext.setText(mlist.get(position).getDescription());
return convertView;
}
static class ViewHolder {
TextView idtext;
TextView nametext;
TextView flagtext;
TextView remarktext;
}
}
}
這段XML 我在官方的demo上試了 沒問題。
<?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"
android:orientation="horizontal"
android:padding="5dp" >
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/textView3"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/textView4"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="wrap_content"
/>
</LinearLayout>
這個問題我搞了一兩天了 找不到哪裡的問題。求大神解決。
onCreate 時,case 1時:
TableListAdapter tblAdapter = new TableListAdapter(data, getApplicationContext());
裡面的 getApplicationContext()
改成 ChaZhuoActivity.this
試試