简体   繁体   English

如何使用ArrayAdapter <myClass>

[英]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 分类: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。

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

You could just add a toString() method to MyClass, per http://developer.android.com/reference/android/widget/ArrayAdapter.html : 您可以根据http://developer.android.com/reference/android/widget/ArrayAdapter.html向MyClass添加toString()方法:

However the TextView is referenced, it will be filled with the toString() of each object in the array. 但是引用了TextView,它将填充数组中每个对象的toString()。 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. 覆盖对象的toString()方法,以确定将为列表中的项显示的文本。

class MyClass {

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

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:

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

ArrayAdapter进行子类化并覆盖方法getView()以返回包含要显示的内容的自己的视图。

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!");
            }
        });
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM