如何在Listview中使用自定义对象的属性.如果我使用字符串列表实现ArrayAdapter,它在Listview中显示正常,但是当我使用自定义对象列表时,它只输出内存地址.
我到目前为止的代码:
ArrayList<CustomObject> allObjects = new ArrayList<>();
allObjects.add("title", "http://url.com"));
ArrayAdapter<NewsObject> adapter = new ArrayAdapter<NewsObject>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, allNews);
// Assign adapter to ListView
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Uri uri = Uri.parse( "http://www.google.com" );
startActivity(new Intent(Intent.ACTION_VIEW, uri));
}
});
Run Code Online (Sandbox Code Playgroud)
这里有一个类似的问题,但这不是我需要的,因为我只需要在列表视图中显示标题,然后点击提取网址.
我从这个活动开始:
adapter = new ItemAdapter(Items.this, items, totals);
setListAdapter(adapter);
Run Code Online (Sandbox Code Playgroud)
现在这里是 ItemAdapter:
public class ItemAdapter extends ArrayAdapter<String> {
private final List<String> items;
private final List<String> totals;
private final Context context;
public ItemAdapter(Context context, List<String> items,
List<String> totals) {
super(context, R.layout.item_row_layout, items);
this.context = context;
this.items = items;
this.totals = totals;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater
.inflate(R.layout.item_row_layout, parent, false);
TextView t1 = (TextView) rowView.findViewById(R.id.itemName);
TextView t2 = …Run Code Online (Sandbox Code Playgroud)