簡體   English   中英

如何在自定義列表視圖中添加textview和editview?

[英]How to add textview and editview inside custom listview?

我想創建以下格式的自定義列表視圖。 例如

 _______________________________________
|                                       |
| -----------           ------------    |
|  Principal              EditView      |
| -----------           ------------    |
|                                       |
| -----------           ------------    |
|  Years                  EditView      |
| -----------           ------------    |
|                                       |
| -----------           ------------    |
|  Rate of Int             EditView     |
| -----------           ------------    |
|_______________________________________|

 _______________________________________
|                                       |
| -----------           ------------    |
|  Interest               EditView      |
| -----------           ------------    |
|_______________________________________|

 _______________________________________
|                                       |
| -----------           ------------    |
|  Simple Int             EditView      |
| -----------           ------------    |
|_______________________________________|

 _______________________________________
|                                       |
| ------------          ------------    |
|  Compond Int            EditView      |
| ------------          ------------    |
|_______________________________________|

簡而言之,列表視圖中的item1應該包含3個文本和editview,而item2應該具有2個文本和editview。 兩項均應獲得不同的輸入。

由於我是android的新手,所以我能夠創建簡單的listview,但是我不知道如何創建具有各種視圖的listview。

您需要創建一個自定義適配器,該適配器將能夠為每個單元格增加其自己的xml視圖。

您的新xml視圖應該簡單地具有列表視圖將要使用的最復雜的布局,對於不為子視圖使用信息的每個單元格,只需將該子視圖設置為不可見即可。

這至少應該指導您正確的方向

您為什么計划使用ListView ScrollView結合使用的TableLayout對您來說可能是一個更簡單的選擇。

對我來說,制作一個自己的ArrayAdapter效果很好。 那里你有方法

您可能會覆蓋的getView()。

在那里,您可以使用LayoutInflater inflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 膨脹您自己的視圖,該視圖在布局中已定義為XML。 您可以直接在此方法中填充視圖元素。

在我的代碼中,我制作了一個圖標布局,兩個文本字段和一個ProgressBar。 看起來像這樣:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 ... >

    <ImageView
        android:id="@+id/inventory_item_icon"
        .../>

    <TextView
        android:id="@+id/inventory_item_titel"
        .../>


    <TextView
        android:id="@+id/inventory_status_textfield"
        ... />

    <TextView
        android:id="@+id/inventory_item_info"
        ... />

    <ProgressBar
        android:id="@+id/inventory_item_status"
        ... />

</RelativeLayout>

直接將它們直接填充到getView()之后,我的代碼如下所示:

public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.inventory_icon_text_text, parent, false);
TextView textViewTitel = (TextView) rowView.findViewById(R.id.inventory_item_titel);
TextView textViewInfo = (TextView) rowView.findViewById(R.id.inventory_item_info);
ImageView imageView = (ImageView) rowView.findViewById(R.id.inventory_item_icon);

textViewTitel.setText(values[position]);
  String s = values[position];
    if (s.equals("A")) {
       imageView.setImageResource(R.drawable.a);
       textViewInfo.setText("This is case A!"); 
    } else if (s.equals("B")) {
    return rowView;
}

因此,您可以直接處理各個View元素。 如果要使用其他視圖布局,可以在增加布局之前嘗試進行檢查。 謝謝您可以選擇要使用的那個。

希望這對您有所幫助! 托比亞斯

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM