适配器如何将带有对象的数组中的其他参数发送到listview?

时间:2021-05-18 22:14:10

I must admit that theory of adapters is not completely clear to me. In my app, I created ListView with parking places numbers and want to change a color of text based on their availability. ListView items get numbers from the array with "Dock" Objects, where every DockPlace has ID (number) and isAvailabile Status. My adapter can take numbers from Dock Object and put it in ListView. It also can change text colour, but I don't have any idea how can I get other parameters about objects.

我必须承认适配器理论对我来说并不完全清楚。在我的应用程序中,我创建了ListView与停车位数字,并希望根据其可用性更改文本的颜色。 ListView项目使用“Dock”对象从数组中获取数字,其中每个DockPlace都有ID(数字)和isAvailabile状态。我的适配器可以从Dock Object获取数字并将其放在ListView中。它也可以改变文本颜色,但我不知道如何获得有关对象的其他参数。

Adapter:

ListView docksList = (ListView) findViewById(R.id.docksList);
    ArrayAdapter<Dock> listAdapter = new ArrayAdapter<Dock>(
            this,
            android.R.layout.simple_list_item_1,
            Dock.docks //Dock - class name. docks - name of array with Dock Objects
    ) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = super.getView(position, convertView, parent);
            TextView text = (TextView) view.findViewById(android.R.id.text1);

            //in this place could be IF statement which check status of dock and can change text color depends on its

            text.setTextColor(Color.BLUE);

            return view;
        }
    };

Dock:

public static final Dock[] docks = {
    new Dock(1, true),
    new Dock(2, true),
    new Dock(3, false),
};
private Dock(int number, boolean isAvailable) {
    this.number = number;
    this.isAvailable = isAvailable;
    }
public int getNumber() {
    return number;
}
public boolean getStatus() {
    return isAvailable;
}
public String toString() {
    String numberStr = "Dock no "+ String.valueOf(this.number);
    return numberStr;
}

2 个解决方案

#1


0  

ArrayAdapter is just one concrete implementation of the ListAdapter interface; there's also BaseAdapter and anything you'd like to implement yourself.

ArrayAdapter只是ListAdapter接口的一个具体实现;还有BaseAdapter和你想要自己实现的任何东西。

ArrayAdapter does a pretty good job of hiding all the hard work it's doing, but in this case some of what's being hidden from you is exactly what you need.

ArrayAdapter可以很好地隐藏它所做的所有艰苦工作,但在这种情况下,隐藏在你身上的一些东西正是你所需要的。

When you construct your adapter with this code:

使用以下代码构造适配器时:

ArrayAdapter<Dock> listAdapter = new ArrayAdapter<Dock>(
        this,
        android.R.layout.simple_list_item_1,
        Dock.docks //Dock - class name. docks - name of array with Dock Objects
)

the important thing to understand is that your adapter is going to hold on to Dock.docks as its own private field (named mObjects) and that there are ways you can access this field in your own code.

要理解的重要一点是,你的适配器将把Dock.docks作为自己的私有字段(名为mObjects),并且有一些方法可以在你自己的代码中访问这个字段。

You can add values to the adapter using add(), addAll(), insert(), etc. You can remove objects using remove() or clear(). And the one that's relevant to you: you can access the object at a given position by using getItem().

您可以使用add(),addAll(),insert()等向适配器添加值。您可以使用remove()或clear()删除对象。与您相关的那个:您可以使用getItem()访问给定位置的对象。

Since the position is available in the getView() method, this means you can access the "current" Dock object quite easily. Perhaps you could use this code:

由于该位置在getView()方法中可用,这意味着您可以非常轻松地访问“当前”Dock对象。也许你可以使用这段代码:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
        TextView text = (TextView) view.findViewById(android.R.id.text1);

        if (getItem(position).getStatus()) {
            text.setTextColor(Color.BLUE);
        }
        else {
            text.setTextColor(Color.BLACK);
        }

        return view;
    }

#2


0  

You can use getItem(position) function of ArrayAdapter class for getting instance of dock object at particular position.

您可以使用ArrayAdapter类的getItem(position)函数来获取特定位置的dock对象的实例。

   public View getView(int position, View convertView, ViewGroup parent) {
       Dock dock=  getItem(position);   
       // do your stuff
   }

#1


0  

ArrayAdapter is just one concrete implementation of the ListAdapter interface; there's also BaseAdapter and anything you'd like to implement yourself.

ArrayAdapter只是ListAdapter接口的一个具体实现;还有BaseAdapter和你想要自己实现的任何东西。

ArrayAdapter does a pretty good job of hiding all the hard work it's doing, but in this case some of what's being hidden from you is exactly what you need.

ArrayAdapter可以很好地隐藏它所做的所有艰苦工作,但在这种情况下,隐藏在你身上的一些东西正是你所需要的。

When you construct your adapter with this code:

使用以下代码构造适配器时:

ArrayAdapter<Dock> listAdapter = new ArrayAdapter<Dock>(
        this,
        android.R.layout.simple_list_item_1,
        Dock.docks //Dock - class name. docks - name of array with Dock Objects
)

the important thing to understand is that your adapter is going to hold on to Dock.docks as its own private field (named mObjects) and that there are ways you can access this field in your own code.

要理解的重要一点是,你的适配器将把Dock.docks作为自己的私有字段(名为mObjects),并且有一些方法可以在你自己的代码中访问这个字段。

You can add values to the adapter using add(), addAll(), insert(), etc. You can remove objects using remove() or clear(). And the one that's relevant to you: you can access the object at a given position by using getItem().

您可以使用add(),addAll(),insert()等向适配器添加值。您可以使用remove()或clear()删除对象。与您相关的那个:您可以使用getItem()访问给定位置的对象。

Since the position is available in the getView() method, this means you can access the "current" Dock object quite easily. Perhaps you could use this code:

由于该位置在getView()方法中可用,这意味着您可以非常轻松地访问“当前”Dock对象。也许你可以使用这段代码:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
        TextView text = (TextView) view.findViewById(android.R.id.text1);

        if (getItem(position).getStatus()) {
            text.setTextColor(Color.BLUE);
        }
        else {
            text.setTextColor(Color.BLACK);
        }

        return view;
    }

#2


0  

You can use getItem(position) function of ArrayAdapter class for getting instance of dock object at particular position.

您可以使用ArrayAdapter类的getItem(position)函数来获取特定位置的dock对象的实例。

   public View getView(int position, View convertView, ViewGroup parent) {
       Dock dock=  getItem(position);   
       // do your stuff
   }