0

i use custom adapter for list view , i want to display an image when click on a item ,

in listView.OnItemClickListener , notifyDataSetChange() not working and not called

i want to change visibility from gone to visible for selected Item

On Item click Listener code :

Music m = (Music) musicManager.getItem(position);
                        m.setPlaying(true);
                        musicManager.notifyDataSetChanged();

custom Adapter getView Method code :

public class MusicManager extends BaseAdapter {
    Context context;
    List<Music> list;
    LayoutInflater layoutInflater;

    public MusicManager(Context context, List<Music> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        try{


        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        ViewHolder viewHolder = new ViewHolder();

        if (view == null){
            view = layoutInflater.inflate(R.layout.list_music,null);
            viewHolder.name = (TextView)view.findViewById(R.id.musicName);
            viewHolder.artist = (TextView)view.findViewById(R.id.musicArtist);
            viewHolder.image = (CircleImageView) view.findViewById(R.id.musicImage);
            viewHolder.imagePlaying = (CircleImageView) view.findViewById(R.id.musicImagePlaying);
        }else viewHolder = (ViewHolder) view.getTag();

        if (list.size() > 0){
            Music music = list.get(position);

            viewHolder.name.setText(music.getName());
            viewHolder.artist.setText(music.getArtist());

            InputStream inputStream = context.getAssets().open(music.getImagePath());
            Drawable drawable = Drawable.createFromStream(inputStream,null);
            viewHolder.image.setImageDrawable(drawable);
            Log.i("Tag", "getView: "+music.isPlaying());
            if (music.isPlaying()){
                viewHolder.imagePlaying.setVisibility(View.VISIBLE);
            }
            else viewHolder.imagePlaying.setVisibility(View.GONE);
        }
        }catch (Exception e){
           // Log.i("Tag", "getView: "+e.toString());
        }
        return view;
    }

    private class ViewHolder{
        TextView name;
        TextView artist;
        CircleImageView image;
        CircleImageView imagePlaying;
    }
}

how i can notify Music Manager for update objects on a item in list view ?

Bahadori
  • 129
  • 1
  • 4
  • 14

1 Answers1

0

UPDATE

Change getItem() method like this:

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

OLD

From your question it is not clear that in which list your making changes. You need to change values in the list which is used in your adapter. Another object of that list would not work. If possible please share your whole code with activity and adapter, For now try something like this:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
               YourListItem  item = (YourListItem)adapterView.getItemAtPosition(i);
               item.setPlaying(true);
               yourAdapter.notifiyDatasetChanged();
            }
        });
AndiM
  • 2,196
  • 2
  • 21
  • 38
  • i test this code but application crashed : music = (Music) musicManager.getItem(position); music.setPlaying(true); musicManager.notifyDataSetChanged(); – Bahadori Sep 04 '17 at 10:46
  • What is the exception? – AndiM Sep 04 '17 at 10:57
  • java.lang.ClassCastException: java.lang.Integer cannot be cast to Music – Bahadori Sep 04 '17 at 11:41
  • What are you passing in your adapter's getItem() method? Please update your question and post whole code. – AndiM Sep 04 '17 at 11:46
  • tested, notifyDataSetChange not called! – Bahadori Sep 04 '17 at 12:19
  • It should call now. What your log says? Is it returning value true after you change it? And also remove if condition inside your getView() method. It is not worth to check size over there. – AndiM Sep 04 '17 at 12:21