How to use 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);

Class: MyClass

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

I have created row.xml in layouts, but don't know how to show both reason and long_val in the ListView using ArrayAdapter.

147272 次浏览

子类化 数组适配器并重写方法 GetView ()以返回包含要显示的内容的自己的视图。

为类实现自定义适配器:

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

你可以在 MyClass 中添加一个 toString()方法,根据 http://developer.android.com/reference/android/widget/ArrayAdapter.html:

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

class MyClass {


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

这里有一个简单的例子,告诉你如何使用 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!");
}
});
}
}

我觉得这是最好的办法。使用通用 ArrayAdapter 类并扩展您自己的 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 布局创建了一个 Gist 引力可定制的 ArrayAdapter:

Https://gist.github.com/m3n0r/8822803