0

In my app i want to change the imageview in the listview on which the user click.in starting i pass the image in the listview throught the adapter class,but on click on the item i want to change the image in the imageview in listview later,how i can change the image at the particluar position.??

MainActivity

public class MainActivity extends Activity {
     ListView listView;
 public static int pos = 0;
    String[] Prayers = new String[] { "REFLETINDO A IMAGEM DE CRISTO",
            "PROVIDÊNCIA PARA TODOS OS CASOS", "A CRUZ REVELA O AMOR DE DEUS",
            "REFLETINDO O AMOR DE CRISTO", "CRISTO CRUCIFICADO POR NÓS",
            "AMOR IMENSURÁVEL", "DÁDIVA DO AMOR DE DEUS",
            "NÃO PARA CONDENAR, MAS PARA SALVAR",
            "O CENTRO DE MINHA ESPERANÇA",


    };

    String[] Dias = new String[] { "Dia1", "Dia2", "Dia3", "Dia4", "Dia5",
            "Dia6", "Dia7", "Dia8", "Dia9", "Dia10", "Dia11", "Dia12",
            "Dia13", "Dia14", "Dia15", "Dia16", "Dia17", "Dia18",


    };
    LinearLayout lay1 ,lay2;
    int i=0;
    int img ;

    List<RowItem> rowItems;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);

       img = R.drawable.praying_hands_normal ;


       rowItems = new ArrayList<RowItem>();
       for (int i = 0; i < 50; i++) {
           RowItem item = new RowItem( Dias[i], Prayers[i],img);
           rowItems.add(item);
       }

       listView = (ListView) findViewById(R.id.listview);
       BaseClass adapter = new BaseClass(this, rowItems);
       listView.setAdapter(adapter);
       //listView.setOnItemClickListener(this);

            // get the position of the clicked item 
        View t = null;
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    // TODO Auto-generated method stub

                    pos = position;
                    //Toast.makeText(getApplication(), String.valueOf(pos), Toast.LENGTH_LONG).show();
                    Intent i = new Intent(MainActivity.this ,OpenActivity.class);
                    startActivity(i);

                }
            });

    }

Adapterclass

    public View getView(int position, View convertView, ViewGroup parent) {
                ViewHolder holder = null;

                LayoutInflater mInflater = (LayoutInflater) context
                        .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
                if (convertView == null) {
                    convertView = mInflater.inflate(R.layout.list, null);
                    holder = new ViewHolder();

                    holder.txtDias = (TextView) convertView.findViewById(R.id.dias);
                    holder.txtPrayers = (TextView) convertView
                            .findViewById(R.id.prayers);
                    holder.imageView = (ImageView) convertView
                            .findViewById(R.id.symbol);
                    convertView.setTag(holder);
                } else {
                    holder = (ViewHolder) convertView.getTag();
                }

                RowItem rowItem = (RowItem) getItem(position);

                holder.txtPrayers.setText(rowItem.getDesc());
                holder.txtDias.setText(rowItem.getTitle());
                holder.imageView.setImageResource(R.drawable.praying_hands_normal);
          if(pos == position && OpenActivity.a + OpenActivity.b +
                  OpenActivity.c == 1 || OpenActivity.a + OpenActivity.b +
                  OpenActivity.c == 2)
          {
              holder.imageView.setImageResource(R.drawable.praying_hands_bookmark);

          } 

          else if(pos == position && OpenActivity.a + OpenActivity.b +
                  OpenActivity.c == 3 )
          {
              holder.imageView.setImageResource(R.drawable.praying_hands_selected);
          }


        return convertView;

}
user3683036
  • 135
  • 1
  • 2
  • 15

2 Answers2

0

The parameters in the onItemClick() method has "View view" which afaik is the item you pressed. view.findViewById() focuses on that row.

Try this.

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

     // Add following to your listener
     ImageView image = (ImageView) view.findViewById( R.id.item_image );
     image.setBackground( getResources().getDrawable( R.drawable.new_item_image ));

     // Or
     Drawable img = ( Drawable ) getResources().getDrawable( R.drawable.new_item_image ));
     image.setBackground( img );

   }
});

Edit: to keep the item changed when you return from your intent, try this

In MainActivity, add an View, Context & ImageView,

public class MainActivity extends Activity {
  public View row;
  public Context ctx;
  public ImageView newImage;

  // your code 

In the onItemClickListener add this,

  row = view; 
  ctx = getBaseContext();
  newImage = ( ImageView ) getResources().getDrawable( R.drawable.new_item_image ));

Now add a new method called something like "setImage" (in MainActivity class),

public void setImage( View row ) {
  ImageView item_image = ( ImageView ) row.findViewById( R.id.item_image );
  item_image.setBackground( newImage );
}

When you leave your new activity, call this first,

MainActivity.setImage( MainActivity.row );

Tell me if this worked, I have not tried it myself.

Jonas Borggren
  • 2,591
  • 1
  • 22
  • 40
  • @ Jonas B it changing image on click.,but when i return back to activity from another activity..,the image i change on click got disappear and previous image get visible..,how can i fix the new image permanently on click..?? – user3683036 Jul 16 '14 at 05:11
  • @ Jonas B by update my adapter class i getting the image change..,but when i click on another item the image changed for previously clicked item to default..,how to change it permanently..please help me in that. – user3683036 Jul 16 '14 at 13:08
  • http://stackoverflow.com/questions/24781709/change-image-of-listivews-item-permanently here is the link – user3683036 Jul 16 '14 at 13:12
0

the ListView refresh after you come back to Activity, in this time, each item's getView() would execute but your RowItem still hold the old ImageId, that's why the previous image get visible, just modify the RowItem's imageId when item clicking :

@Override 
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    RowItem rowItem = rowItems.get(position);
    rowItem.setImageId(R.drawable.new_item_image);

    Intent i = new Intent(MainActivity.this ,OpenActivity.class);
    startActivity(i);
}

--------------- Update at 3 hours later ---------------

save the BaseClass outside of onCreate() method, notify dataset changed at onResume() :

public class MainActivity extends Activity {

    private BaseClass adapter;

    protected void onCreate(Bundle savedInstanceState) {
        ...
        adapter = new BaseClass(this, rowItems);
        ...
    }

    @Override
    public void onResume() {
        super.onResume();
        adapter.notifyDataSetChanged();
    }

}

by the way, you should post the BaseClass code as well.

VinceStyling
  • 3,707
  • 3
  • 29
  • 44