使用自定义适配器时文本不显示的ListView了 [英] Text not showing up in ListView when using a custom adapter

查看:163
本文介绍了使用自定义适配器时文本不显示的ListView了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某些原因,我不能让文字在我的列表视图中显示出来。当我运行下面的示例中,两个空白列表元素出现,没有文字。此外,调试输出显示,在TextView的文本的价值是正确的。这是我的主要活动:

 进口的java.util.ArrayList;进口android.app.ListActivity;
进口android.content.Context;
进口android.os.Bundle;
进口android.util.Log;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.ArrayAdapter;
进口android.widget.TextView;公共类MyListActivity扩展ListActivity {    私有静态最后弦乐TAG =MyListActivity;    / **当第一次创建活动调用。 * /
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);        ArrayList的<串GT;值=新的ArrayList<串GT;();        values​​.add(测试1);
        values​​.add(Test2的);        TimelineAdapter适配器=新TimelineAdapter(
                this.getApplicationContext(),价值);
        setListAdapter(适配器);        Log.d(TAG,完成的onCreate);
    }    私有类TimelineAdapter扩展ArrayAdapter<串GT; {        ArrayList的<串GT;项目;        公共TimelineAdapter(上下文的背景下,ArrayList的<串GT;的值){
            超(背景下,R.layout.row,R.id.message,价值观);
            this.items =值;
        }        @覆盖
        公共查看getView(INT位置,查看convertView,父母的ViewGroup){            Log.d(TAG,进入getView);            //对于未来的性能优化
            查看查看= convertView;            //充气视图
            如果(查看== NULL){
                LayoutInflater吹气=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                鉴于= inflater.inflate(R.layout.row,父母,假);
                Log.d(TAG,查看虚增);
            }            //获取当前值
            字符串值= items.get(位置);
            Log.d(TAG,怒吼:+值);            //设置TextView的文本
            如果(值!= NULL){
                TextView的消息=(TextView中)view.findViewById(R.id.message);                message.setText(值);
                Log.d(TAG(字符串)message.getText());
                Log.d(TAG的TextView文本集);
            }
            返回视图。
        }    }
}

这是我的row.xml

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =机器人:ATTR /列表preferredItemHeight
    机器人:填充=6DIP>    <的TextView
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =0dip
        机器人:ID =@ + ID /消息
    />< / LinearLayout中>


解决方案

我不明白的setContentView(..),也许这就是问题?

For some reason, I can't get text to show up in my list view. When I run the following example, two blank list elements appear with no text. Also, the debugging output shows that the value of the text in the TextView is correct. Here is my main activity:

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class MyListActivity extends ListActivity {

    private static final String TAG = "MyListActivity";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ArrayList<String> values = new ArrayList<String>();

        values.add("Test1");
        values.add("Test2");

        TimelineAdapter adapter = new TimelineAdapter(
                this.getApplicationContext(), values);
        setListAdapter(adapter);

        Log.d(TAG, "Finished onCreate");
    }

    private class TimelineAdapter extends ArrayAdapter<String> {

        ArrayList<String> items;

        public TimelineAdapter(Context context, ArrayList<String> values) {
            super(context, R.layout.row, R.id.message, values);
            this.items = values;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            Log.d(TAG, "Entered getView");

            // For future performance optimization
            View view = convertView;

            // Inflate the view
            if (view == null) {
                LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = inflater.inflate(R.layout.row, parent, false);
                Log.d(TAG, "View inflated");
            }

            // Get current value
            String value = items.get(position);
            Log.d(TAG, "Shout: " + value);

            // Set TextView text
            if (value != null) {
                TextView message = (TextView) view.findViewById(R.id.message);

                message.setText(value);
                Log.d(TAG, (String) message.getText());
                Log.d(TAG, "Textview text set");
            }
            return view;
        }

    }
}

And here is my row.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="?android:attr/listPreferredItemHeight"
    android:padding="6dip">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:id="@+id/message"
    />

</LinearLayout>

解决方案

I dont see setContentView(..), maybe thats the problem?

这篇关于使用自定义适配器时文本不显示的ListView了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