如何将多个textView添加到listView
How to add more than one textView to listView
我使用的是自定义适配器,但 getView 方法有问题。这是我的代码 -
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi = convertView;
if (vi == null)
vi = inflater.inflate(R.layout.list_item, null);
if(position==0){
TextView text1 = (TextView) vi.findViewById(R.id.text1);
text1.setText(data[position]);
}else if(position==1){
TextView text2 = (TextView) vi.findViewById(R.id.text2);
text2.setText(data[position]);
}
return vi;
}
这里是 XML 文件 -
<?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="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:background="@android:color/black"
android:textColor="@android:color/white"
android:id="@+id/text1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:background="@android:color/green"
android:textColor="@android:color/white"
android:id="@+id/text2" />
</LinearLayout>
实际上我的 if 命令正在运行,但另一个空白 textView 也随之出现。
假设如果 position==0 那么应该显示“@+id/text1”,但是“@+id/text2”也没有文本显示。
我只想显示一个文本视图,而不是另一个。怎么做?
position
是adapter中当前显示的item的索引,不是显示的是哪个TextView。
您的适配器的每一行都有两个 TextView,我假设您有一些字符串需要在两个视图中显示。
举个例子,试试这个。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
String item = String.valueOf(data[position]);
View v = convertView;
if (v == null) {
v = inflater.inflate(R.layout.list_item, null);
}
TextView text1 = (TextView) v.findViewById(R.id.text1);
TextView text2 = (TextView) v.findViewById(R.id.text2);
text1.setText("1: " + item);
text2.setText("2: " + item);
return v;
}
I want only one textview to be displayed, not the other one. How to do that?
听起来你不想在一个布局中使用两个 TextView...
如果您的问题是 "How to display more than one TextView in a ListView",那么您应该将多个值放入 data
数组中。
我使用的是自定义适配器,但 getView 方法有问题。这是我的代码 -
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi = convertView;
if (vi == null)
vi = inflater.inflate(R.layout.list_item, null);
if(position==0){
TextView text1 = (TextView) vi.findViewById(R.id.text1);
text1.setText(data[position]);
}else if(position==1){
TextView text2 = (TextView) vi.findViewById(R.id.text2);
text2.setText(data[position]);
}
return vi;
}
这里是 XML 文件 -
<?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="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:background="@android:color/black"
android:textColor="@android:color/white"
android:id="@+id/text1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:background="@android:color/green"
android:textColor="@android:color/white"
android:id="@+id/text2" />
</LinearLayout>
实际上我的 if 命令正在运行,但另一个空白 textView 也随之出现。 假设如果 position==0 那么应该显示“@+id/text1”,但是“@+id/text2”也没有文本显示。 我只想显示一个文本视图,而不是另一个。怎么做?
position
是adapter中当前显示的item的索引,不是显示的是哪个TextView。
您的适配器的每一行都有两个 TextView,我假设您有一些字符串需要在两个视图中显示。
举个例子,试试这个。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
String item = String.valueOf(data[position]);
View v = convertView;
if (v == null) {
v = inflater.inflate(R.layout.list_item, null);
}
TextView text1 = (TextView) v.findViewById(R.id.text1);
TextView text2 = (TextView) v.findViewById(R.id.text2);
text1.setText("1: " + item);
text2.setText("2: " + item);
return v;
}
I want only one textview to be displayed, not the other one. How to do that?
听起来你不想在一个布局中使用两个 TextView...
如果您的问题是 "How to display more than one TextView in a ListView",那么您应该将多个值放入 data
数组中。