I would like to be able to have a generic adapter that works for any ListFragment,so that I can send it any type of viewholder and it will just load the list. This is what I have so far and there a good few errors. I was originally want an adapter just for a friends list but now I want to be able to reuse the adapter for future projects. I come from a c++ background so templates in this are a little different. If you guys could show me whats to do be done in the actual ListFragment it would make my year
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import java.util.ArrayList;
public class FriendsAdapter<T> extends ArrayAdapter<T>
{
private ArrayList<T> objects;
private final LayoutInflater inflater;
private int resources;
private T viewHolder;
private int currPosition;
public FriendsAdapter(Context context, ArrayList<T> objects,
int resources, AdapterCommand<T> listener, T viewholder)
{
super(context, R.layout.friends_fragment);
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount()
{
return objects.size();
}
@Override
public T getItem(int position)
{
return objects.get(position);
}
@Override
public long getItemId(int position)
{
return 0;
}
public int getPosition()
{
return this.currPosition;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View rowView = convertView;
T viewHolder;
currPosition = position;
if (rowView == null)
{
// alternate layouts
if (position % 2 == 0)
{
rowView = inflater.inflate(R.layout.friends_fragment_even, null);
}
else
{
rowView = inflater.inflate(R.layout.friends_fragment_odd, null);
}
rowView = inflater.inflate(resources, parent, false);
viewHolder = new T();
rowView.setTag(viewHolder);
}
else
{
viewHolder = (T) rowView.getTag();
}
T object = getItem(position);
return rowView;
}
/*
public void NewFriend(String name,String status,Drawable img) // for adding a single friend to the list
{
Friend newFriend = new Friend(name, status, img);
if (rowView == null)
{
}
}
*/
public void DeletedFriend(String name,String hash) // delete a single friend from the list
{
// loop through , find name , remove the item from the list
}
}
As explained here you can use a GenericViewHolder (used in my application for a RecyclerAdapter instead of adapter for ListView)
Here my implementation
public static class GenericViewHolder extends RecyclerView.ViewHolder {
private SparseArray<View> holder = new SparseArray<View>();
public GenericViewHolder(View v) {
super(v);
deepScan((ViewGroup)v);
}
private void deepScan(ViewGroup v) {
if (v.getChildCount() == 0) return;
for(int i=0; i< v.getChildCount(); ++i) {
View nextChild = v.getChildAt(i);
put(nextChild);
if (nextChild instanceof ViewGroup) deepScan((ViewGroup)nextChild);
}
}
public <T extends View> T get(Integer id) {
return (T) holder.get(id);
}
public void put(View view) { put(view, view.getId()); }
public void put(View view, Integer id) {
if (id == -1) return; // Mean that hasn't id
holder.put(id, view);
}
public void putViewFromParent(View parent, Integer id) {
View view = parent.findViewById(id);
put(view, id);
}
public void clear() {
holder.clear();
}
public TextView getAsTextView(int id) { return (TextView) holder.get(id); }
public ImageView getAsImageView(int id) { return (ImageView) holder.get(id); }
public EditText getAsEditText(int id) { return (EditText) holder.get(id); }
public Button getAsButton(int id) { return (Button) holder.get(id); }
public Spinner getAsSpinner(int id) { return (Spinner) holder.get(id); }
public ListView getAsListView(int id) { return (ListView) holder.get(id); }
// ... add more getter or use the generic form
// Type x = holder.<Type>get(id);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.