gpt4 book ai didi

android - ListView 中的多个数组

转载 作者:行者123 更新时间:2023-11-29 18:16:27 25 4
gpt4 key购买 nike

对于我的 ListView ,我希望能够在其中显示多个 TextView ,每个 TextView 显示与其关联的特定数组的项目。现在是我所拥有的:

public class GreatestHits extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, Array1));

ListView lv = getListView();
lv.setTextFilterEnabled(true);

lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String text = (String) ((TextView)view).getText();
}

});

}
static final String[] Array1 = new String[] {"Item1", "Item2"};
static final String[] Array2 = new String[] {"Item1", "Item2"};
static final String[] Array3 = new String[] {"Item1", "Item2"};

和 R.layout.list_item 的 xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp"
android:id= "@+id/textView1"
>

</TextView>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp"
android:id= "@+id/textView2"
>
</TextView>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp"
android:id= "@+id/textView3"
>
</TextView>

如何将数组 2 和数组 3 放入代码中,以便它可以显示在 listview lv 的 TextView 中?

最佳答案

ArrayAdapter 只能在一个 TextView 中显示来自一个数组的数据。您必须通过自己扩展 BaseAdapter 来自己提供此功能。我目前无法编译 android 代码,因此以下代码未经测试...但它至少应该让您了解您可以做什么。

public class TripleArrayAdapter extends BaseAdapter {
private String[] array1, array2, array3;
private LayoutInflater inflater;

public TripleArrayAdapter(Context context, String[] a1, String[] a2, String[]a3) {
array1 = a1;
array2 = a2;
array3 = a3;
inflater =
(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
return array1.length;
}

@Override
public Object getItem(int position) {
return null;
}

@Override
public long getItemId(int position) {
return position;
}

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

if(currentView==null) {
currentView = inflater.inflate(R.layout.list_item, parent, false);
}

TextView tView = (TextView)currentView.findViewById(R.id.textView1);
tView.setText(array1[position]);

tView = (TextView)currentView.findViewById(R.id.textView2);
tView.setText(array2[position]);

tView = (TextView)currentView.findViewById(R.id.textView3);
tView.setText(array3[position]);

return currentView;
}
}

然后,要将此适配器用作您的 ListActivity 的适配器,您只需说

setListAdapter(new TripleArrayAdapter(this, Array1, Array2, Array3));

关于android - ListView 中的多个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7828892/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com