我有兴趣创建一个通用的BaseAdapter,它将应用于任何具有list_item的ListView。它会为list_view设置item_row。
public class GenericAdapter extends BaseAdapter
{
Context context;
ArrayList<HashMap<String, String>> list;
LayoutInflater inflater;
public GenericAdapter (Context context, ArrayList<HashMap<String, String>> list)
{
this.context = context;
this.list = list;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount()
{
return list.size();
}
@Override
public Object getItem(int position)
{
return list.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 = inflater.inflate(R.layout.item_of_any_list, parent, false);
// i have to do something here like there may be a method which must be call
}
/*HashMap<String, String> map = list.get(position);
TextView textViewName = (TextView) view.findViewById(R.id.name);
TextView textViewDate = (TextView) view.findViewById(R.id.date);
//TextView textViewDesc = (TextView) view.findViewById(R.id.desc);
textViewName.setText(map.get("name"));
textViewDate.setText(map.get("date"));
//textViewDesc.setText(map.get("description"));
*/
return view;
}
我认为我必须创建一个接口,并为单个项目设置item_row。但我不知道这可能是可能的。有人能告诉我如何实现它。
我的想法是创建一个接口,GenericAdapter实现该接口。在该界面中,有一种获取和设置item_row的方法。任何扩展GenericAdapter的类都必须实现该方法。它将帮助我们制作通用适配器。
当我们扩展该适配器时,我们不需要编写所有代码,如getCount,getItem,getItemId,我们只需要覆盖一个inflateLayout函数。必须在getView里面
先谢谢
答案 0 :(得分:11)
两年后回答我自己的问题。
要制作通用适配器,您需要abstract
中的parent class
方法,这些方法应由child
实施。因此,GenericAdapter
必须为Abstract
并为您要更改的数据创建abstract
方法。
我有不同类型的 ArrayList 只在适配器中显示它的model class
是不同的。所以我决定写一个genericAdapter
。这适用于recyclerview
,如果someOne interested
用于列表视图。让我知道。
<强> GenericAdapter 强>
public abstract class GenericAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private ArrayList<T> items;
public abstract RecyclerView.ViewHolder setViewHolder(ViewGroup parent);
public abstract void onBindData(RecyclerView.ViewHolder holder, T val);
public GenericAdapter(Context context, ArrayList<T> items){
this.context = context;
this.items = items;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder holder = setViewHolder(parent);
return holder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
onBindData(holder,items.get(position));
}
@Override
public int getItemCount() {
return items.size();
}
public void addItems( ArrayList<T> savedCardItemz){
items = savedCardItemz;
this.notifyDataSetChanged();
}
public T getItem(int position){
return items.get(position);
}
}
我用它就像
adapter = new GenericAdapter<DataModel>(context,modelList) {
@Override
public RecyclerView.ViewHolder setViewHolder(ViewGroup parent) {
final View view = LayoutInflater.from(context).inflate(R.layout.item_view_holder, parent, false);
ItemViewHolder viewHolder = new ItemViewHolder(context, view);
return viewHolder;
}
@Override
public void onBindData(RecyclerView.ViewHolder holder1, DataModel val) {
DataModel userModel = val;
ItemViewHolder holder = (ItemViewHolder)holder1;
holder.name.setText(userModel.getName());
holder.fatherName.setText(userModel.getFatherName());
}
};
mRecyclerView.setAdapter(adapter);
答案 1 :(得分:1)
我创建了一个通用适配器,aar和源代码位于this url:
它易于使用:
RecyclerAdapter<Customer> mAdapter = new RecyclerAdapter(customerList, CustomerViewHolder.class);
请参阅网址链接更多详情
答案 2 :(得分:0)
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import java.util.List;
public abstract class GenericAdapter<T> extends BaseAdapter {
LayoutInflater layoutInflater;
Context context;
private List<T> tList;
public GenericAdapter(Context context, List<T> tList) {
this.context = context;
this.tList = tList;
layoutInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
if (tList == null)
return 0;
return tList.size();
}
@Override
public Object getItem(int position) {
return tList.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
public abstract View getMyView(int position, View convertView, ViewGroup parent,T t);
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getMyView(position, convertView, parent,tList.get(position));
}
}
你可以像
一样使用它spnDistrict.setAdapter(new GenericAdapter<Institutions>(getContext(), institutionsList) {
@Override
public View getMyView(int position, View convertView, ViewGroup parent, Institutions institutions) {
TextView textView = (TextView) convertView;
if (textView == null) {
textView = (TextView) getLayoutInflater().inflate(android.R.layout.simple_list_item_1, null);
textView.setTextColor(getContext().getResources().getColor(R.color.black));
}
textView.setText(institutions.getName());
return textView;
}
});
答案 3 :(得分:-1)
import java.util.ArrayList;
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import com.accel.cloudRDB.model.OpenTask;
public class Open_task_fragment extends Fragment {
View view;
ListView mListView;
Open_task_adapter open_task_adapter;
Context mContext;
ArrayList<OpenTask> items;
Button main_menu;
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState)
{
mContext=getActivity();
view=inflater.inflate(R.layout.open_task_layout, container,false);
items=new ArrayList<OpenTask>();
initXML();
OpenTask open=new OpenTask();
open.setDate("10/10/2014");
open.setTime("12:00 pm");
open.setAddress("chennai Dlf,Accel@123");
open.setStatus("Deliver ABC");
OpenTask open1=new OpenTask();
open1.setDate("10/10/2014");
open1.setTime("12:00 pm");
open1.setAddress("chennai Dlf,Accel@123");
open1.setStatus("Deliver ABC2");
items.add(open);
items.add(open1);
open_task_adapter=new Open_task_adapter(mContext,items);
// open_task_adapter=new Open_task_adapter(mContext,items);
mListView.setAdapter(open_task_adapter);
mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View arg1, int position,
long arg3) {
Intent intent=new Intent(mContext,Task_detail_activity.class);
((Activity)mContext).startActivity(intent);
}
});
return view;
}
public void initXML()
{
mListView=(ListView)view.findViewById(R.id.listView1);
main_menu=(Button)view.findViewById(R.id.main_menu);
}
}
模型:
public class OpenTask {
String Date,Time,Address,Status;
public String getDate() {
return Date;
}
public void setDate(String date) {
Date = date;
}
public String getTime() {
return Time;
}
public void setTime(String time) {
Time = time;
}
public String getAddress() {
return Address;
}
public void setAddress(String address) {
Address = address;
}
public String getStatus() {
return Status;
}
public void setStatus(String status) {
Status = status;
}
}
适配器:
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class Open_task_adapter extends BaseAdapter {
Context context;
ArrayList<OpenTask> list;
public Open_task_adapter(Context context,ArrayList<OpenTask> list) {
this.context = context;
this.list = list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
OpenTaskHolder hodler;
LayoutInflater inflater;
OpenTask open=list.get(position);
if (convertView == null)
{
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.open_task_list_item_layout, null);
hodler = new OpenTaskHolder();
hodler.date = (TextView)convertView.findViewById(R.id.Date);
hodler.time = (TextView)convertView.findViewById(R.id.Time);
hodler.address = (TextView)convertView.findViewById(R.id.Address);
hodler.status = (TextView)convertView.findViewById(R.id.Status);
convertView.setTag(hodler);
} else {
hodler = (OpenTaskHolder)convertView.getTag();
}
hodler.date.setText(open.getDate());
hodler.time.setText(open.getTime());
hodler.address.setText(open.getAddress());
hodler.status.setText(open.getStatus());
return convertView;
}
class OpenTaskHolder {
TextView date,time,address,status;
}
}