1

I want to have a heart without color in the listView when I clicked on it, its color turns red and save when exit the app.

enter image description here

The StructAnatomy class:

public class StructAnatomy {

    public String title;

    //public ImageView heart_empty;

    //public boolean   done;
}

THe AdapterAnatomy class:

the heart_impty is an image that no color and heart_fill has color.

public class AdapterAnatomy extends ArrayAdapter<StructAnatomy> {

    public AdapterAnatomy(ArrayList<StructAnatomy> array) {

        super(G.context, R.layout.adapter_anatomy, array);
    }
    public static class ViewHolder {

        public ViewGroup layoutRoot;
        public TextView  txtTitle;

        //public ImageView heart_empty;

         public ViewHolder(View view) {
            layoutRoot = (ViewGroup) view.findViewById(R.id.layoutRoot);
            txtTitle = (TextView) view.findViewById(R.id.txtTitle);
            //heart_empty.setImageResource(R.drawable.heart_fill);
        }


        public void fill(ArrayAdapter<StructAnatomy> adapter, StructAnatomy item, final int position) {
            txtTitle.setText(item.title);
            //heart_empty.setEnabled(item.done);
            layoutRoot.setOnClickListener(new OnClickListener() {

             //******************************************************************************************
                @Override
                public void onClick(View arg0) {
                    Intent intent = new Intent(G.currentActivity, Anatomy1_2.class);
                    intent.putExtra("POSITION", position);
                    G.currentActivity.startActivity(intent);
                 }

            });
        }
     }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        StructAnatomy item = getItem(position);
        if (convertView == null) {
            convertView = G.inflater.inflate(R.layout.adapter_anatomy, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.fill(this, item, position);
        return convertView;
    }
}
Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
gadolf
  • 1,035
  • 11
  • 19

3 Answers3

1
  • When you click on listview getPosition(getChildAt(position)) and from that you can set color
  • Now on exit you need to save this to sharedPreferences.

EX:

if(Your condition){    //if it is true

 img.setImageResource(R.drawable.my_first_image); //replace image

}else{                 

 img.setImageResource(R.drawable.my_second_image);

}

To change color of listview item you can use

view.setBackgroundColor(Color.parseColor("#08A3F5"));

Please check this link for more detail.

Community
  • 1
  • 1
Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
  • I need just change the heart color by onClick on the heart_empty image. – gadolf Mar 15 '16 at 09:45
  • I'm a little confused, should I check first. I'll check back later. I have two pictures of the heart that would be pasted together. – gadolf Mar 15 '16 at 12:54
  • how you want this to work now ? when you click it will change color and than on exit save it to sharedpreference so when you open application it's state will be restored. it will be same as you have done while exit. – Amit Vaghela Mar 15 '16 at 12:58
  • I don't want change color, I want change image (I have two image of heart, first heart has no color (gray) and second heart has red color). As shown in the photo above. I started programming fresh and I copying some codes and I'm not fluent. – gadolf Mar 15 '16 at 16:55
  • Than you need to check condition, use if else and replace image as you require. – Amit Vaghela Mar 15 '16 at 16:58
  • I do't understand your mean, my English is not good . I want to Favourites the user, be replaced with a red heart. – gadolf Mar 15 '16 at 17:11
  • i got it. please check edited answer. check if else condition and implement accordingly. @gadolf – Amit Vaghela Mar 16 '16 at 11:28
1

To change the color of your heart on click , write

heartViww.setBackgroundColor(Color.parseColor("#ff0000"));

If you want to save the color of selected heart even if exit from the app then you can save one boolean in sharedpreference and again when come back to app in your adapter constructor check that boolean from shared preference.

0

I use these codes and my goal is satisfied:

public void heart(int heartId, int listViewId) {
    final String NameId = listViewId + "";
    final ImageView heart = (ImageView) findViewById(heartId);

    if (AppPrefrances.getInstance(getApplicationContext()).getClicked(NameId).equals("1")) {
        heart.setImageResource(R.drawable.heart_fill);
    } else {
        heart.setImageResource(R.drawable.heart_empty);
    }

    heart.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View V) {

            if (AppPrefrances.getInstance(getApplicationContext()).getClicked(NameId).equals("1")) {
                heart.setImageResource(R.drawable.heart_empty);
                AppPrefrances.getInstance(getApplicationContext()).setClicked(NameId, "0");
            } else {
                heart.setImageResource(R.drawable.heart_fill);
                AppPrefrances.getInstance(getApplicationContext()).setClicked(NameId, "1");
            }

        }
    });
}
gadolf
  • 1,035
  • 11
  • 19