我正在努力从定制列表中获取多个值。
感谢 getItem(int pos)
我可以从中获取“主要”值(value),但由于我为一行存储多个值(行表示 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
中获取值(value)的最后一次疲惫尝试。数据。
我不能因为喜欢它而以任何方式、形状或形式调用该函数。
这是我如何使用这个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
的ArrayAdapter
.
我尝试做 @Override
上getItem
也一样,但它只需要一个参数,即 position
,我对 Java 不够熟悉,无法让它为我工作。
知道我如何才能获得 imageId
或identifier
listView1
的每个行项的变量?
如果您需要list_single.xml
这是 listView1
的布局(至少,这是我对该文件的作用的解释),它是:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow>
<ImageView
android:id="@+id/img"
android:layout_width="50dp"
android:layout_height="50dp"/>
<TextView
android:id="@+id/identifier"
android:layout_width="wrap_content"
android:visibility="invisible"
android:layout_height="50dp" />
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="50dp" />
</TableRow>
</TableLayout>
最佳答案
在这种情况下,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]));
}
}
关于java - 从扩展 ArrayAdapter 的类中获取 ListView 自定义 friend ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41337505/