Android 如何使用 ArrayAdapter<myClass>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2265661/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use ArrayAdapter<myClass>
提问by Sumit M Asok
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.
我在布局中创建了 row.xml,但不知道如何使用 ArrayAdapter 在 ListView 中同时显示 reason 和 long_val。
回答by Nikola Smiljani?
Implement custom adapter for your class:
为您的类实现自定义适配器:
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;
}
}
For those not very familiar with the Android framework, this is explained in better detail here: https://github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView.
对于那些不太熟悉 Android 框架的人,这里有更详细的解释:https: //github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView。
回答by Sumit M Asok
You could just add a toString()
method to MyClass, per http://developer.android.com/reference/android/widget/ArrayAdapter.html:
您可以toString()
根据http://developer.android.com/reference/android/widget/ArrayAdapter.html向 MyClass添加一个方法:
However the TextView is referenced, it will be filled with the toString() of each object in the array. You can add lists or arrays of custom objects. Override the toString() method of your objects to determine what text will be displayed for the item in the list.
然而,TextView 被引用,它将用数组中每个对象的 toString() 填充。您可以添加自定义对象的列表或数组。覆盖对象的 toString() 方法以确定将为列表中的项目显示什么文本。
class MyClass {
@Override
public String toString() {
return "Hello, world.";
}
}
回答by cesards
I think this is the best approach. Using generic ArrayAdapter class and extends your own Object adapter is as simple as follows:
我认为这是最好的方法。使用通用 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);
}
}
}
and here your adapter (example):
这里是您的适配器(示例):
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());
}
}
and finally, how to initialize it:
最后,如何初始化它:
ArrayList<Size> sizes = getArguments().getParcelableArrayList(Constants.ARG_PRODUCT_SIZES);
SizeArrayAdapter sizeArrayAdapter = new SizeArrayAdapter(getActivity(), sizes);
listView.setAdapter(sizeArrayAdapter);
I've created a Gist with TextView layout gravity customizable ArrayAdapter:
我创建了一个带有 TextView 布局重力可定制 ArrayAdapter 的 Gist:
回答by Klarth
Subclass the ArrayAdapterand override the method getView()to return your own view that contains the contents that you want to display.
子类中的ArrayAdapter和覆盖的方法getView()返回包含要显示的内容你自己的看法。
回答by Francois Dermu
Here's a quick and dirty example of how to use an ArrayAdapter if you don't want to bother yourself with extending the mother class:
如果您不想为扩展母类而烦恼,这是一个关于如何使用 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!");
}
});
}
}