title: How to set Icons into ListItems dynamically?
body: <p>I have a ListView whose list item comes from a 'String' from Shared Preference. Now I have to set two icons "success" and "failed" identifying those keywords "successful" and "failed" from string. But while setting it is either setting "success" icons to all listitems or "failed" icons to all reason because string contains both. Any idea how can i identify each list items and set icons to them ? Below is my code :</p>
  
  <p>Class where I am retrieving "oldlistitems" and "newlistitems" strings from Shared Preference and trying to set icons to listitems</p>
  
  <pre><code>public class EntryAdapterLog extends ArrayAdapter&lt;Item&gt; {
  
      private Context context;
      private ArrayList&lt;Item&gt; items;
      private LayoutInflater vi;
  
      public EntryAdapterLog(Context context,ArrayList&lt;Item&gt; items) {
          super(context,0, items);
          this.context = context;
          this.items = items;
          vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      }
  
  
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
          View v = convertView;
  
          final Item i = items.get(position);
          if (i != null) {
              if(i.isSection()){
                  SectionItem si = (SectionItem)i;
                  v = vi.inflate(R.layout.list_item_section, null);
  
                  v.setOnClickListener(null);
                  v.setOnLongClickListener(null);
                  v.setLongClickable(false);
  
                  final TextView sectionView = (TextView) v.findViewById(R.id.list_item_section_text);
                  sectionView.setText(si.getTitle());
              }else{
  
                  String oldlistitems = LogListView.first;
                  String newlistitems = LogListView.title;
                  Log.d("LOG", "ABCD : " + oldlistitems);
                  Log.d("LOG", "DEFG : " + newlistitems);
  
                  EntryItem ei = (EntryItem)i;
                  v = vi.inflate(R.layout.list_item_entry_log, null);
                  final TextView title = (TextView)v.findViewById(R.id.list_item_entry_title);
                  final TextView subtitle = (TextView)v.findViewById(R.id.list_item_entry_summary);
                  final ImageView imageicon = (ImageView)v.findViewById(R.id.list_item_entry_drawable);
  
                  if(title != null) {
                      title.setText(ei.title);
                  }
                  if(subtitle != null){
                      subtitle.setText(ei.subtitle);
                  }
                  //HERE IS PROCESS OF SETTING ICONS
                  if ((oldlistitems !=null &amp;&amp; oldlistitems.contentEquals("Sync Successful")) || (newlistitems != null &amp;&amp; newlistitems.contentEquals("Sync Successful"))){
                      imageicon.setImageResource(R.drawable.ok);
                  }
                  else {
                      imageicon.setImageResource(R.drawable.wrong);
                  }
  
              }
          }
          return v;
      }
  
  }
  </code></pre>
  
  <p>Class where i am setting shared preference</p>
  
  <pre><code>public class LogListView extends ListActivity {
      /** Called when the activity is first created. */
      static String newString;
      private static EntryAdapterLog adapter;
      int clickCounter = 0;
      static ArrayList&lt;Item&gt; items = new ArrayList&lt;Item&gt;();
      static SharedPreferences preferences = null;
      private static Context context = null;
      static StringTokenizer tokens;
      static String first;
      private static String second;
      private JSONArray jsonarry = null;
      static String saveitems;
      private JSONObject jsonobject = null;
      private String subtitle;
      static String title;
  
  
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          context = this;
          adapter = new EntryAdapterLog(this, items);
          // items.add(new SectionItem("Log Report"));
          setListAdapter(adapter);
  
          if (adapter.getCount() != 0) {
              // Do nothing Adapter has value
          } else {
              retreiveItems();
          }
  
      }
  
      // Method which will handle dynamic insertion
      public static void addItems() {
  
          preferences = context.getSharedPreferences("LOG",android.content.Context.MODE_PRIVATE);
          newString = preferences.getString("log", "");
  
          tokens = new StringTokenizer(newString, ",");
          first = tokens.nextToken();
          second = tokens.nextToken();
  
          items.add(new EntryItem(first, second));
  
          adapter.notifyDataSetChanged();
  
      }
      // Method which will handle dynamic insertion ends
  
      @Override
      protected void onDestroy() {
          super.onDestroy();
          saveItems();
      }
  
      // Save ListItems if restarted
      protected static void saveItems() {
          SharedPreferences prefs = context.getSharedPreferences("prefName",Context.MODE_PRIVATE);
          Editor editor = prefs.edit();
          editor.putString("myList", new Gson().toJson(items).toString());
          editor.apply();
          Log.d("LOG", "Saved Items : " + items);
      }
      // Save ListItems if restarted ends
  
      // Retrieve ListItems if restarted
      protected void retreiveItems() {
          preferences = context.getSharedPreferences("prefName",android.content.Context.MODE_PRIVATE);
          saveitems = preferences.getString("myList", "");
          Log.d("LOG", "Retreived Items : " + saveitems);
  
          try {
              jsonarry = new JSONArray(saveitems);
          } catch (JSONException e) {
              e.printStackTrace();
          }
  
          if (jsonarry == null || jsonarry.length() == 0) {
              return;                                                                        //This checks before setting adapter onCreate if adapter is null
          }
  
          for (int i = 0; i &lt; jsonarry.length(); i++) {
  
              try {
                  jsonobject = jsonarry.getJSONObject(i);
              } catch (JSONException e) {
                  e.printStackTrace();
              }
  
              // get all values here from JSONObject
              title = jsonobject.optString("title");
              subtitle = jsonobject.optString("subtitle");
  
              items.add(new EntryItem(title, subtitle));
              adapter.notifyDataSetChanged();
  
          }
  
      }
      // Retrieve ListItems if restarted ends
  
      // Counter for amount of period of time before flusing adapter
      protected void flushList(){
  
      }
      // Counter for amount of period of time before flusing adapter ends
  
      @Override
      protected void onListItemClick(ListView l, View v, int position, long id) {
  
          if (!items.get(position).isSection()) {
              items.get(position);
              Toast.makeText(this, "You clicked " + position, Toast.LENGTH_SHORT).show();
  
          }
  
          if (position == 9) {
  
          }
  
          super.onListItemClick(l, v, position, id);
      }
  
  
  
  
  }
  </code></pre>
  
  <p>Regards</p>