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

时间:2015-06-25 06:12:12

标签: android android-layout android-listview baseadapter

我是android的新手.. 我想使用自定义适配器在Listview中创建不同的视图。 请建议我如何做到这一点。

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

请查看随附的屏幕..

提前致谢

2 个答案:

答案 0 :(得分:0)

定义您希望列表行如此

的自定义布局
<?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" />

这是适配器类

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类

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

在arraylist中添加项目

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

//添加任意数量的项目

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

答案 1 :(得分:0)

此链接可能对您有所帮助。 link 你应该一步一步地做。

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