Android ListView only last row on button click - java

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

Related

Android ListView getView display image randomly

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();

Using ViewHolder With ListView

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 ;
}
}

Android Listview clickable textview conflict

I have made an Listview populated with list_row_layout.xml(which is populated with json serializable class), i have clickable textview and onclick changing text from "Accept" to "Accepted". But when i click it on first listview item, another textview listview items below are changing. Here's some photos to descibe you better
Activity class
feedListView.setAdapter(new CustomListAdapter(this, feedList));
adapter class
public class CustomListAdapter extends BaseAdapter
{
private ArrayList<FeedItem> listData;
private LayoutInflater layoutInflater;
private Context mContext;
public CustomListAdapter(Context context, ArrayList<FeedItem> listData)
{
this.listData = listData;
layoutInflater = LayoutInflater.from(context);
mContext = context;
}
#Override
public int getCount()
{
return listData.size();
}
#Override
public Object getItem(int position)
{
return listData.get(position);
}
#Override
public long getItemId(int position)
{
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
final ViewHolder holder;
if (convertView == null)
{
convertView = layoutInflater.inflate(R.layout.list_row_layout, null);
holder = new ViewHolder();
holder.headlineView = (TextView)convertView.findViewById(R.id.title);
holder.reportedDateView = (TextView) convertView.findViewById(R.id.confid);
holder.approve = (TextView) convertView.findViewById(R.id.approveTV);
holder.approve.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View argView)
{
holder.approve.setText("accepted");
}
}
);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
FeedItem newsItem = listData.get(position);
holder.headlineView.setText(Html.fromHtml(newsItem.getTitle()));
holder.reportedDateView.setText(Html.fromHtml(newsItem.getContent()));
holder.approve.setTag(newsItem);
return convertView;
}
static class ViewHolder
{
TextView approve;
TextView headlineView;
TextView reportedDateView;
ImageView imageView;
}
}
textview code
<TextView
android:id="#id/approveTV"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginRight="5dp"
android:background="#drawable/pressed"
android:gravity="fill"
android:text="Accept"
android:clickable="true"
android:textColor="#0D98BA"
android:textSize="17sp" />
You have to write some tracking position functionality which remember the position which you have changed and make sure that only text of position changed .
you must set id to each row, and save it in one array,like integer and when you click on one row you must change the value of that, and in changing text or anything you must check the value of that row,if 0 then set default and if 1 then so what you want: my idea is create int[] with length of that is size of your list,
first you must define you int[] like below and set to zero every index.
int[] selected = new int[listData.getsize()]
something like this in getview:
holder.approve.setId(Html.fromHtml(newsItem.getId());
and after else statement in getView:
if (selected[position] == 0)
{
// set default
}
else
{
// any changing that you want
}
and in onClick:
selected[holder.approve.getId()] = 1;
// any change that you want
Try the below code:-
public View getView(final int position, View convertView, ViewGroup parent)
{
final ViewHolder holder;
if (convertView == null)
{
holder = new ViewHolder();
convertView = layoutInflater.inflate(R.layout.list_row_layout, null);
holder.headlineView = (TextView)convertView.findViewById(R.id.title);
holder.reportedDateView = (TextView) convertView.findViewById(R.id.confid);
holder.approve = (TextView) convertView.findViewById(R.id.approveTV);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
FeedItem newsItem = listData.get(position);
holder.headlineView.setText(Html.fromHtml(newsItem.getTitle()));
holder.reportedDateView.setText(Html.fromHtml(newsItem.getContent()));
holder.approve.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View argView)
{
holder.approve.setText("accepted");
}
}
);
return convertView;
}
Thanks!!

Android Listview clickable textview

I have made an Listview populated with list_row_layout.xml(which is populated with json serializable class), i have clickable textview and onclick changing text from "Accept" to "Accepted". But when i click it on first listview item, another textview listview items below are changing.
Here's some photos to descibe you better
this is the adapter class
public class CustomListAdapter extends BaseAdapter {
private ArrayList<FeedItem> listData;
private LayoutInflater layoutInflater;
private Context mContext;
public CustomListAdapter(Context context, ArrayList<FeedItem> listData) {
this.listData = listData;
layoutInflater = LayoutInflater.from(context);
mContext = context;
}
#Override
public int getCount() {
return listData.size();
}
#Override
public Object getItem(int position) {
return listData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.list_row_layout, null);
holder = new ViewHolder();
holder.headlineView = (TextView)convertView.findViewById(R.id.sex);
holder.reportedDateView = (TextView) convertView.findViewById(R.id.confid);
holder.approve = (TextView) convertView.findViewById(R.id.approveTV);
holder.approve.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View argView)
{
holder.approve.setText("Accepted");
}
}
);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
FeedItem newsItem = (FeedItem) listData.get(position);
holder.headlineView.setText(Html.fromHtml(newsItem.getTitle()));
holder.reportedDateView.setText(Html.fromHtml(newsItem.getContent()));
return convertView;
}
static class ViewHolder {
TextView approve;
TextView headlineView;
TextView reportedDateView;
ImageView imageView;
}
}
Remember that views can be recycled via convertView.
In your onClick method you set the approve text to "Accepted" but when the view is recycled, you never set it back to "Accept"
Actually you need to update (something in) the list in response to an click and have the Accept/Accepted value toggle based on that value rather than simply updating what is currently visible on the screen.
-- to answer the "how" question (asked below)--
Add a new field to ViewHolder
static class ViewHolder {
TextView approve;
TextView headlineView;
TextView reportedDateView;
ImageView imageView;
FeedItem newsItem;
}
Change the onClick method:
public void onClick(View argView)
{
// note that holder no longer needs to be final in the parent class
// because it is not used here.
View parent = (View)argView.getParent();
ViewHolder clickedHolder = (ViewHolder)parent.getTag();
clickedHolder .newsItem.setAccepted(true); /// a new method
clickedHolder .approve.setText ("Accepted");
Log.d(TAG, "Accepted item #" + position);
}
After you have convertView created (if necessary)
FeedItem newsItem = (FeedItem) listData.get(position);
holder.newsItem = newsItem; // populate the new field.
holder.headlineView.setText(Html.fromHtml(newsItem.getTitle()));
holder.reportedDateView.setText(Html.fromHtml(newsItem.getContent()));
if(newsItem.isAccepted ()){ // another new method!
holder.approve.setText ("Accepted");
Log.d(TAG, "Set text to Accepted for item #" + position);
}else{
holder.approve.setText("Accept");
Log.d(TAG, "Set text to Accept for item #" + position);
}
Once it is working you should consider removing the Log.d() lines to cut down on the noise in LogCat.

How to get view for an item in listview in android?

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();
}
}
});

Categories