2

I am using BaseAdapter in Android.These are my codes.

 public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.article_list_item, null);
            holder = new ViewHolder();
            holder.favoriteImage= (ImageView) convertView.findViewById(R.id.imageViewFavoriteItem);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        ArticleItem articleItem = (ArticleItem) listData.get(position);
        String isFavorite= articleItem.get_favorite();
        if(isFavorite.equals("1"))
        {
            holder.favoriteImage.setImageResource(R.drawable.ic_star_active);
        }

        return convertView;
    }

The problem is I have a list of items and I want to add "Star Icon" to the item which are added to favorite.

        String isFavorite= articleItem.get_favorite();
        if(isFavorite.equals("1"))
        {
           //Statement is okay
        }

However I can't manage to use like this.

        String isFavorite= articleItem.get_favorite();
        if(isFavorite.equals("1"))
        {
            holder.favoriteImage.setImageResource(R.drawable.ic_star_active);
        }

I did some research and came to this. Android BaseAdapter Context

an Activity is a single, focused thing that the user can do. Almost all activities interact with the user, so the Activity class takes care of creating a window for you in which you can place your UI.
a Fragment is a piece of an application's user interface or behavior that can be placed in an Activity.
an Adapter object acts as a bridge between an AdapterView and the underlying data for that view. It is also responsible for making a View for each item in the data set.

According to what the answer said I can't use in Adapter. I am not sure as I came from PHP background.I am now stuck .

Here is debug log

09-03 02:22:36.278    1233-1233/myanmarwebsolutions.myanmar_shopper D/AndroidRuntime﹕ Shutting down VM
09-03 02:22:36.278    1233-1233/myanmarwebsolutions.myanmar_shopper W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa6225288)
09-03 02:22:36.278    1233-1233/myanmarwebsolutions.myanmar_shopper E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at myanmarwebsolutions.mws_myanmarshopper.Adatper.ArticleAdapter.getView(ArticleAdapter.java:92)
            at android.widget.AbsListView.obtainView(AbsListView.java:2267)
            at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
            at android.widget.ListView.onMeasure(ListView.java:1156)
            at android.view.View.measure(View.java:15172)
            at android.widget.RelativeLayout.measureChild(RelativeLayout.java:602)
            at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:415)
            at android.view.View.measure(View.java:15172)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
            at android.view.View.measure(View.java:15172)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at android.view.View.measure(View.java:15172)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
            at android.view.View.measure(View.java:15172)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
            at android.view.View.measure(View.java:15172)
            at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1848)
            at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1100)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1273)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
            at android.view.Choreographer.doCallbacks(Choreographer.java:555)
            at android.view.Choreographer.doFrame(Choreographer.java:525)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
09-03 02:22:36.294    1233-1236/myanmarwebsolutions.myanmar_shopper D/dalvikvm﹕ GC_CONCURRENT freed 1276K, 12% free 10991K/12359K, paused 11ms+1ms, total 16ms
09-03 02:22:36.646    1233-1272/myanmarwebsolutions.myanmar_shopper D/skia﹕ --- SkImageDecoder::Factory returned null
09-03 02:22:36.646    1233-1272/myanmarwebsolutions.myanmar_shopper D/skia﹕ --- SkImageDecoder::Factory returned null
09-03 02:22:37.446    1233-1263/myanmarwebsolutions.myanmar_shopper D/skia﹕ --- SkImageDecoder::Factory returned null
09-03 02:22:37.446    1233-1263/myanmarwebsolutions.myanmar_shopper D/skia﹕ --- SkImageDecoder::Factory returned null
Community
  • 1
  • 1

1 Answers1

0

You need to write else statement also Beacause adapter reuses the old views which are already created So if and else both should be there

Set favorite image in else alse

SHASHIDHAR MANCHUKONDA
  • 3,302
  • 2
  • 19
  • 40
  • added else not working " String isFavorite= articleItem.get_favorite(); if(isFavorite.equals("1")) { holder.favoriteImage.setImageResource(R.drawable.ic_star_active); } else { holder.favoriteImage.setImageResource(R.drawable.ic_star_inactive); }" – Myanmar Web Solution Sep 03 '14 at 02:15