使用数组列表的Andr​​oid微调数据绑定数组、绑定、数据、列表

由网友(等不到的离人)分享简介:我有一个这样的数组列表:I have a array list like this:private ArrayList Artist_Result = new ArrayList();该位置类有两个属性: ID 和位置 This Location class has tw...

我有一个这样的数组列表:

I have a array list like this:

private ArrayList<Locations> Artist_Result = new ArrayList<Location>();

该位置类有两个属性: ID 位置

This Location class has two properties: id and location.

我需要绑定我的的ArrayList 来一个微调。我已经尝试过这种方式:

I need to bind my ArrayList to a spinner. I have tried it this way:

Spinner s = (Spinner) findViewById(R.id.SpinnerSpcial);
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, Artist_Result);
s.setAdapter(adapter);

然而,它示出了该对象的十六进制值。所以我觉得我必须设置显示文本和值的微调控制器。请能有人帮助我。我需要这个非常迫切。

However, it shows the object's hexadecimal value. So I think I have to set display the text and value for that spinner controller. Please can some one help me. I need this very urgently.

推荐答案

ArrayAdapter 尽量显示你的位置 -objects为字符串(这将导致十六进制值),通过调用Object.toString()-method.它的默认实现返回:

The ArrayAdapter tries to display your Location-objects as strings (which causes the Hex-values), by calling the Object.toString()-method. It's default implementation returns:

[...]组成的类的名称的字符串,它的对象   是一个实例,该符号字符'@',和无符号   十六进制重的对象的哈希code presentation。

[...] a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object.

为了让 ArrayAdadpter 显示一些项目列表中真正有用的,你可以覆盖的toString() - 方法返回一些有意义的:

To make the ArrayAdadpter show something actually useful in the item list, you can override the toString()-method to return something meaningful:

@Override
public String toString(){
  return "Something meaningful here...";
}

另一种方式做,这是,延长 BaseAdapter 和实施 SpinnerAdapter 创建自己的适配器,它知道,在的ArrayList 元素是对象,以及如何使用这些对象的属性。

Another way to do this is, to extend BaseAdapter and implement SpinnerAdapter to create your own Adapter, which knows that the elements in your ArrayList are objects and how to use the properties of those objects.

我是打了一下周围,我设法获得一些工作:

I was playing around a bit and I managed to get something to work:

public class Main extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Create and display a Spinner:
        Spinner s = new Spinner(this);
        AbsListView.LayoutParams params = new AbsListView.LayoutParams(
                ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT
        );
        this.setContentView(s, params);
        // fill the ArrayList:
        List<Guy> guys = new ArrayList<Guy>();
        guys.add(new Guy("Lukas", 18));
        guys.add(new Guy("Steve", 20));
        guys.add(new Guy("Forest", 50));
        MyAdapter adapter = new MyAdapter(guys);
        // apply the Adapter:
        s.setAdapter(adapter);
        // onClickListener:
        s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            /**
             * Called when a new item was selected (in the Spinner)
             */
            public void onItemSelected(AdapterView<?> parent,
                                       View view, int pos, long id) {
                Guy g = (Guy) parent.getItemAtPosition(pos);
                Toast.makeText(
                        getApplicationContext(),
                        g.getName()+" is "+g.getAge()+" years old.",
                        Toast.LENGTH_LONG
                ).show();
            }

            public void onNothingSelected(AdapterView parent) {
                // Do nothing.
            }
        });
    }

    /**
     * This is your own Adapter implementation which displays
     * the ArrayList of "Guy"-Objects.
     */
    private class MyAdapter extends BaseAdapter implements SpinnerAdapter {

        /**
         * The internal data (the ArrayList with the Objects).
         */
        private final List<Guy> data;

        public MyAdapter(List<Guy> data){
            this.data = data;
        }

        /**
         * Returns the Size of the ArrayList
         */
        @Override
        public int getCount() {
            return data.size();
        }

        /**
         * Returns one Element of the ArrayList
         * at the specified position.
         */
        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }
        /**
         * Returns the View that is shown when a element was
         * selected.
         */
        @Override
        public View getView(int position, View recycle, ViewGroup parent) {
            TextView text;
            if (recycle != null){
                // Re-use the recycled view here!
                text = (TextView) recycle;
            } else {
                // No recycled view, inflate the "original" from the platform:
                text = (TextView) getLayoutInflater().inflate(
                        android.R.layout.simple_dropdown_item_1line, parent, false
                );
            }
            text.setTextColor(Color.BLACK);
            text.setText(data.get(position).name);
            return text;
        }


    }

    /**
     * A simple class which holds some information-fields
     * about some Guys.
     */
    private class Guy{
        private final String name;
        private final int age;

        public Guy(String name, int age){
            this.name = name;
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public int getAge() {
            return age;
        }
    }
}

我完全注释的code,如果你有任何问题,请不要犹豫,问他们。

I fully commented the code, if you have any questions, don't hesitate to ask them.

阅读全文

相关推荐

最新文章