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();
Related
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 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 ;
}
}
my images get shuffled or changed when i scroll in list view.......
Images get shuffled when i scroll down and keep on changing own its own...............
public class CustomListAdapter extends BaseAdapter {
private ArrayList<NewsItem> listData;
private LayoutInflater layoutInflater;
public CustomListAdapter(Context context, ArrayList<NewsItem> listData) {
this.listData = listData;
layoutInflater = LayoutInflater.from(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) {
ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.activity_list_search, null);
holder = new ViewHolder();
holder.headlineView = (TextView) convertView.findViewById(R.id.title);
holder.reporterNameView = (TextView) convertView.findViewById(R.id.reporter);
holder.reportedDateView = (TextView) convertView.findViewById(R.id.date);
holder.imageView = (ImageView) convertView.findViewById(R.id.thumbImage);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
NewsItem newsItem = (NewsItem) listData.get(position);
holder.headlineView.setText(newsItem.getHeadline());
holder.reporterNameView.setText("Model: " + newsItem.getReporterName());
holder.reportedDateView.setText(newsItem.getDate());
if (holder.imageView != null) {
new ImageDownloaderTask(holder.imageView).execute(newsItem.getUrl());
}
return convertView;
}
static class ViewHolder {
TextView headlineView;
TextView reporterNameView;
TextView reportedDateView;
ImageView imageView;
}
}
The problem is very generic for new Android developers.
Your ImageDownloaderTask should take care of whether the downloaded image should still be posted on screen.
For example, after ImageDownloaderTask finishes one image downloading task (e.g. for index 15), it should make sure the 15th item is still displaying on screen. Since ListView is reusing all the item views, if you directly set the bit map to the original referenced view, your imageview will be shuffled.
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();
}
}
});
I have created a custom cursor adapter which fetches the value from the database and displays it in my defined view.
I have one textview and imageview.
Depends on the text in the database image changes accordingly.
I have defined MyArrayAdapter as follows, but it gives an error.
public class MyArrayAdapter extends SimpleCursorAdapter {
private final Context context;
private final Cursor cursor;
private final String[] from;
static class ViewHolder {
public ImageView imageView;
public TextView textView;
}
public MyArrayAdapter(Context context, int layout, Cursor c, String[] from,
int[] to) {
super(context, layout, c, from, to);
this.context = context;
this.cursor = c;
this.from = from;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
ViewHolder holder;
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.todo_row,null,true);
holder = new ViewHolder();
holder.textView = (TextView) rowView.findViewById(R.id.todo_edit_summary);
holder.imageView = (ImageView) rowView.findViewById(R.id.icon);
rowView.setTag(holder);
}else{
holder = (ViewHolder) rowView.getTag();
}
String s = cursor.getString(cursor.getColumnIndex(from[0]));
holder.textView.setText(s);
if (cursor.getString(cursor.getColumnIndex("category")) == "Urgent"){
holder.imageView.setImageResource(R.drawable.urgent);
}else{
holder.imageView.setImageResource(R.drawable.reminder);
}
return rowView;
}
}
(Update from deleted answer)
The following code is working fine, but they are showing the same value in each and every entry in the ListView. I have extended SimpleCursorAdapter. Am I making any mistakes with managing the cursor?
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
ViewHolder holder;
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.todo_row,null,true);
holder = new ViewHolder();
holder.textView = (TextView) rowView.findViewById(R.id.label);
holder.imageView = (ImageView) rowView.findViewById(R.id.icon);
rowView.setTag(holder);
}else{
holder = (ViewHolder) rowView.getTag();
}
String s = cursor.getString(cursor.getColumnIndex(from[0]));
holder.textView.setText(s);
if ((cursor.getString(cursor.getColumnIndex("category"))).equals("Urgent")){
holder.imageView.setImageResource(R.drawable.reminder);
}else{
holder.imageView.setImageResource(R.drawable.urgent);
}
return rowView;
}
As far as I can tell the only part of SimpleCursorAdapter you are using is the from parameter which you can easily substitute with a literal string since you are extending it. While I'm not familiar with the "guts" of the code it's pretty safe to assume that since getView is not mentioned in SimpleCursorAdapter documentation that it's not meant to be overridden lightly since doing so can break it like in your example.
Based on what you have, I've adapted it to extend CursorAdapter instead and by splitting the code you had in getView into newView and bindView, both of which are defined as abstract methods in CursorAdapter.
public class MyCursorAdapter extends CursorAdapter {
static class ViewHolder {
public ImageView imageView;
public TextView textView;
}
public MyArrayAdapter(Context context, Cursor c) {
super(context, c);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.todo_row,null,true);
ViewHolder holder = new ViewHolder();
holder.textView = (TextView) rowView.findViewById(R.id.todo_edit_summary);
holder.imageView = (ImageView) rowView.findViewById(R.id.icon);
rowView.setTag(holder);
return rowView;
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
ViewHolder holder = (ViewHolder) view.getTag();
String s = cursor.getString(cursor.getColumnIndex("whatever from[] represented"));
holder.textView.setText(s);
if (cursor.getString(cursor.getColumnIndex("category")) == "Urgent"){
holder.imageView.setImageResource(R.drawable.urgent);
}else{
holder.imageView.setImageResource(R.drawable.reminder);
}
}
}
To compare between value of two strings use equals() of String class. Donot use == operator in this case
So use the following code
if ((cursor.getString(cursor.getColumnIndex("category"))).equals("Urgent")){
holder.imageView.setImageResource(R.drawable.urgent);
}else{
holder.imageView.setImageResource(R.drawable.reminder);
}