Подтвердить что ты не робот

В чем разница между 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 в arrayadapter в android.

Я знаю в android.R.layout.simple_list_item_1 и android.R.layout.simple_list_item_2 - это макет, который определяется в самом андроиде.

in android.R.layout.simple_list_item_1 содержит только одно текстовое представление, но android.R.layout.simple_list_item_2 содержит два текстовых представления.

Я хочу пример для android.R.layout.simple_list_item_2... как показать два текстовых вида в списке с адаптером.

мой код

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<String> adapter = new ArrayAdapter<String>(this,
                                                                android.R.layout.simple_list_item_1,
                                                                android.R.id.text1,
                                                                nameArr);
        listView.setAdapter(adapter);
    }
}
4b9b3361

Ответ 1

Разница заключается в следующем. simple_list_item_1 содержит только a TextView, тогда как simple_list_item_2 имеет два внутри подкласса RelativeLayout. Они взяты из желе Bean.

simple_list_item_1

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2006 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
    android:paddingRight="?android:attr/listPreferredItemPaddingRight"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"
/>

simple_list_item_2

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2006 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:mode="twoLine"
>

    <TextView android:id="@android:id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
    android:layout_marginLeft="?android:attr/listPreferredItemPaddingLeft"
    android:layout_marginTop="8dip"
        android:textAppearance="?android:attr/textAppearanceListItem"
    />

    <TextView android:id="@android:id/text2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@android:id/text1"
    android:layout_alignLeft="@android:id/text1"
        android:textAppearance="?android:attr/textAppearanceSmall"
    />

</TwoLineListItem>

В соответствии с docs для ArrayAdapter:

По умолчанию этот класс ожидает, что ссылки на предоставленные идентификаторы ресурса один TextView.

Таким образом, по умолчанию ArrayAdapter не заполняет автоматически несколько экземпляров TextView. Однако вы можете переопределить метод getView() и заполнить два TextView, которые отображаются в R.layout.simple_list_item_2

Ответ 2

Я нашел, что это самый простой ответ на ваш вопрос:

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 вызовет исключение.

Кроме того, для этого решения не требуется Inflater или использовать TwoLineListItem, который устарел в API 17.

Ответ 3

Как вы заметили, layout_1 имеет один textView и он используется по умолчанию. layout_2 имеет два текстовых вида - другой, используемый в качестве подтекста.

но здесь трюк - не все адаптеры используют подтекст один;)

Мне было проще (не собираюсь говорить обязательно) написать специально созданный пользовательский адаптер для чего угодно и всего...

Например, здесь пользовательский адаптер, который будет отображать имя и его статус, используя этот simple_list_item_2

Этот не будет копировать/вставлять код, но вы исправите его несколькими изменениями...

 public class BuddyArrayAdapter extends ArrayAdapter<Buddy>
 {

private static final String tag         = "BuddyArrayAdapter";
private Context             context;

private TextView            buddyName;
private TextView            buddyStatus;
private List<Buddy>         buddies     = new ArrayList<Buddy>();

/**
 * The default constructor which is invoked to create the buddy array
 * adapter.
 * <p>
 * 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}<T>
 */

public BuddyArrayAdapter(Context context, int textViewResourceId, List<Buddy> 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.
 * <p>
 * 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 data set of the item whose
 *            view we want.
 *            <p>
 * @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))
 *            <p>
 * @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()+" mood is "+buddyStatus.getText());



    return row;
}

Итак, я предлагаю вам развернуть конструктор с дополнительным ArrayList, который содержит подтексты, а затем использовать em вместо вызова buddy.getMood().

Наконец, создайте экземпляр этого адаптера и установите его как адаптер listView. Вуала, у тебя есть оба текста: wink.gif

Для дальнейшего уточнения создайте собственный XML файл, содержащий два текстовых элемента, например:

 <?xml version="1.0" encoding="utf-8"?>
 <com.skype.widget.CheckableLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<CheckedTextView
    android:id="@+id/buddy_name"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:checkMark="?android:attr/textCheckMark"
    android:gravity="center_vertical"
    android:paddingLeft="6dip"
    android:paddingRight="6dip"
    android:text="@string/buddy_name"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/buddy_mood"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/empty_string"
    android:layout_marginLeft="-350dp"
    android:layout_marginTop="16dp"
    android:gravity="center_vertical|bottom"
    android:textAppearance="?android:attr/textAppearanceSmall" />

