【问题标题】:Creating custom list view in android using arraylist items使用arraylist项目在android中创建自定义列表视图
【发布时间】:2014-07-30 17:08:59
【问题描述】:

我想知道如何在 android 中为我的应用创建自定义列表视图。我的应用程序中有一个导航抽屉,其中包含几个片段。在一个片段中,我必须显示一个自定义列表视图,其中包含以下文本字段(姓名、名称、电话号码) 和片段中的图像视图(照片)。有字符串类型的(姓名指定电话号码)等arraylist和位图类型的arraylist(image)。这个数组列表的值是从 sqlite 数据库中获得的。所以我需要一个自定义列表视图,它应该从这个数组列表中填充值。我为活动中的每一行创建了一个布局文件。如下所示: 我不知道在我的代码中包含这个 xml 文件的位置。此外,我只看到了不适合我情况的教程。我试图实现它。但未能实现。

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:paddingTop="0dip" android:layout_gravity="top">
<TableRow>
  <LinearLayout 
        android:layout_width="110dp"
        android:layout_height="match_parent"
        android:paddingLeft="10dp"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        >
    <ImageView
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_gravity="left|top"  
        android:scaleType="centerCrop"
        android:id="@+id/image"

        />
    </LinearLayout>
<TableLayout 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:paddingTop="0dip" android:layout_gravity="top" 
  >
  <TableRow>      
     <TextView
          android:id="@+id/text"
          android:layout_height="wrap_content"
          android:layout_width="wrap_content"
          android:layout_weight="1" android:layout_gravity="left|center_vertical" 
          android:textSize="16sp" 
          android:layout_marginLeft="10dip"
          android:layout_marginTop="4dip"
          android:textColor="#000000"
          android:layout_span="1"
          android:text="Designation:text"
          /> 
    </TableRow>
    <TableRow>        
       <TextView
          android:text="Name : text1"
          android:id="@+id/text1"
          android:layout_height="wrap_content"
          android:layout_width="wrap_content"
          android:layout_weight="1" 
          android:layout_gravity="left|center_vertical" 
          android:textSize="16sp" 
          android:textColor="#000000"
          android:layout_marginLeft="10dip"
          android:layout_marginTop="4dip"
          android:gravity="left"/>
    </TableRow>
    <TableRow>    
     <TextView
          android:id="@+id/text2"
          android:layout_height="wrap_content"
          android:layout_width="wrap_content"
          android:layout_weight="1" android:layout_gravity="left|center_vertical" 
          android:textSize="16sp" 
          android:layout_marginLeft="10dip"
          android:layout_marginTop="4dip"
          android:textColor="#000000"
          android:layout_span="1"
          android:text="Phone No : text2"
          /> 
    </TableRow>
    <TableRow>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
            <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:src="@android:drawable/ic_menu_call" />

        <ImageButton
            android:id="@+id/imageButton2"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:src="@android:drawable/ic_dialog_email" />


        </LinearLayout>



    </TableRow>
  </TableLayout>

【问题讨论】:

    标签: android sqlite android-fragments arraylist navigation-drawer


    【解决方案1】:

    您需要创建自己的自定义列表适配器并为列表的每一行扩展自定义行布局。只需创建一个扩展 BaseAdapter 或 ArrayAdapter 的类。像这样的

    public class MyListAdapter extends ArrayAdapter<YourObjectItem> {
    
        private Context context; 
        private List<YourObjectItem> items;
        private LayoutInflater mInflater;
    
    
        public TerritoryListAdapter(Context context, List<YourObjectItem> listOfStuff) {
            super(context, territories);
            this.context = context; 
            this.items= listOfStuff;
            this.mInflater = LayoutInflater.from(context);
    
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
    
           //here inflate your view
          if(convetView == null) {
              convertView = mInflater.inflate(R.layout.list_item, null);
          }
           //now obtain an object from your list of objects
           YourObjectItem item = items.get(position);
    
           //use the fields to fill out your list item layout
           TextView text1 = (TextView) convertView.findViewById(R.id.text1);
           text1.setText(item.getSomethingFromYourObject);
    
           //and so on...
    
        }
    
    }
    

    您还应该覆盖该类的其他公共方法,例如 getCount 和 getItemId 等。在这种情况下,只需分别返回列表的大小和项目在列表中的位置。希望这会有所帮助

    编辑:我应该在你的布局中使用 listView 添加它,你应该在 Activity 的 onCreate 方法中调用 mListView.setAdapter(mListAdapter)。

    【讨论】:

      猜你喜欢
      • 2014-10-15
      • 1970-01-01
      • 2015-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-30
      相关资源
      最近更新 更多