I am using ListView to List text and images but I want to use ViewHolder to make the scrolling more smooth I have tried but can't quite get it right how must I modify the code
The way I have tried some of my images doesn't show up
#Override
public int getCount() {
return text1.length;
}
#Override
public Object getItem(int position) {
return text1[position];
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater infla=getActivity().getLayoutInflater();
View v = infla.inflate(R.layout.list_view_layout, null);
TextView tv1 = (TextView) v.findViewById(R.id.textView1);
ImageView iv1 = (ImageView) v.findViewById(R.id.imageView1);
TextView tv2 = (TextView) v.findViewById(R.id.textView2);
ImageView iv2 = (ImageView) v.findViewById(R.id.imageView2);
TextView tv3 = (TextView) v.findViewById(R.id.textView3);
ImageView iv3 = (ImageView) v.findViewById(R.id.imageView3);
TextView tv4 = (TextView) v.findViewById(R.id.textView4);
ImageView iv4 = (ImageView) v.findViewById(R.id.imageView4);
TextView tv5 = (TextView) v.findViewById(R.id.textView5);
ImageView iv5 = (ImageView) v.findViewById(R.id.imageView5);
TextView tv6 = (TextView) v.findViewById(R.id.textView6);
ImageView iv6 = (ImageView) v.findViewById(R.id.imageView6);
TextView tv7 = (TextView) v.findViewById(R.id.textView7);
ImageView iv7 = (ImageView) v.findViewById(R.id.imageView7);
tv1.setText(text1[position]);
iv1.setImageResource(text2[position]);
tv2.setText(text3[position]);
iv2.setImageResource(text4[position]);
tv3.setText(text5[position]);
iv3.setImageResource(text6[position]);
tv4.setText(text7[position]);
iv4.setImageResource(text8[position]);
tv5.setText(text9[position]);
iv5.setImageResource(text10[position]);
tv6.setText(text11[position]);
iv6.setImageResource(text12[position]);
tv7.setText(text13[position]);
iv7.setImageResource(text14[position]);
if(text2[position]==R.drawable.ic_star){
iv1.setVisibility(View.GONE);
}if(text3[position].matches("")) {
tv2.setVisibility(View.GONE);
}if(text4[position]==R.drawable.ic_star){
iv2.setVisibility(View.GONE);
}if(text5[position].matches("")) {
tv3.setVisibility(View.GONE);
}if(text6[position]==R.drawable.ic_star){
iv3.setVisibility(View.GONE);
}if(text7[position].matches("")){
tv4.setVisibility(View.GONE);
}if(text8[position]==R.drawable.ic_star){
iv4.setVisibility(View.GONE);
}if(text9[position].matches("")){
tv5.setVisibility(View.GONE);
}if(text10[position]==R.drawable.ic_star){
iv5.setVisibility(View.GONE);
}if(text11[position].matches("")){
tv6.setVisibility(View.GONE);
}if(text12[position]==R.drawable.ic_star){
iv6.setVisibility(View.GONE);
}if(text13[position].matches("")){
tv7.setVisibility(View.GONE);
}if(text14[position]==R.drawable.ic_star){
iv7.setVisibility(View.GONE);
}
return v;
}
Tricks:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if( convertView == null ){
convertView = LayoutInflater.from(getActivity()).inflate(R.layout.list_view_layout, null);
holder = new ViewHolder();
holder.tv=(TextView) convertView.findViewById(R.id.textView);
holder.iv=(ImageView) convertView.findViewById(R.id.imageView);
...
convertView.setTag(holder);
}else {
holder = (ViewHolder)convertView.getTag();
}
holder.tv.setText(text1[position]);
holder.iv.setImageResource(text2[position]);
...
//handle with your widgets cached in ViewHolder
return convertView;
}
And ViewHolder is here:
public class ViewHolder{
TextView tv;
ImageView iv;
}
First of all you need a static ViewHolder Class that holds your view in your adapter.
more like this one:
static class ViewHolder {
public TextView text;
public ImageView image;
}
And that viewholder will be used as a holder of view where you initialized its view in the getView method and set it as a tag so you can recycle it later for other item in the list.
You can follow this link to apply the viewholder pattern of listview
Your need to make use of the convertView sent across in getView() method for optimizing working.
Read my blog from properly optimizing the ListView.
The Viewholder pattern is used to cache the Views of the list item in a holder class so you don't have to call inflate view or findViewById() for every list item in the ListView. By making a class where you can hold keep a reference to all the ChildViews in the list item. Usually this is a inner static class.
The View convertView, parameter in the getView() method is the view that is cached by the Android system already. So you start with an if statement where you inflate a list item if convertView is null. You also set all the childview references on the viewholder there. Then you add the instance of the viewholder class as a tag to the convertView with setTag(). In the else part of the if statement you just get the viewholder from the convertView with getTag(). No you have the viewHolder and can actually update the listview item with new data.
I hope this explains what you need to if not here are some links to some better rescources then my explaination.
Vogella
Hold View Objects in a View Holder
Android ViewHolder Pattern Example
-here is the mmodified adapter to make the scroll more smooth using the viewholder pattern to reuse the item instead of create new one each time you scroll.
/**
* Created by Ziad on 4/22/2015.
*/
public class Adapter extends BaseAdapter {
#Override
public int getCount() {
// TODO Auto-generated method stub
return text1.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return text1[position];
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewdHolder holder=null;
if (convertView == null) {
LayoutInflater infla=getActivity().getLayoutInflater();
View v = infla.inflate(R.layout.list_view_layout, null);
holder = new viewdHolder();
holder.tv1= (TextView) v.findViewById(R.id.textView1);
holder.iv1 = (ImageView) v.findViewById(R.id.imageView1);
holder.tv2 =(TextView) v.findViewById(R.id.textView2);
holder.iv2= (ImageView) v.findViewById(R.id.imageView2);
holder.tv3 = (TextView) v.findViewById(R.id.textView3);
holder.iv3 = (ImageView) v.findViewById(R.id.imageView3);
holder.tv4 = (TextView) v.findViewById(R.id.textView4);
holder.iv4 = (ImageView) v.findViewById(R.id.imageView4);
holder.tv5= (TextView) v.findViewById(R.id.textView5);
holder.iv5 = (ImageView) v.findViewById(R.id.imageView5);
holder.tv6 = (TextView) v.findViewById(R.id.textView6);
holder.iv6 = (ImageView) v.findViewById(R.id.imageView6);
holder.tv7 = (TextView) v.findViewById(R.id.textView7);
holder.iv7 = (ImageView) v.findViewById(R.id.imageView7);
convertView.setTag(holder);
}else{
holder = (ViewdHolder) convertView.getTag();
}
holder.tv1.setText(text1[position]);
holder.iv1.setImageResource(text2[position]);
holder.tv2.setText(text3[position]);
holder.iv2.setImageResource(text4[position]);
holder.tv3.setText(text5[position]);
holder.iv3.setImageResource(text6[position]);
holder.tv4.setText(text7[position]);
holder.iv4.setImageResource(text8[position]);
holder.tv5.setText(text9[position]);
holder.iv5.setImageResource(text10[position]);
holder.tv6.setText(text11[position]);
holder.iv6.setImageResource(text12[position]);
holder.tv7.setText(text13[position]);
holder.iv7.setImageResource(text14[position]);
if(text2[position]==R.drawable.ic_star){
holder.iv1.setVisibility(View.GONE);
}
if(text3[position].matches("")) {
holder.tv2.setVisibility(View.GONE);
}
if(text4[position]==R.drawable.ic_star){
holder.iv2.setVisibility(View.GONE);
}
if(text5[position].matches("")) {
holder.tv3.setVisibility(View.GONE);
}
if(text6[position]==R.drawable.ic_star){
holder.iv3.setVisibility(View.GONE);
}
if(text7[position].matches("")){
holder.tv4.setVisibility(View.GONE);
}
if(text8[position]==R.drawable.ic_star){
holder.iv4.setVisibility(View.GONE);
}
if(text9[position].matches("")){
holder.tv5.setVisibility(View.GONE);
}
if(text10[position]==R.drawable.ic_star){
holder.iv5.setVisibility(View.GONE);
}
if(text11[position].matches("")){
holder.tv6.setVisibility(View.GONE);
}
if(text12[position]==R.drawable.ic_star){
holder.iv6.setVisibility(View.GONE);
}
if(text13[position].matches("")){
holder.tv7.setVisibility(View.GONE);
}
if(text14[position]==R.drawable.ic_star){
holder.iv7.setVisibility(View.GONE);
}
return v;
}
class ViewdHolder {
TextView tv1;
ImageView iv1 ;
TextView tv2 ;
ImageView iv2;
TextView tv3 ;
ImageView iv3 ;
TextView tv4 ;
ImageView iv4 ;
TextView tv5 ;
ImageView iv5 ;
TextView tv6 ;
ImageView iv6 ;
TextView tv7 ;
ImageView iv7 ;
}
}
Related
I have a custom listview with a button to add more elements
but when I add and element the app crash, but when I restart I find that the element is added, (rarely it doesn't crash)
And i
I use custom adapter
class CustomAdapter extends BaseAdapter {
ArrayList<ListItem> listItems = new ArrayList<ListItem>();
CustomAdapter(ArrayList<ListItem> list){
this.listItems = list;
}
#Override
public int getCount() {
return listItems.size();
}
#Override
public Object getItem(int position) {
return listItems.get(position).name;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int i, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.list_item,null);
TextView name = (TextView) view.findViewById(R.id.name);
TextView lastm = (TextView) view.findViewById(R.id.last);
TextView time = (TextView) view.findViewById(R.id.time);
CircleImageView pic= (CircleImageView) view.findViewById(R.id.pic);
name.setText(listItems.get(i).name);
lastm.setText(listItems.get(i).lastm);
time.setText(listItems.get(i).time);
Bitmap bmp = BitmapFactory.decodeByteArray(listItems.get(i).pic,0,listItems.get(i).pic.length);
pic.setImageBitmap(bmp);
return view;
}
}
and when I add an element the app crashs
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText editText=(EditText) mView.findViewById(R.id.name);
String name=editText.getText().toString();
boolean result=myDataBase.insertData(imageViewToByte(img),name,"no messages yet","");
if (result) {
Toast.makeText(Main2Activity.this, "Saved in DATABASE", Toast.LENGTH_SHORT).show();
viewLastData();
dialog.dismiss();
}
If your ListView lags it's because you used wrap_content for the listView's layout_height. It forces your ListView to count all the items inside and it has a huge performance impact.
So replace wrap_content by match_parent to avoid those lags.
EDIT: Use a ViewHolder pattern too in your Adapter, see this link.
Here is an example:
// ViewHolder Pattern
private static class ViewHolder {
TextView name, status;
}
#Override #NonNull
public View getView(int position, View convertView, #NonNull ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater vi = LayoutInflater.from(getContext());
convertView = vi.inflate(R.layout.list_item, parent, false);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.text_name);
holder.status = (TextView) convertView.findViewById(R.id.another_text);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// Then do the other stuff with your item
// to populate your listView
// ...
return convertView
}
I have an custom list view.
it is laggy when if comes to the conditional statements part
public View getView(int position, View convertView, ViewGroup parent) {
View caloocanView = convertView;
ViewHolder holder = new ViewHolder();
if (caloocanView == null)
caloocanView = getLayoutInflater().inflate(R.layout.caloocan_list_view, parent, false);
restauInfoDB restaurant = Restau.get(position);
//ImageView from URL
holder.restauIcon = (ImageView) caloocanView.findViewById(R.id.imageView);
Glide.with(getContext()).load(restaurant.getUrl()).centerCrop()
.placeholder(R.drawable.six)
.crossFade()
.into(holder.restauIcon);
String x = "N";
Double hotCTR = 15.0;
String New = "N";
holder.healthyIcon = (ImageView) caloocanView.findViewById(R.id.healthyIcon);
//HEALTHY
if (x.equals(restaurant.getHealthy())) {
holder.healthyIcon.setVisibility(View.INVISIBLE);
}
else
{
holder.healthyIcon.setVisibility(View.VISIBLE);
}
//NEW
if (New.equals(restaurant.getNew())){
holder.newLabel = (ImageView) caloocanView.findViewById(R.id.newLabel);
holder.newLabel.setVisibility(View.INVISIBLE);
}
else {
holder.newLabel = (ImageView) caloocanView.findViewById(R.id.newLabel);
holder.newLabel.setVisibility(View.VISIBLE);
}
//HOT
if (hotCTR <= Double.valueOf(restaurant.getRating())) {
holder.hotIcon = (ImageView) caloocanView.findViewById(R.id.iconHot);
holder.hotIcon.setVisibility(View.VISIBLE);
}
else
{
holder.hotIcon = (ImageView) caloocanView.findViewById(R.id.iconHot);
holder.hotIcon.setVisibility(View.INVISIBLE);
}
String serving = "Serving: ";
// RESTAU NAME
holder.restauName = (TextView) caloocanView.findViewById(R.id.resnameTxt);
holder.restauName.setText(restaurant.getResname());
//FOOD TYPE
holder.oh = (TextView) caloocanView.findViewById(R.id.ophrTxt);
holder.oh.setText("Operating Hours: " +restaurant.getOh());
holder.resloc = (TextView) caloocanView.findViewById(R.id.reslocTxt);
holder.resloc.setText(restaurant.getResloc());
return caloocanView;
}`
and this is the static viewHolder
static class ViewHolder
{
ImageView restauIcon;
ImageView healthyIcon;
ImageView newLabel;
ImageView hotIcon;
TextView restauName;
TextView oh;
TextView resloc;
}
you are missing the point of using ViewHolder,the point is to minimize inflating new layouts and finding views in layout which are time consuming and make view laggy,but you are calling findViewById() every time.
try this code instead:
View caloocanView = convertView;
ViewHolder holder = new ViewHolder();
if (caloocanView == null) {
caloocanView = getLayoutInflater().inflate(R.layout.caloocan_list_view, parent, false);
holder.restauIcon = (ImageView) caloocanView.findViewById(R.id.imageView);
holder.healthyIcon = (ImageView) caloocanView.findViewById(R.id.healthyIcon);
holder.newLabel = (ImageView) caloocanView.findViewById(R.id.newLabel);
...
caloocanView.setTag(holder);
}else{
holder = (ViewHolder) caloocanView.getTag();
}
...
//HEALTHY
if (x.equals(restaurant.getHealthy())) {
holder.healthyIcon.setVisibility(View.INVISIBLE);
}
else
{
holder.healthyIcon.setVisibility(View.VISIBLE);
}
and reformat rest of your code
Try adding this line to your RecyclerView layout xml:
android:nestedScrollingEnabled="false"
It may be not affect older versions of Android, but in newer versions the lag is completely gone.
EDIT:by RecyclerView, I mean ListView
I would like to display items with an adapter in a ListView but when getView is called in the ArrayAdapter, it displays the good image but not on the good item when I am scrolling. It is like if the findViewById didn't give me the good id of the layout.
public class ItemPackAdapter extends ArrayAdapter<Pack> {
Context context;
public ItemPackAdapter(Context context, ArrayList<Pack> pack) {
super(context, 0, pack);
this.context = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
// Check if an existing view is being reused, otherwise inflate the view
final Pack pack = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.item_pack, parent, false);
holder = new ViewHolder();
holder.textView1 = (TextView) convertView.findViewById(R.id.textView1);
holder.textView2 = (TextView) convertView.findViewById(R.id.textView2);
holder.textView3 = (TextView) convertView.findViewById(R.id.textView3);
holder.imageView = (ImageView) convertView.findViewById(R.id.imageView);
if (!pack.getImageName().equals("null")) {
UrlGenerator urlGenerator = new UrlGenerator();
String url = urlGenerator.getDownloadPicture(pack.getImageName());
DownloadPicture downloadPicture = new DownloadPicture(holder.imageView, url, getContext());
downloadPicture.start();
}
convertView.setTag(holder);
}
else {
holder = (ViewHolder)convertView.getTag();
}
holder.textView1.setText(pack.getSomething1());
holder.textView2.setText(pack.getSomething2());
holder.textView3.setText(pack.getSomething3());
if (!pack.getImageName().equals("null")) {
UrlGenerator urlGenerator = new UrlGenerator();
String url = urlGenerator.getDownloadPicture(pack.getImageName());
DownloadPicture downloadPicture = new DownloadPicture(holder.imageView, url, getContext());
downloadPicture.start();
}
if(pack.getImageName().equals("null")){
holder.imageView.setImageBitmap(null);
}
return convertView;
}
static class ViewHolder {
TextView textView1;
TextView textView2;
TextView textView3;
ImageView imageView;
}
}
Actually I found a solution with :
if(pack.getImageName().equals("null")){
holder.imageView.setImageBitmap(null);
}
But when I am scrolling on the listView, I can see the image in the wrong item, and I need to scroll again to call getView to delete the image with the previous condition.
I would like something cleaner :p
Thank you in advance.
And sorry for my bad English.
Your code looks will work just fine with just a little tweak. I discovered that you were setting the ImageView twice, so I edit your code as shown below
public class ItemPackAdapter extends ArrayAdapter<Pack> {
Context context;
ArrayList<Pack> packs;
public ItemPackAdapter(Context context, ArrayList<Pack> packs) {
super(context, 0, packs);
this.context = context;
this.packs = packs;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
// Check if an existing view is being reused, otherwise inflate the view
final Pack pack = packs.get(position);
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.item_pack, parent, false);
holder = new ViewHolder();
holder.textView1 = (TextView) convertView.findViewById(R.id.textView1);
holder.textView2 = (TextView) convertView.findViewById(R.id.textView2);
holder.textView3 = (TextView) convertView.findViewById(R.id.textView3);
holder.imageView = (ImageView) convertView.findViewById(R.id.imageView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if (pack.getImageName().equals("null")) {
holder.imageView.setImageBitmap(null);
} else {
UrlGenerator urlGenerator = new UrlGenerator();
String url = urlGenerator.getDownloadPicture(pack.getImageName());
DownloadPicture downloadPicture = new DownloadPicture(holder.imageView, url, getContext());
downloadPicture.start();
}
holder.textView1.setText(pack.getSomething1());
holder.textView2.setText(pack.getSomething2());
holder.textView3.setText(pack.getSomething3());
return convertView;
}
static class ViewHolder {
TextView textView1;
TextView textView2;
TextView textView3;
ImageView imageView;
}
}
In this case if you don't caching your image that may give you like this problem.
Use Picasso library is highly recommended for avoiding this problem. Add the picasso library in your project then write your code some thing like this.
Picasso.with(getApplicationContext()).load(url).into(holder.imageView);
instead of this line
DownloadPicture downloadPicture = new DownloadPicture(holder.imageView, url, getContext());
downloadPicture.start();
I have the following code:
static class ViewHolder {
TextView camera;
TextView players;
TextView max_players;
ImageView privata;
Button Buton;
}
ViewHolder holder;
#Override
public View getView(int position, View convertView, ViewGroup parent) {
String variabile[] = getItem(position).split("\\s+");
if(convertView == null)
{
LayoutInflater linflater = LayoutInflater.from(getContext());
convertView = linflater.inflate(R.layout.custom_row, parent, false);
holder = new ViewHolder();
holder.camera = (TextView) convertView.findViewById(R.id.Nume);
holder.players = (TextView) convertView.findViewById(R.id.players);
holder.max_players = (TextView) convertView.findViewById(R.id.max_players);
holder.privata = (ImageView) convertView.findViewById(R.id.privata);
holder.Buton = (Button) convertView.findViewById(R.id.Buton);
holder.camera.setText(variabile[0]);
if (!variabile[1].equals("true")) {
parola = false;
holder.privata.setVisibility(View.INVISIBLE);
}
holder.players.setText(variabile[2]);
holder.max_players.setText(variabile[3]);
room_id = variabile[4];
nume = variabile[5];
holder.Buton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
hash = new HashMap<String, String>();
hash.put("name", nume);
hash.put("room", room_id);
if (intra) {
holder.Buton.setText("Iesi");
site = siteul + "/join";
intra = false;
} else {
holder.Buton.setText("Intra");
site = siteul + "/leave";
intra = true;
}
new ATask().execute(site);
}
});
convertView.setTag(holder);
}
else
holder = (ViewHolder) convertView.getTag();
return convertView;
}
I'm trying to access a row from an AsyncTask on PostExecute and modify it like this:
TextView players_mare = holder.players;
players_mare.setText(rez.substring(2));
No matter the button pressed it seems to modify the textview of the last item in the list.
This is happening because you are using holder. After rendering complete ListView it has reference of the last item in the ListView, so only last item is updated irrespective of the item on which button was clicked. You need to pass the reference of item in which button was clicked to the ATask class.
Add
holder.Buton.setTag(holder);
after
convertView.setTag(holder);
in getView() method
Create a class level variable in ATask clas like this
ViewHolder myHolder;
Add this in ATask
public ATask(ViewHolder view) {
myHolder = view;
}
Change ATask call like this
new ATask((ViewHolder) v.getTag()).execute(site);
In PostExecute replace every holder with myHolder
Is it possible to get an item view based on its position in the adapter and not in the visible views in the ListView?
I am aware of functions like getChildAt() and getItemIdAtPosition() however they provide information based on the visible views inside ListView. I am also aware that Android recycles views which means that I can only work with the visible views in the ListView.
My objective is to have a universal identifier for each item since I am using CursorAdapter so I don't have to calculate the item's position relative to the visible items.
Here's how I accomplished this. Within my (custom) adapter class:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflater = context.getLayoutInflater();
view = inflater.inflate(textViewResourceId, parent, false);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.name = (TextView) view.findViewById(R.id.name);
viewHolder.button = (ImageButton) view.findViewById(R.id.button);
viewHolder.button.setOnClickListener
(new View.OnClickListener() {
#Override
public void onClick(View view) {
int position = (int) viewHolder.button.getTag();
Log.d(TAG, "Position is: " +position);
}
});
view.setTag(viewHolder);
viewHolder.button.setTag(items.get(position));
} else {
view = convertView;
((ViewHolder) view.getTag()).button.setTag(items.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
return view;
}
Essentially the trick is to set and retrieve the position index via the setTag and getTag methods. The items variable refers to the ArrayList containing my custom (adapter) objects.
Also see this tutorial for in-depth examples. Let me know if you need me to clarify anything.
See below code:
public static class ViewHolder
{
public TextView nm;
public TextView tnm;
public TextView tr;
public TextView re;
public TextView membercount;
public TextView membernm;
public TextView email;
public TextView phone;
public ImageView ii;
}
class ImageAdapter extends ArrayAdapter<CoordinatorData>
{
private ArrayList<CoordinatorData> items;
public FoodDriveImageLoader imageLoader;
public ImageAdapter(Context context, int textViewResourceId,ArrayList<CoordinatorData> items)
{
super(context, textViewResourceId, items);
this.items = items;
}
public View getView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
ViewHolder holder = null;
if (v == null)
{
try
{
holder=new ViewHolder();
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader = new FoodDriveImageLoader(FoodDriveModule.this);
v = vi.inflate(R.layout.virtual_food_drive_row, null);
//System.out.println("layout is null.......");
holder.nm = (TextView) v.findViewById(R.id.name);
holder.tnm = (TextView) v.findViewById(R.id.teamname);
holder.tr = (TextView) v.findViewById(R.id.target);
holder.re = (TextView) v.findViewById(R.id.received);
holder.membercount = new TextView(FoodDriveModule.this);
holder.membernm = new TextView(FoodDriveModule.this);
holder.email = new TextView(FoodDriveModule.this);
holder.phone = new TextView(FoodDriveModule.this);
holder.ii = (ImageView) v.findViewById(R.id.icon);
v.setTag(holder);
}
catch(Exception e)
{
System.out.println("Excption Caught"+e);
}
}
else
{
holder=(ViewHolder)v.getTag();
}
CoordinatorData co = items.get(position);
holder.nm.setText(co.getName());
holder.tnm.setText(co.getTeamName());
holder.tr.setText(co.getTarget());
holder.re.setText(co.getReceived());
holder.ii.setTag(co.getImage());
imageLoader.DisplayImage(co.getImage(), FoodDriveModule.this , holder.ii);
if (co != null)
{
}
return v;
}
}
A better option is to identify using the data returned by the CursorAdapter rather than visible views.
For example if your data is in a Array , each data item has a unique index.
Here, Its very short described code, you can simple re-use viewholder pattern to increase listview performance
Write below code in your getview() method
ViewHolder holder = new ViewHolder();
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.skateparklist, null);
holder = new ViewHolder();
holder.headlineView = (TextView) convertView
.findViewById(R.id.textView1);
holder.DistanceView = (TextView) convertView
.findViewById(R.id.textView2);
holder.imgview = (NetworkImageView) convertView
.findViewById(R.id.imgSkatepark);
convertView.setTag(holder); //PLEASE PASS HOLDER AS OBJECT PARAM , CAUSE YOU CAN NOY PASS POSITION IT WILL BE CONFLICT Holder and Integer can not cast
//BECAUSE WE NEED TO REUSE CREATED HOLDER
} else {
holder = (ViewHolder) convertView.getTag();
}
// your controls/UI setup
holder.DistanceView.setText(strDistance);
......
return convertview
Listview lv = (ListView) findViewById(R.id.previewlist);
final BaseAdapter adapter = new PreviewAdapter(this, name, age);
confirm.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
View view = null;
String value;
for (int i = 0; i < adapter.getCount(); i++) {
view = adapter.getView(i, view, lv);
Textview et = (TextView) view.findViewById(R.id.passfare);
value=et.getText().toString();
Toast.makeText(getApplicationContext(), value,
Toast.LENGTH_SHORT).show();
}
}
});