android.R.layout.simple_list_item_1和android.R.layout.simple_list_item_2有什么区别

任何人都可以解释android中的arrayadapter中的android.R.layout.simple_list_item_1和android.R.layout.simple_list_item_2。

我知道android.R.layout.simple_list_item_1和android.R.layout.simple_list_item_2是在android本身定义的布局。

在android.R.layout.simple_list_item_1中只包含一个textview但android.R.layout.simple_list_item_2包含两个文本视图。

我想举例说明android.R.layout.simple_list_item_2 …如何在listview中用适配器显示两个文本视图。

我的代码是

package com.app.listview; import android.app.Activity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.ListView; public class ExampleListViewActivity extends Activity { private String[] nameArr = new String[]{"Arun","Anil","Ankit","Manoj"}; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ListView listView = (ListView)findViewById(R.id.lv); ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, android.R.id.text1, nameArr); listView.setAdapter(adapter); } } 

Solutions Collecting From Web of "android.R.layout.simple_list_item_1和android.R.layout.simple_list_item_2有什么区别"

区别在于以下内容。 simple_list_item_1仅包含TextView ,而simple_list_item_2RelativeLayout的子类中有两个。 这些都来自果冻豆。

simple_list_item_1管理

 < ?xml version="1.0" encoding="utf-8"?>   

simple_list_item_2

 < ?xml version="1.0" encoding="utf-8"?>      

根据ArrayAdapter的文档 :

默认情况下,此类期望提供的资源ID引用单个TextView。

因此,默认情况下, ArrayAdapter不会自动填充多个TextView实例。 但是,您可以覆盖getView()方法并填写R.layout.simple_list_item_2中显示的两个TextView

我发现这是你问题的最简单答案:

 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(person[position].getName()); text2.setText(person[position].getAge()); return view; } }; 

如果您没有注意到:诀窍是将android.R.id.text1作为(主要是不必要的)参数提供给ArrayAdapter,否则对super的调用将导致exception。

此外,此解决方案不需要TwoLineListItem或使用在API 17中弃用的TwoLineListItem

就像你注意到的那样,layout_1有一个textView,它是默认使用的。 layout_2有两个文本视图 – 另一个用作子文本。

但这里有诀窍 – 并非所有适配器都使用了子文本;)

我发现为任何事物和所有内容编写专用的自定义适配器更容易(不是说强制性的)……

例如,这是一个自定义适配器,它将使用此simple_list_item_2显示名称及其状态

