如何在ListView的每一行中显示多个TextViews? [英] How to display multiple number of TextViews inside each row in ListView?
问题描述
我正在创建一个帮助"页面,其中包含一系列问题和答案.这些问题和答案具有不同的样式.这是xml文件,它描述了问题&的布局.答案集:
I am creating a Help page in which I have a set of questions and answers. These questions and answers have different styles. Here is the xml file, which describes the layout of a question & answer set:
<?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="wrap_content"
android:orientation="vertical"
android:layout_gravity="center">
<TextView
android:text="@string/Help_first_question"
android:id="@+id/text1"
android:padding="5dip"
android:background="#e0f3ff"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:id="@+id/panel1"
android:visibility="gone"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_margin="2dip"
android:text="@string/Help_first_answer"
android:padding="5dip"
android:background="#FFFFFF"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
我想在listView
中显示多个问题和答案,其中每一行都包含一组问题和答案.我的listview
看起来像:
I want to display multiple questions and answers within a listView
, whereby each row contains a set of the question and the answer. My listview
looks like :
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView>
所以它看起来像:
first row : Q
A
second row : Q
A
third row : Q
A
实现此目标的最佳方法是什么?
What is the best approach for achieving this?
推荐答案
您需要实现自定义 ListAdapter 实现所有抽象方法.
You need to implement a custom ListAdapter implementing all of the abstract methods.
让我们创建一个QuestionAndAnswerListAdapter
,您可以通过在onCreate
中进行设置来创建ListView
:
Let's create a QuestionAndAnswerListAdapter
, which you can make your ListView
by setting it up in onCreate
:
public void onCreate(Bundle savedInstanceState) {
super(savedInstanceState);
setContentView(R.layout.listview);
QuestionsAndAnswersListAdapter adapter = new QuestionsAndAnswersListAdapter(data);
ListView listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(adapter);
}
适配器本身看起来像这样:
The adapter itself would look something like this:
public QuestionsAndAnswersListAdapter implements ListAdapter {
private QuestionAndAnswer[] data;
public QuestionsAndAnswersListAdapter(QuestionAndAnswer[] data) {
this.data = data;
}
public View getView(int position, View view, ViewGroup parent) {
if(view == null) {
//Only creates new view when recycling isn't possible
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.question_and_answer_list_item, null);
}
QuestionAndAnswer thisQA = data[position];
TextView questionView = (TextView) view.findViewById(R.id.text1);
questionView.setText(thisQA.question);
TextView answerView = (TextView) view.findViewById(R.id.answer);
answerView.setText(thisQA.answer);
return view;
}
// ...
}
getView
实际上是正确的中心方法.您需要实现以实现ListAdapter
接口的其余方法非常简单.检查参考以了解它们的确切含义.
getView
is really the central method to get right. The rest of the methods you need to implement to live up to the ListAdapter
interface are pretty straight-forward. Check the reference to see exactly what they are.
这篇关于如何在ListView的每一行中显示多个TextViews?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!