我有以下代码:
import android.app.ListActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
public class MainActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[][] from ={{"jonathan","guillaume","gerard"},{"fraise","patate","chocolat"}};
ArrayAdapter<String[][]> adapter = new ArrayAdapter<String[][]>(getListView().getContext(),android.R.layout.simple_list_item_1, from);
getListView().setAdapter(adapter);
}
}
XML如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="zathstudio.projetliste.MainActivity">
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
我试图从&#34;中获取#34;使用arrayAdapter在listView中显示的数组。如果数组是单维的,它可以工作,但我怎么能用2D数组呢?
答案 0 :(得分:0)
您需要创建一个继承自ArrayAdapter类的自定义ArrayAdapter。我发现这对于你自己的类中的对象的ArrayList而不是数组也是最容易的,但我假设两者都是可能的。我只用ArrayList结构实现了这个。
public class CustomArrayAdapter extends ArrayAdapter<String> {
private Context context;
private ArrayList<YourClass> objects;
//or with 2d array this might work:
private String [][] strings2d;
public CustomArrayAdapter (Context context, ArrayList<YourClass> objects) {
super(context, -1, objects);
this.context = context;
this.objects = objects;
}
public View getView(int position, View convertView, ViewGroup parent) {
//Get individual object to be displayed
YourClass object = this.objects.get(position);
//or with 2d array
String[] strings1d = this.strings2d[position];
String string1 = object.strings[1]; //Your class has array of strings as class var
String string2 = object.strings[2];
String string3 = object.strings[3];
//Inflate view and get (custom) layout for each item in list
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View customView = layoutInflater.inflate(R.layout.your_custom_list_item_layout, parent, false);
//Get textView to display each String
TextView textView1 = (TextView) customView.findViewById(R.id.text_field_1);
TextView textView2 = (TextView) customView.findViewById(R.id.text_field_2);
TextView textView3 = (TextView) customView.findViewById(R.id.text_field_2);
//Set text of each textView
textView1.setText(string1);
textView1.setText(string2);
textView1.setText(string3);
//Or with 2d arrays
textView1.setText(strings1d[0]);
textView1.setText(strings1d[1]);
textView1.setText(strings1d[2]);
return customView;
}
}
我没有对此进行测试,看它是否运行,显然你需要创建一个自定义类来保存字符串和自定义布局来显示ListView中的每个项目。