2015-06-25 3 views
1

Я новичок в android .. Я хотел бы создать разные виды в Listview с помощью Custom Adapter. Пожалуйста, предложите мне, как я могу это сделать.Различные виды в пользовательском адаптере в android

В первой строке будет один элемент, а во втором ряду будет два товара и так далее ..

Пожалуйста, проверьте прилагаемый экран ..

Заранее спасибо

+0

Что вы пробовали до сих пор. – Yugesh

+2

Где экран? –

+0

@Yugesh Я установил адаптер с настраиваемой компоновкой. Он показывает один элемент в строке или два в строке. – Singh

ответ

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="[email protected]" 
    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);   
0

Эта ссылка может помочь вам. link Вы должны сделать это шаг за шагом.

  1. Расширение базового класса адаптера вами Пользовательский адаптер класса
  2. Override GetView и другие родственные методы
  3. установить адаптер для вывода списка просмотра адаптера.
Смежные вопросы