文本未出现在我的 ListView 中
我有一个正在填充的 ListView。我已检查数据源的内容,它包含有效数据。不幸的是,行数是正确的,但我想要显示的文本没有出现。
以下是我设置 ListView 的方法:
private void setupUI() {
setContentView(R.layout.streamactivity);
this.streamListView = (ListView) findViewById(R.id.streamListView);
this.streamAdapter = new StreamAdapter(this, R.layout.streamactivityrow, messages);
streamListView.setAdapter(this.streamAdapter);
}
这是我的适配器:
private class StreamAdapter extends ArrayAdapter<STMessage> {
private ArrayList<STMessage> messages;
public StreamAdapter(Context context, int textViewResourceId, ArrayList<STMessage> messages) {
super(context, textViewResourceId, messages);
this.messages = messages;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.streamactivityrow, null);
}
STMessage aMessage = messages.get(position);
if (aMessage != null) {
TextView titleText = (TextView) v.findViewById(R.id.toptext);
TextView descriptionText = (TextView) v.findViewById(R.id.bottomtext);
if (titleText != null) {
titleText.setText(aMessage.getBody());
}
if (descriptionText != null) {
descriptionText.setText(aMessage.getFor_user_login());
}
}
return v;
}
}
这是列表中每一行的 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">
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="fill_parent">
<TextView
android:id="@+id/toptext"
android:layout_width="fill_parent"
android:layout_weight="1"
android:gravity="center_vertical" android:layout_height="0dip" android:singleLine="true" android:textSize="15sp" android:textStyle="bold" android:ellipsize="end" android:shadowColor="#000000" android:shadowDy="1.0" android:shadowRadius="1.0" android:textColor="#FFFFFF"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:id="@+id/bottomtext"
android:singleLine="true"
android:ellipsize="marquee"
android:textSize="15sp" android:shadowColor="#000000" android:shadowDy="1.0" android:shadowRadius="1.0" android:textColor="#FFFFFF"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/dateText" android:shadowColor="#000000" android:shadowDy="1.0" android:shadowRadius="1.0" android:textColor="#E2E2E2"></TextView></LinearLayout>
</LinearLayout>
这是带有 ListView 的 StreamActivity 的 XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/streamListView"></ListView>
</LinearLayout>
I have a ListView that is being populated. I have checked the contents of the datasource and it contains valid data. Unfortunately, the number of rows is correct, but the text I want to display is not appearing.
Here is how I am setting up the ListView:
private void setupUI() {
setContentView(R.layout.streamactivity);
this.streamListView = (ListView) findViewById(R.id.streamListView);
this.streamAdapter = new StreamAdapter(this, R.layout.streamactivityrow, messages);
streamListView.setAdapter(this.streamAdapter);
}
Here is my Adapter:
private class StreamAdapter extends ArrayAdapter<STMessage> {
private ArrayList<STMessage> messages;
public StreamAdapter(Context context, int textViewResourceId, ArrayList<STMessage> messages) {
super(context, textViewResourceId, messages);
this.messages = messages;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.streamactivityrow, null);
}
STMessage aMessage = messages.get(position);
if (aMessage != null) {
TextView titleText = (TextView) v.findViewById(R.id.toptext);
TextView descriptionText = (TextView) v.findViewById(R.id.bottomtext);
if (titleText != null) {
titleText.setText(aMessage.getBody());
}
if (descriptionText != null) {
descriptionText.setText(aMessage.getFor_user_login());
}
}
return v;
}
}
Here is the XML for each row in the list:
<?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">
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="fill_parent">
<TextView
android:id="@+id/toptext"
android:layout_width="fill_parent"
android:layout_weight="1"
android:gravity="center_vertical" android:layout_height="0dip" android:singleLine="true" android:textSize="15sp" android:textStyle="bold" android:ellipsize="end" android:shadowColor="#000000" android:shadowDy="1.0" android:shadowRadius="1.0" android:textColor="#FFFFFF"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:id="@+id/bottomtext"
android:singleLine="true"
android:ellipsize="marquee"
android:textSize="15sp" android:shadowColor="#000000" android:shadowDy="1.0" android:shadowRadius="1.0" android:textColor="#FFFFFF"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/dateText" android:shadowColor="#000000" android:shadowDy="1.0" android:shadowRadius="1.0" android:textColor="#E2E2E2"></TextView></LinearLayout>
</LinearLayout>
Here is the XML for the StreamActivity with the ListView:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/streamListView"></ListView>
</LinearLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请检查您的 XML,某些值为 0。
Please review your XML, some values are 0.
罪魁祸首是:
应该是
fill_parent
。The culprit was:
Should be
fill_parent
.