问题描述
- ListView列表文字无故变灰
-
我参考了官方的代码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
试试
解决方案二:
看你AndroidManifest.xml文件中是不是你按照创建工程向导默认设置了全局样式。导致字体是灰色。
解决方案三:
R.layout.chazhuo_list这个自定义布局里面看看是不是设置字体颜色了
解决方案四:
应该是你工程设置了style的Theme,你看看呢
解决方案五:
自己用baseadapter吧 可控性高
时间: 2024-08-20 02:50:58