и вместо

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

делать

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

Там вы идете, теперь вы знаете, как сделать адаптеры работать с пользовательскими XML и listViews.

Ответ 4

сообщения - это List<Map<String, String>>, title и data - это ключи карты.

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);

Это все, что вам нужно.

Ответ 5

ArrayAdapter знает, как обращаться с одним TextView в строке. Если вы хотите, чтобы он имел дело с больше, вам нужно справиться с этим самим путем подклассификации ArrayAdapter и переопределения метода getView().

В зависимости от того, как вы создаете свой массив, может быть и другой ответ.

Если массив создается из БД (вы показываете жестко закодированный массив строк, но это может быть только для вашего примера, насколько я знаю), и вы не ограничены массивом каким-либо другим фактором, вы может рассмотреть возможность использования CursorAdapter, поскольку они уже настроены для работы с несколькими TextViews без необходимости подклассификации адаптера, и это сэкономит вам вычислительную мощность для преобразования данных DB в массив.

Ответ 6

Я сделал это также программно, используя базовый элемент:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginLeft="3dp"
    android:id="@+id/linearLayoutBasicItem"
     >

    <ImageView
        android:id="@+id/imageViewBasicItem"
        android:layout_marginTop="3dp"
        android:layout_width="80dp"
        android:layout_height="100dp"
        android:src="@drawable/blockbreaker3"
        android:background="#b3b3b3"
         />
    <RelativeLayout 
        android:id="@+id/relativeLayoutInsideBasicItem"
        android:layout_width="fill_parent"
        android:layout_marginTop="3dp"
        android:layout_height="100dp"
        android:background="#b3b3b3"
        >
        <TextView
            android:id="@+id/textViewBasicItem"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Affronta livelli ancora più complessi che ti porteranno al di là di un semplice schermo pieno di mattoncini."
            android:textSize="10dp"
            android:textColor="#000000"
            android:gravity="top"
            android:ems="10" />

        <TextView
          android:id="@+id/textViewPlatformItem"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_below="@+id/textViewBasicItem"
          android:layout_marginTop="3dp"
          android:text="Platform: "
          android:textSize="8dp"
          android:textColor="#000000"
          android:gravity="top"
          android:ems="10" />
        <TextView
          android:id="@+id/textViewTypeItem"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_below="@+id/textViewPlatformItem"
          android:layout_marginTop="3dp"
          android:text="Genere: "
          android:textSize="8dp"
          android:textColor="#000000"
          android:gravity="top"
          android:ems="10" />
        <TextView
          android:id="@+id/textViewDateItem"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_below="@+id/textViewTypeItem"
          android:layout_marginTop="3dp"
          android:text="Data di lancio: "
          android:textSize="8dp"
          android:textColor="#000000"
          android:gravity="top"
          android:ems="10"
          android:layout_marginBottom="3dp"
           />
        <TextView
          android:id="@+id/textViewPriceItem"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentBottom="true"
          android:layout_alignParentRight="true"
          android:text="Gratis    "
          android:gravity="right"
          android:textSize="15dp"
          android:textColor="#0096ff"
          android:ems="10"
           />

    </RelativeLayout>

</LinearLayout>

добавив этот слой к вертикальной линейной компоновке в моем основном действии

...
<ScrollView 
        android:id="@+id/scrollViewStep1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"        
        android:layout_below="@+id/textViewStep1"
        android:layout_marginTop="35dp"
        android:layout_marginRight="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginBottom="32dp"
        android:background="#e8e8e8"
        android:orientation="vertical" >

        <LinearLayout 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:id="@+id/collector"
            ></LinearLayout>


    </ScrollView>
...

Код внутри действия, который добавляет элементы, изменяющие их содержимое:

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 - это базовый элемент, добавленный к вертикальной линейной компоновке.

Надеюсь, это поможет.