2011-01-13 3 views
0

Эй, я хочу, чтобы посмотреть ниже под последним, что было создано.Как сделать вид ниже

Это показывает, как это:

alt text

Я хочу сделать следующий вид шоу под последней, поэтому для добавления каждого новое зрения, это показывает ниже. Понимаю?

Вот мой код. Xml-файл и java-файл.

listitem.xml

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="?android:attr/listPreferredItemHeight" 
    android:id="@+id/linearId" 
    android:padding="6dip" 
    > 
    <ImageView 
     android:id="@+id/icon" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:layout_marginRight="6dip" 
     android:src="@drawable/icon" 
     /> 
    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="0dip" android:layout_weight="1" 
     android:layout_height="fill_parent" 
     > 
     <TextView 
      android:id="@+id/txt1" 
      android:layout_width="fill_parent" 
      android:layout_height="0dip" 
      android:layout_weight="1" 
      android:textSize="20sp" 
      android:gravity="center_vertical" 
      android:text="My Application" 
      /> 
     <TextView 
      android:id="@+id/txt2" 
      android:layout_width="fill_parent" 
      android:layout_height="0dip" 
      android:layout_weight="1" 
      android:singleLine="true" 
      android:ellipsize="marquee" 
      android:text="Simple application that shows how to use RelativeLayout" 
      android:textSize="10sp" 
      /> 
    </LinearLayout> 
</LinearLayout> 

RatedCalls.java

public class RatedCalls extends Activity { 

private static final String LOG_TAG = "RatedCalls"; 
private TableLayout table; 
private CallDataHelper cdh; 
private TableRow row; 
private TableRow row2; 

public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 

    setContentView(R.layout.listitem); 

    Log.i(LOG_TAG, "calling from onCreate()"); 

    cdh = new CallDataHelper(this); 

    startService(new Intent(this, RatedCallsService.class)); 
    Log.i(LOG_TAG, "Service called."); 
    Log.i(LOG_TAG, "before call fillList"); 

    List<String> ratedCalls = new ArrayList<String>(); 
    ratedCalls = this.cdh.selectTopCalls(); 

    LayoutInflater inflater = (LayoutInflater) this 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    LinearLayout ll = (LinearLayout) this.findViewById(R.id.linearId); 

    for (int i = 0; i < 1; i++) { 
     View item = inflater.inflate(R.layout.listitem, null); 

     TextView x = (TextView) item.findViewById(R.id.txt1); 
     x.setText(ratedCalls.get(0)); 

     TextView ht = (TextView) item.findViewById(R.id.txt2); 
     ht.setText(ratedCalls.get(1)); 

     ll.addView(item, ViewGroup.LayoutParams.WRAP_CONTENT); 


    } 
} 

ответ

0

Чтобы работать так, вам нужно создать два различных XML для каждого Inflater

В вашей папке макета, новый XML макет/main2.xml

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent"  
     android:layout_height="fill_parent">  
      <ImageView 
      android:id="@+id/icon" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginRight="6dip" 
      android:src="@drawable/icon" 
      /> 
      <TextView 
       android:id="@+id/txt1" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:textSize="20sp" 
       android:gravity="center_vertical" 
       android:text="My Application" 
       /> 
      <TextView 
       android:id="@+id/txt2" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:singleLine="true" 
       android:ellipsize="marquee" 
       android:text="Simple application that shows how to use RelativeLayout" 
       android:textSize="10sp" 
       />  
     </LinearLayout> 

Тогда ваш макет/main.xml

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/linearId" 
    android:orientation="vertical" 
    > 
    <TextView android:id="@+id/rowCount" android:layout_width="wrap_content" 
    android:layout_height="wrap_content" ></TextView> 
    <LinearLayout 
     android:id="@+id/linearId2" 
     android:layout_width="fill_parent" 
    android:layout_height="?android:attr/listPreferredItemHeight" 
    android:padding="6dip"> 
     </LinearLayout> 
</LinearLayout> 

Тогда вы можете установить вам активность

private int ELEMENTINEACHROW=2 ; 
    private int NOOFROW = 4; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     LinearLayout ll = (LinearLayout) this.findViewById(R.id.linearId); 
     for (int j = 0; j < NOOFROW; j++) { 
      View itemMain = inflater.inflate(R.layout.main, null, false); 
      TextView rowCount = (TextView) itemMain.findViewById(R.id.rowCount); 
      rowCount.setText("Row:"+(j+1)); 
      LinearLayout ll2 = (LinearLayout) itemMain.findViewById(R.id.linearId2); 
      for (int i = 0; i < ELEMENTINEACHROW; i++) { 
       View item = inflater.inflate(R.layout.main2, null, false); 

       TextView x = (TextView) item.findViewById(R.id.txt1); 
       x.setText("test"); 

       TextView ht = (TextView) item.findViewById(R.id.txt2); 
       ht.setText("good"); 

       ll2.addView(item, ViewGroup.LayoutParams.FILL_PARENT); 
      } 
      ll.addView(itemMain, ViewGroup.LayoutParams.FILL_PARENT); 
     } 
    } 

Надеюсь, это даст вам эту идею.

+0

Очень хороший ответ. Благодаря! – rogcg

0

В файле XML, установите атрибут verticalandroid:orientation на вашем <LinearLayout/>:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="?android:attr/listPreferredItemHeight" 
    android:id="@+id/linearId" 
    android:padding="6dip" 
    android:orientation="vertical"/> 

Ознакомьтесь с документацией класса LinearLayout и документацией orientation для получения дополнительной информации.

+0

Вы имеете в виду 'LinearLayout', а не' ListView', правильно? На самом деле это не проблема с этим примером, хотя я и думал, что это так. listitem должен быть просто единственным представлением, вставленным в другой вертикальный LinearLayout (реалистично ListView - это то, что необходимо). – kcoppock

+0

@kcoppock Да LinearLayout не ListView. Фиксация сейчас –

1

Ваша проблема заключается в том, что вы должны использовать listitem.xml из адаптера списка. Что вы делаете, сначала настройте представление контента на экземпляр listitem.xml, а затем попытайтесь вставить экземпляр списка Listitem INTO listitem в LinearLayout. Вы должны использовать listitem.xml в качестве представления, которое вы надуваете в методе getView() пользовательского адаптера. См. Класс QuoteAdapter от this question для примера или просто поиск Google «пользовательский ArrayAdapter Android» для тонны результатов по этой теме.

+0

Можете ли вы дать мне более четкий пример того, как это реализовать? Я не очень хорошо понял. Я создал объект ListAdatpter, но когда я пытаюсь создать его экземпляр, он говорит, что он не может быть сумасшедшим. Благодарю. – rogcg

+1

Это тот, к которому я обычно отношу людей: http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/ Выписка из книги CommonsWare в ListViews также прекрасна: http: // commonsware. com/Android/excerpt.pdf – kcoppock

+0

Хорошо, я попробую первую ссылку, а позже разместил здесь обновление. Благодарю. – rogcg

0

mmm ... Я полностью не понял, что требуется, но может потребоваться добавить android: orientation = "vertical" to LinearLayout с id = "linearId".

Смежные вопросы