Search code examples
javaandroidlistviewandroid-listviewlistviewitem

List view behave anonymously


I am working on list view i did create custom list view. List view item contains arrow that points the selected list row, text, separator line and a delete button. First of all arrows are invisible and then the selected child's arrow view is visible. But when i am going to visible it it didn't work. I did try a number of time by cleaning project build it again but it didn't work and i'm not getting the main problem so need help. Here is my code.

public class MusicListadaper extends BaseAdapter {

    int previouspostion=-1;
    Activity context;
    ArrayList<Item> soundItems;
    LayoutInflater inflater;
    View Music_List_Layout,root_view;
    public TextView listview_arrow, listview_text, listview_seperator;
    boolean isDeleteButtonVisibility = false;
    MusicPlay musicPlayObj;
    PlaySounds playSounds;
    public MusicListadaper(Activity context, ArrayList<Item> listItems,
            boolean isDeleteButtonVisible, MusicPlay musicPlayObj) {
        this.context = context;
        soundItems = listItems;
        this.musicPlayObj = musicPlayObj;
        playSounds = new PlaySounds(context);
        this.isDeleteButtonVisibility = isDeleteButtonVisible;
        root_view=context.getWindow().getDecorView().findViewById(android.R.id.content);

    }

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

    @Override
    public MusicListadaper getItem(int arg0) {
        return this;
    }

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        inflater = (LayoutInflater) context
                .getSystemService(context.LAYOUT_INFLATER_SERVICE);


        // Log.d("Total Postions", soundItems.size()+"");
        if (convertView == null) {
            Music_List_Layout = inflater.inflate(R.layout.listview_items, null);
            listview_arrow = (TextView) Music_List_Layout
                    .findViewById(R.id.listview_arrow);
            listview_arrow.setTag("Arrow_"+position);

            listview_text = (TextView) Music_List_Layout
                    .findViewById(R.id.listview_text);
            listview_text.setTypeface(Font.getListItemTextTypeFace());
            listview_seperator = (TextView) Music_List_Layout
                    .findViewById(R.id.listview_seperator);
            ImageView btn_delete_songs = (ImageView) Music_List_Layout
                    .findViewById(R.id.btn_delete_songs);
            View listitem_main_layout = Music_List_Layout
                    .findViewById(R.id.listitem_main_layout);
            if (!isDeleteButtonVisibility) {
                btn_delete_songs
                        .setBackgroundResource(R.drawable.btn_add_selector);
            } else {
                listitem_main_layout
                        .setBackgroundResource(R.drawable.template_musicplay_selector);
            }
            listview_arrow.setVisibility(View.INVISIBLE);
            btn_delete_songs.setTag("btn_" + position);
            listitem_main_layout.setTag("btn_" + position);
            // *******************************Implemetn click listner
            btn_delete_songs.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    String tag = (String) v.getTag();
                    String[] arr = tag.split("_");
                    GalleryMusicDbHandler dbManager = new GalleryMusicDbHandler(
                            context, new KeysString());
                    if (isDeleteButtonVisibility) {
                        try {

                            dbManager.deleteSpecificSongFromDb(soundItems
                                    .get(Integer.parseInt(arr[1])));
                            soundItems.remove(Integer.parseInt(arr[1]));

                            musicPlayObj.updateMainListSong(
                                    Integer.parseInt(arr[1]),
                                    isDeleteButtonVisibility);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    } else {

                        if (dbManager.insertGallerySong(soundItems.get(Integer
                                .parseInt(arr[1])))) {
                            Toast.makeText(context,
                                    "Congratulation Song is added",
                                    Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(context,
                                    "Already added to PlayList",
                                    Toast.LENGTH_SHORT).show();
                        }
                        musicPlayObj.updateMainListSong(
                                Integer.parseInt(arr[1]),
                                isDeleteButtonVisibility);
                    }

                }
            });
            listview_text.setText(soundItems.get(position).getTitle());
            Log.d("Position", position + " "
                    + soundItems.get(position).getTitle());

            if (isDeleteButtonVisibility) {
                listitem_main_layout.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        TextView view;
                        if(previouspostion!=-1){
                            view=(TextView) root_view.findViewWithTag("Arrow_"+previouspostion);
                            view.setVisibility(View.INVISIBLE);
                        }
                        view=(TextView) root_view.findViewWithTag("Arrow_"+(KeysInteger.NUMOFMUSIC+position));
                        view.setVisibility(View.VISIBLE);
                        previouspostion=(KeysInteger.NUMOFMUSIC+position);


                    }
                });
            }

        }

        return Music_List_Layout;
    }

    public void setVisibleArrowButton() {

    }

}

Here is my complete code in which listitem_main_layout Click listener didn't work properly i need help.


Solution

  • call notifydatasetchanged method to reslove this issue