如何在android中将文本多个文本视图添加到列表视图项?
如何在android中向列表视图项目添加文本多个文本视图?请添加代码您必须通过自定义布局覆盖默认列表视图项目。 在如何在android中将文本多个文本视图添加到列表视图项?,android,Android,如何在android中向列表视图项目添加文本多个文本视图?请添加代码您必须通过自定义布局覆盖默认列表视图项目。 在onCreate()中使用: yourList = (ListView) findViewById(R.id.yourList); arrayAdapter = new MyClassAdapter(this,android.R.layout.simple_list_item_1, results); yourList.setAdapter(arrayAdapter)
onCreate()
中使用:
yourList = (ListView) findViewById(R.id.yourList);
arrayAdapter = new MyClassAdapter(this,android.R.layout.simple_list_item_1, results);
yourList.setAdapter(arrayAdapter);
在公共类中,MyClassAdapter扩展了ArrayAdapter{
覆盖以下内容:
public View getView(int position, View convertView, ViewGroup list) {
View element;
if (convertView == null) {
element = View.inflate(ctx, R.layout.yourlayout, null);
} else {
element = convertView;
}
///setup your listview element
return element;
}
在res/layout/yourlayout.xml文件中创建如下内容:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/txt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
在onCreate()
:arrayAdapter.setQ(2);
在公共类中,MyClassAdapter扩展了ArrayAdapter{
公共类MyClassAdapter扩展了ArrayAdapter{
私人int q=2;
公共void setQ(int num){
这个。q=num;
}
公共视图getView(int位置、视图转换视图、视图组列表){
视图组元素;
if(convertView==null){
元素=(视图组)视图。充气(ctx,R.layout.yourlayout,null);
}否则{
元素=(视图组)转换视图;
}
元素。removeAllViews();
对于(int i=0;i
为列表视图充气,并在列表项视图中添加所需数量的文本视图
下面是创建适配器布局的示例,如下所示
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/headerlayoutid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="Vertical">
</LinearLayout>
如果您需要许多文本视图,请在for循环中使用代码。这就足够了。无法理解您的问题。需要更多详细信息“请添加代码”-请添加您的。@MathaN………我有一个列表视图和一个名为Quantity的列表视图项。我需要根据Quantity值显示文本视图。如果Quantity=2,则需要显示2个文本视图查看答案,它可能适合您的问题。可能是重复的,我还没有得到解决方案?
public class MyClassAdapter extends ArrayAdapter<Long> {
private int q = 2;
public void setQ(int num){
this.q = num;
}
public View getView(int position, View convertView, ViewGroup list) {
ViewGroup element;
if (convertView == null) {
element = (ViewGroup) View.inflate(ctx, R.layout.yourlayout, null);
} else {
element = (ViewGroup) convertView;
}
element.removeAllViews();
for (int i = 0; i < q; i++){
TextView tv = new TextView(this);
tv.setText("blablabla");
element.addChild(tv);
}
///setup your listview element
return element;
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/headerlayoutid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="Vertical">
</LinearLayout>
final TextView t = new TextView(this);
t.setText("String value");
t.setTextSize(14);
//You can set gravity for the textview
t.setGravity(Gravity.CENTER_VERTICAL);
LinearLayout ll = (LinearLayout) findViewById(R.id.headerlayoutid);
ll.addView(t);