从扩展ArrayAdapter的类中获取ListView自定义朋友

时间:2016-12-27 00:11:01

标签: java android listview android-arrayadapter

我正在努力从定制列表中获取超过一个值 由于getItem(int pos),我可以从中获得“主要”值,但由于我为一行存储了多个值(行意味着CustomList中的每个TableView),我我希望能够获得我能够提取的标签的相应值。

我确信这比我要做的更简单,所以我希望之前有人这样做过。

我有CustomList班,

public class CustomList extends ArrayAdapter<String>{

    private final Activity context;
    private final String[] labels;
    private final Integer[] imageId;
    private final String[] identifier;
    public CustomList(Activity context,
                      String[] labels, Integer[] imageId, String[] id) {
        super(context, R.layout.list_single, labels);
        this.context = context;
        this.labels = labels;
        this.imageId = imageId;
        this.identifier = id;

    }
    @Override
    public View getView(int position, View view, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView= inflater.inflate(R.layout.list_single, null, true);

        TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
        TextView idTitle = (TextView) rowView.findViewById(R.id.identifier);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.img);

        txtTitle.setText(labels[position]);
        idTitle.setText(identifier[position]);
        imageView.setImageResource(imageId[position]);

        return rowView;
    }

    public String getKey(int position, int category) {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView= inflater.inflate(R.layout.list_single, null, true);

        TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
        TextView idTitle = (TextView) rowView.findViewById(R.id.identifier);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.img);

        if (category == 1) {
            return idTitle.getText().toString();
        } else if (category == 2) {
            return "url:();";
        } else {
            return this.getItem(position);
        }
    }
}

注意getKey最后一次尝试从此ListView/CustomList数据中获取值。 我不能因为爱而以任何方式,形状或形式称呼这个功能。

以下是我使用此CustomList

的方法
CustomList adapter = new
                    CustomList(MainActivity.this, urls, images, identifiers);
list=(ListView)findViewById(R.id.listView1);
list.setAdapter(adapter);

相当直接,现在稍后在代码中 - 更具体地说是在按钮上 - 我想访问存储在列表中的数据。
我通过执行以下操作来完成此操作:

(这是我遇到心灵弯曲的地方)

list=(ListView)findViewById(R.id.listView1);
ListView listView = (ListView)findViewById(R.id.listView1);
ListAdapter table = listView.getAdapter();

for(int i = 0; i < table.getCount(); i++){
    Object row = table.getItem(i);
    Log.i("Test", row.toString());
}

我得到的是CustomList.labels[i]值,这并不奇怪,因为我认为这会引用getItem的{​​{1}}。

我尝试在ArrayAdapter上执行@Override,但因为它只需要一个参数,即getItem,所以我对Java不太熟悉,无法使用它

任何想法如何从position的每个行项中获取imageIdidentifier个变量?

如果您需要listView1这是list_single.xml的布局(至少,这是我对此文件所做的解释),这里是:

listView1

2 个答案:

答案 0 :(得分:1)

在这种情况下,BaseAdapter是比ArrayAdapter更好的选择。

您可以在您的案例中创建包含3个字段的域模型,在适配器中定义此模型的数组,并从getItem返回它们。

示例实施: https://guides.codepath.com/android/Using-a-BaseAdapter-with-ListView

在你的情况下,它会是这样的

public class CustomListAdapter extends BaseAdapter {

    public class DataObject {
        private final String label;
        private final Integer imageId;
        private final String identifier;

        public DataObject() {}

        public DataObject(String label, Integer imageId, String identifier) {
            this.label = label;
            this.imageId = imageId;
            this.identifier = identifier;
        }

        public String getLabel() {
            return label;
        }

        public Integer getImageId() {
            return imageId;
        }

        public String getIdentifier() {
            return identifier;
        }
    }


    private Context context; //context
    private ArrayList<DataObject> items; //data source of the list adapter

    //public constructor
    public CustomListAdapter(Context context, ArrayList<DataObject> items) {
        this.context = context;
        this.items = items;
    }

    @Override
    public int getCount() {
        return items.size(); //returns total of items in the list
    }

    @Override
    public DataObject getItem(int position) {
        return items.get(position); //returns list item at the specified position
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // inflate the layout for each list row
        if (convertView == null) {
            convertView = LayoutInflater.from(context).
                    inflate(R.layout.list_single, parent, false);
        }

        // get current item to be displayed
        DataObject currentItem = getItem(position);

        // get the TextView for item name and item description
        TextView txtTitle = (TextView) convertView.findViewById(R.id.txt);
        TextView idTitle = (TextView) convertView.findViewById(R.id.identifier);
        ImageView imageView = (ImageView) convertView.findViewById(R.id.img);

        //sets the text for item name and item description from the current item object
        txtTitle.setText(currentItem.label);
        idTitle.setText(currentItem.identifier);
        imageView.setImageResource(currentItem.imageId);

        // returns the view for the current row
        return convertView;
    }
}

<强>更新

如果你仍想从3个数组构造适配器,你可以使用这样的附加构造函数。但也许可以更好地改变数据模型中的内容。

public CustomList(Activity context,
                  String[] labels, Integer[] imageId, String[] id) {
    this.context = context;
    this.items = new ArrayList(labels.length);

    for (int i = 0; i < labels.length; i++) {
        this.items.add(new DataObject(labels[i], imageId[i], id[i]));
    }
}

答案 1 :(得分:0)

Java是面向对象的。制作物品。

class DataObject {
    private final String label;
    private final Integer imageId;
    private final String identifier;

    public DataObject() {}
} 

使用public class CustomList extends ArrayAdapter<DataObject>{

您可以选择存储DataObject[]或更好List<DataObject>,但getItem现在可以返回整个对象,然后您可以从中获取数据。

您可以使用adapter.add将数据直接添加到适配器/列表视图