Android 如何使用 simple_list_item_2 设置 ListView 的两行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11256563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set both lines of a ListView using simple_list_item_2?
提问by Andrew Rabon
So the following will create a ListView where the rows have their "primary" textview filled by the values array.
因此,以下将创建一个 ListView,其中行的“主要”文本视图由 values 数组填充。
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_2, android.R.id.text1, values);
Changing the third parameter to android.R.id.text2 sets the "secondary" textview. Is there any simple way to set both?
将第三个参数更改为 android.R.id.text2 设置“辅助”文本视图。有没有简单的方法来设置两者?
回答by winne2
This questions ranks high with Google but I consider the given answer to be way too complicated. As pointed out in otheranswers, the desired functionality can be achieved using ArrayAdapter with a very easy trick.
这个问题在谷歌排名很高,但我认为给出的答案太复杂了。正如其他答案中所指出的,使用 ArrayAdapter 可以通过一个非常简单的技巧来实现所需的功能。
You can override the getView method of the ArrayAdapter:
您可以覆盖 ArrayAdapter 的 getView 方法:
ArrayAdapter adapter = new ArrayAdapter(context, android.R.layout.simple_list_item_2, android.R.id.text1, list) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView text1 = (TextView) view.findViewById(android.R.id.text1);
TextView text2 = (TextView) view.findViewById(android.R.id.text2);
text1.setText(persons.get(position).getName());
text2.setText(persons.get(position).getAge());
return view;
}
};
If you didn't notice: the trick is to supply android.R.id.text1
as (principally unneccessary) parameter, otherwise the call to super
will cause an exception.
如果您没有注意到:诀窍是提供android.R.id.text1
as(主要是不必要的)参数,否则调用super
将导致异常。
Also, this solution does not make use of TwoLineListItem
, which was deprecated in API 17.
此外,此解决方案不使用TwoLineListItem
API 17 中已弃用的 。
回答by Vipul Shah
AFAIK simple_list_item_2 contains a TwoLineListItem containing two TextViews. ArrayAdapter is not going to work here,you'll either have to create a custom adapter or use one that supports it like SimpleCursorAdapter.
AFAIK simple_list_item_2 包含一个包含两个 TextView 的 TwoLineListItem。ArrayAdapter 在这里不起作用,您要么必须创建自定义适配器,要么使用支持它的适配器,如 SimpleCursorAdapter。
ListAdapter adapter = new SimpleCursorAdapter(
this,
android.R.layout.simple_list_item_2,
mCursor, // Pass in the cursor to bind to.
new String[] {People.NAME, People.COMPANY}, // Array of cursor columns to bind to.
new int[] {android.R.id.text1, android.R.id.text2}); // Parallel array of which template objects to bind to those columns.
// Bind to our new adapter.
setListAdapter(adapter);
Or if you dont want SimpleCursorAdapter You will have to create Custom ArrayAdapter or BaseAdapter
或者如果你不想要 SimpleCursorAdapter 你将不得不创建自定义 ArrayAdapter 或 BaseAdapter
Create a custom ArrayAdapter,apply the object(Having two items) array to the custom adapter, and feed it to getListView.setAdapter.
创建自定义 ArrayAdapter,将对象(有两个项目)数组应用于自定义适配器,并将其提供给 getListView.setAdapter。
Override the ArrayAdapter's getView method to apply your name strings to TextViews in your custom list row view.
覆盖 ArrayAdapter 的 getView 方法以将您的名称字符串应用于自定义列表行视图中的 TextView。
Following Snippet will help you.
遵循 Snippet 将对您有所帮助。
SampleActivity.java
示例活动.java
package org.sample;
import java.util.ArrayList;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.TwoLineListItem;
public class SampleActivity extends ListActivity {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
Person person;
ArrayList<Person> persons = new ArrayList<Person>();
person = new Person();
person.setName("Vipul");
person.setAge(20);
persons.add(person);
person = new Person();
person.setName("Anil");
person.setAge(22);
persons.add(person);
setListAdapter(new MyAdapter(this, persons));
}
}
Person.java
人.java
class Person {
String name;
int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
MyAdapter.java
我的适配器
class MyAdapter extends BaseAdapter {
private Context context;
private ArrayList<Person> persons;
public MyAdapter(Context context, ArrayList<Person> persons) {
this.context = context;
this.persons = persons;
}
@Override
public int getCount() {
return persons.size();
}
@Override
public Object getItem(int position) {
return persons.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TwoLineListItem twoLineListItem;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
twoLineListItem = (TwoLineListItem) inflater.inflate(
android.R.layout.simple_list_item_2, null);
} else {
twoLineListItem = (TwoLineListItem) convertView;
}
TextView text1 = twoLineListItem.getText1();
TextView text2 = twoLineListItem.getText2();
text1.setText(persons.get(position).getName());
text2.setText("" + persons.get(position).getAge());
return twoLineListItem;
}
}
回答by Shan Xeeshi
hope this help you
希望这对你有帮助
ArrayAdapter<CustomObject> adapter = new ArrayAdapter<CustomObject> (_context, android.R.layout.simple_list_item_2, android.R.id.text1, listCustomObject) {
@Override
public View getView(int position,
View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView text1 = (TextView) view.findViewById(android.R.id.text1);
TextView text2 = (TextView) view.findViewById(android.R.id.text2);
text1.setText(listCustomObject.get(position).getText1());
text2.setText( listCustomObject.get(position).getText2() );
return view;
}
};
回答by Dheeresh Singh
can try this as well....
这个也可以试试....
adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_2,values){
@Override
public View getView(int position, View convertView, ViewGroup parent){
TwoLineListItem row = (TwoLineListItem)super.getView(position, convertView, parent);
row.getText1().setText(values.get***());
row.getText2().setText(values.get****());
return row;
}
};
setAdapter(adapter);