【问题标题】:Different views in custom adapter in androidandroid中自定义适配器中的不同视图
【发布时间】:2015-06-25 06:12:12
【问题描述】:

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

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

请检查附加的屏幕..

提前致谢

【问题讨论】:

  • 到目前为止你已经尝试过什么。
  • @Yugesh 我已经用自定义布局设置了适配器。它在一行中显示一个项目或在一行中显示两个项目。
  • @Priyank 由于对 SO 的评价很低。它不允许我添加屏幕
  • 网上有很多教程。首先检查是否存在与您的问题匹配的问题,然后仅在 stackoverflow 上提问。

标签: android android-layout android-listview baseadapter


【解决方案1】:

定义一个你想要的列表行的自定义布局,像这样

<?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;
}    

}

和项目类

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2015-10-24
      • 1970-01-01
      • 2014-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-18
      相关资源
      最近更新 更多