android 自定义适配器中的不同视图

Different views in custom adapter in android

我是 android 的新人.. 我想使用自定义适配器在 Listview 中创建不同的视图。 请建议我该怎么做。

第一行将有一个项目,第二行将有两个项目,依此类推..

请检查附加屏幕..

提前致谢

像这样定义列表行的自定义布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="@dimen/headerHeightCustRow"
 android:background="#FFFFFF"
 tools:ignore="HardcodedText" >

<TextView
    android:id="@+id/txtName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="2dip"
    android:layout_marginLeft="@dimen/viewSpace1"
    android:text="Varun Mad"
    android:textSize="@dimen/logout_textSize"
    android:textStyle="bold"
    android:textColor="@color/orange_normal" />

<TextView
    android:id="@+id/txtCustId"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_marginLeft="@dimen/viewSpace3"
    android:layout_marginTop="@dimen/viewSpace3"
    android:layout_marginRight="@dimen/viewSpace3"
    android:text="10549"
    android:layout_centerVertical="true"
    android:textColor="@color/orange_normal"
    android:textSize="15sp" />

<TextView
    android:id="@+id/txtPhone"
    android:layout_below="@id/txtName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="@dimen/viewSpace1"
    android:text="(886)677-8855"
    android:textColor="@color/orange_normal"
    android:textSize="@dimen/vsText" />

<TextView
    android:id="@+id/txtEmail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="@dimen/viewSpace1"
    android:text="ggg@gmail.com"
    android:textSize="@dimen/vsText"
    android:layout_below="@id/txtPhone"
    android:textColor="@color/orange_normal" />

<View
    android:layout_width="fill_parent"
    android:layout_height="0.5dip"
    android:layout_alignParentBottom="true"
    android:background="@color/greyDark" />

这是适配器class

public class SearchCustomerAdapter extends BaseAdapter {
Context ctx;
LayoutInflater lInflater;
ArrayList<SearchCustomerItem> objects;

public SearchCustomerAdapter(Context context, ArrayList<SearchCustomerItem> products) {
    ctx = context;
    objects = products;
    lInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    return objects.size();
}

@Override
public Object getItem(int position) {
    return objects.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    if (view == null) {
        view = lInflater.inflate(R.layout.search_cust_row, parent, false);
    }

    SearchCustomerItem p = getProduct(position);

    ((TextView) view.findViewById(R.id.txtName)).setText(p.customerName);

    if (p.customerEmail.compareTo("anyType{}") == 0) {
        ((TextView) view.findViewById(R.id.txtEmail)).setText("");
    } else {
        ((TextView) view.findViewById(R.id.txtEmail)).setText(p.customerEmail);
    }
    ((TextView) view.findViewById(R.id.txtPhone)).setText(p.customerPhone);
    ((TextView) view.findViewById(R.id.txtCustId)).setText(p.customerId);

    return view;
}

SearchCustomerItem getProduct(int position) {
    return ((SearchCustomerItem) getItem(position));
}

@SuppressWarnings("unused")
ArrayList<SearchCustomerItem> getBox() {
    ArrayList<SearchCustomerItem> box = new ArrayList<SearchCustomerItem>();
    for (SearchCustomerItem p : objects) {
      //  if (p.box)
      //      box.add(p);
    }
    return box;
}    

}

和 ITem Class

public class SearchCustomerItem {

public String customerName;
public String customerPhone;
public String customerEmail;
public String customerId;

 public SearchCustomerItem(String _cName, String _cPhone, String _cEmail, String _cCustId) {
     customerName = _cName;
     customerPhone = _cPhone;
     customerEmail = _cEmail;
     customerId = _cCustId;
 }  
}

你可以初始化适配器, 使用

在Arraylist中添加项目并显示在列表中
SearchCustomerAdapter boxAdapter;
ArrayList<SearchCustomerItem> products = new ArrayList<SearchCustomerItem>();

在数组列表中添加项目

products.add(new SearchCustomerItem("CustomerName","PhoneNo","Cust_EmailId","Cust_Id"));

//添加任意数量的项目

boxAdapter = new SearchCustomerAdapter(SearchActivity.this, products);
list.setAdapter(boxAdapter);        

这篇 link 可能会对您有所帮助。 link 你应该一步一步来。

  1. 通过自定义适配器 class
  2. 扩展基本适配器 class
  3. 覆盖 getView 和其他相关方法
  4. 将适配器设置为列表视图适配器。