如何使用ArrayAdapter <myClass>

ArrayList<MyClass> myList = new ArrayList<MyClass>(); ListView listView = (ListView) findViewById(R.id.list); ArrayAdapter<MyClass> adapter = new ArrayAdapter<MyClass>(this, R.layout.row, to, myList.); listView.setAdapter(adapter); 

类:MyClass

 class MyClass { public String reason; public long long_val; } 

我在布局中创build了row.xml,但不知道如何使用ArrayAdapter在ListView中显示reason和long_val。

为您的课程实施自定义适配器:

 public class MyClassAdapter extends ArrayAdapter<MyClass> { private static class ViewHolder { private TextView itemView; } public MyClassAdapter(Context context, int textViewResourceId, ArrayList<MyClass> items) { super(context, textViewResourceId, items); } public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = LayoutInflater.from(this.getContext()) .inflate(R.layout.listview_association, parent, false); viewHolder = new ViewHolder(); viewHolder.itemView = (TextView) convertView.findViewById(R.id.ItemView); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } MyClass item = getItem(position); if (item!= null) { // My layout has only one TextView // do whatever you want with your string and long viewHolder.itemView.setText(String.format("%s %d", item.reason, item.long_val)); } return convertView; } } 

对于那些不太熟悉Android框架的人来说,这里有更详细的解释: https : //github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView 。

你可以通过http://developer.android.com/reference/android/widget/ArrayAdapter.html添加一个;toString()方法到MyClass。

然而,TextView被引用,它将被填充数组中每个对象的toString()。 您可以添加自定义对象的列表或数组。 重写对象的toString()方法,以确定列表中的项目将显示哪些文本。

 class MyClass { @Override public String toString() { return "Hello, world."; } } 

我认为这是最好的方法。 使用genericsArrayAdapter类并扩展自己的Object适配器非常简单,如下所示:

 public abstract class GenericArrayAdapter<T> extends ArrayAdapter<T> { // Vars private LayoutInflater mInflater; public GenericArrayAdapter(Context context, ArrayList<T> objects) { super(context, 0, objects); init(context); } // Headers public abstract void drawText(TextView textView, T object); private void init(Context context) { this.mInflater = LayoutInflater.from(context); } @Override public View getView(int position, View convertView, ViewGroup parent) { final ViewHolder vh; if (convertView == null) { convertView = mInflater.inflate(android.R.layout.simple_list_item_1, parent, false); vh = new ViewHolder(convertView); convertView.setTag(vh); } else { vh = (ViewHolder) convertView.getTag(); } drawText(vh.textView, getItem(position)); return convertView; } static class ViewHolder { TextView textView; private ViewHolder(View rootView) { textView = (TextView) rootView.findViewById(android.R.id.text1); } } } 

在这里你的适配器(示例):

 public class SizeArrayAdapter extends GenericArrayAdapter<Size> { public SizeArrayAdapter(Context context, ArrayList<Size> objects) { super(context, objects); } @Override public void drawText(TextView textView, Size object) { textView.setText(object.getName()); } } 

最后,如何初始化它:

 ArrayList<Size> sizes = getArguments().getParcelableArrayList(Constants.ARG_PRODUCT_SIZES); SizeArrayAdapter sizeArrayAdapter = new SizeArrayAdapter(getActivity(), sizes); listView.setAdapter(sizeArrayAdapter); 

我已经用TextView布局创build了一个Gist可重定制的ArrayAdapter:

https://gist.github.com/m3n0R/8822803

子类ArrayAdapter并重写方法getView()返回您自己的视图,其中包含您要显示的内容。

这里有一个如何使用ArrayAdapter的快速和肮脏的例子,如果你不想打扰扩展母类:

 class MyClass extends Activity { private ArrayAdapter<String> mAdapter = null; @Override protected void onCreate(Bundle savedInstanceState) { mAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_dropdown_item_1line, android.R.id.text1); final ListView list = (ListView) findViewById(R.id.list); list.setAdapter(mAdapter); //Add Some Items in your list: for (int i = 1; i <= 10; i++) { mAdapter.add("Item " + i); } // And if you want selection feedback: list.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //Do whatever you want with the selected item Log.d(TAG, mAdapter.getItem(position) + " has been selected!"); } }); } }