不会是复制/粘贴代码,但你会通过一些调整来修复它…

  public class BuddyArrayAdapter extends ArrayAdapter { private static final String tag = "BuddyArrayAdapter"; private Context context; private TextView buddyName; private TextView buddyStatus; private List buddies = new ArrayList(); /** * The default constructor which is invoked to create the buddy array * adapter. * 

* The adapter is needed to 'translate' data into a viewable item / widget. * * @param context * the application context * @param objects * the backing array populated by Buddy objects to be displayed. * @see {@link ArrayAdapter} */ public BuddyArrayAdapter(Context context, int textViewResourceId, List objects) { super(context, textViewResourceId, objects); this.context = context; this.buddies = objects; Collections.sort(buddies); } /** * The method used for determining how many views are in this list or in * other words, how many views are managed by this adapter. * * @return the number of items this adapter controls. */ @Override public int getCount() { return this.buddies.size(); } /** * Get the data item associated with the specified position in the data set. * * @param index * Position of the item whose data we want within the adapter's * data set. * @return the Buddy object data at the specified position. */ @Override public Buddy getItem(int index) { if (index < = getCount()) //IndexOutOfBoundsException fix return this.buddies.get(index); return this.buddies.get(getCount() - 1); } /** * Get a View that displays the data at the specified position in the data * set. You can either create a View manually or inflate it from an XML * layout file. When the View is inflated, the parent View (GridView, * ListView...) will apply default layout parameters unless you use * inflate(int, android.view.ViewGroup, boolean) to specify a root view and * to prevent attachment to the root. *

* This method is used to generate views to be used in the ListView. This * the method that defines how data will look and be represented throughout * the UI. * * @param position * The position of the item that is being placed / The position * of the item within the adapter's data set of the item whose * view we want. *

* @param convertView * The old view to reuse, if possible. Note: You should check * that this view is non-null and of an appropriate type before * using. If it is not possible to convert this view to display * the correct data, this method can create a new view. * Heterogeneous lists can specify their number of view types, so * that this View is always of the right type (see * getViewTypeCount() and getItemViewType(int)) *

* @param parent * The parent that this view will eventually be attached to. * @return the view that defines how this Buddy object is represented in the * ListView / A View corresponding to the data at the specified * position. * * @see {@link BaseAdapter#getView(int, View, ViewGroup)} */ @Override public View getView(int position, View convertView, ViewGroup parent) { View row = convertView; if (row == null) { // ROW INFLATION LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); row = inflater.inflate(R.layout.simple_list_item_2, parent, false); } // Get item Buddy buddy = getItem(position); buddy.refresh(); buddyName = (TextView) row.findViewById(R.id.buddy_name); //change this to textField1 from simple_list_item_2 buddyName.setText(buddy.toString()); buddyStatus = (TextView) row.findViewById(R.id.buddy_mood); //change this to textField2 from simple_list_item_2 buddyStatus.setText(buddy.getMood()); // Log.d(tag, buddy.getIdentity()+"'s mood is "+buddyStatus.getText()); return row; }

因此,我建议您使用包含子文件的附加ArrayList扩展构造函数,然后使用em而不是buddy.getMood()调用。

最后,实例化此适配器并将其设置为listView的适配器。 瞧,你们两个文本都出现了;)

要进一步细化,请创建包含两个textView的XML文件,如下所示。

  < ?xml version="1.0" encoding="utf-8"?>    

而不是

  row = inflater.inflate(R.layout.simple_list_item_2, parent, false); 

  row = inflater.inflate(R.layout.buddy_list_item, parent, false); 

你去了,现在你知道如何使适配器使用自定义XML和listViews。

消息 – 是List

>

,title和data – 是map的键。

 SimpleAdapter adapter = new SimpleAdapter(this, messages, android.R.layout.simple_list_item_2, new String[] {"title", "data"}, new int[] {android.R.id.text1, android.R.id.text2, }); list.setAdapter(adapter); 

多数民众赞成你需要的。

ArrayAdapter只知道如何处理每行一个TextView。 如果你想要它处理更多,你需要通过inheritanceArrayAdapter并重写getView()方法来处理它。

根据您创建arrays的方式,可能还有另一个答案。

如果从数据库创建数组(你显示一个硬编码的字符串数组,但据我所知,这可能只是你的例子)并且你没有被其他因素约束到数组,你可能会考虑使用一个CursorAdapter因为它们已被设置为处理多个TextView而无需子类化适配器,它将节省您将DB数据转换为数组的处理能力。

我也使用基本项目以编程方式完成了此操作:

 < ?xml version="1.0" encoding="utf-8"?>           

通过在主活动中将此图层添加到垂直线性布局

 ...    ... 

在活动中添加修改其内容的项目的代码是:

 public void listViewTailer(int rootId, int itemId){ LinearLayout collector = (LinearLayout) findViewById(rootId); LinearLayout inflatedView; for(int i = 0; i < listFeeder.size(); i++){ inflatedView = (LinearLayout) View.inflate(this, itemId, null); TextView description = (TextView) inflatedView.findViewById(id.textViewBasicItem); description.setText(listFeeder.getGameList().get(i).getPrdDescription()); TextView platform = (TextView) inflatedView.findViewById(id.textViewPlatformItem); platform.setText(platform.getText() + "" + listFeeder.getGameList().get(i).getPrdPlatform()); TextView type = (TextView) inflatedView.findViewById(id.textViewTypeItem); type.setText(type.getText() + "" + listFeeder.getGameList().get(i).getPrdType()); TextView date = (TextView) inflatedView.findViewById(id.textViewDateItem); date.setText(date.getText() + "" + listFeeder.getGameList().get(i).getPrdDateAvailability()); TextView price = (TextView) inflatedView.findViewById(id.textViewPriceItem); price.setText(listFeeder.getGameList().get(i).getPrdPrice() + " "); collector.addView(inflatedView); ImageView imageView = (ImageView) inflatedView.findViewById(id.imageViewBasicItem); imageView.setImageResource(listFeeder.getGameList().get(i).getPrdImage()); } } 

其中rootId是收集器布局,而itemId是添加到垂直线性布局的基本项。

希望这可以提供帮助。