Last active
December 15, 2015 20:19
-
-
Save codebreaker/5318067 to your computer and use it in GitHub Desktop.
A default adapter with viewholder and getview () method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.anim_play.adapter; | |
import java.util.List; | |
import org.json.JSONObject; | |
import com.example.anim_play.R; | |
import android.app.Activity; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.BaseAdapter; | |
import android.widget.ImageView; | |
import android.widget.TextView; | |
public class DemoAdapter extends BaseAdapter{ | |
private List<JSONObject> listItems; | |
private static class ViewHolder{ | |
/** The image view. */ | |
protected ImageView imageView; | |
/** The location text. */ | |
protected TextView txtUserName; | |
} | |
/** The m context. */ | |
private Activity mContext; | |
public DemoAdapter(Activity context){ | |
super(); | |
this.mContext=context; | |
} | |
public List<JSONObject> getListItems() { | |
return listItems; | |
} | |
public void setListItems(List<JSONObject> listItems) { | |
this.listItems = listItems; | |
} | |
@Override | |
public int getCount() { | |
if( listItems == null ) return 0; | |
return listItems.size(); | |
} | |
@Override | |
public Object getItem(int arg0) { | |
if( listItems == null ) return ""; | |
return listItems.get(arg0); | |
} | |
@Override | |
public long getItemId(int position) { | |
return 0; | |
} | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
if (getCount() < position) | |
return null; | |
ViewHolder holder = null; | |
final JSONObject obj = listItems.get(position); | |
if (convertView == null) { | |
holder = new ViewHolder(); | |
convertView = mContext.getLayoutInflater().inflate(R.layout.user_row, null); | |
holder.imageView = (ImageView)convertView.findViewById(R.id.img_user); | |
holder.txtUserName = (TextView)convertView.findViewById(R.id.txt_user_name); | |
convertView.setTag(holder); | |
//set your values here | |
} else { | |
holder = (ViewHolder) convertView.getTag(); | |
} | |
return convertView; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment