选中的项目与自定义列表视图中的其余列表一起删除

时间:2017-09-27 10:39:13

标签: android listview sharedpreferences

我的自定义列表视图行有删除按钮。我尝试使用此删除按钮删除listview中的特定项目。 (从收藏夹操作中删除)。我可以删除所选项目,但删除列表的其余部分(所选项目下的项目)。顶部仍然留下来。我怎样才能解决这个问题?提前谢谢。

FavoriteListAdapter:

public class FavoritesListAdapter extends ArrayAdapter<String> {

    private Activity context;
    private List<String> radioName;
    private List<String> listenerNumbers;

    public FavoritesListAdapter(Activity context, List<String> radioName, List<String> listenerNumbers) {
        super(context, R.layout.custom_favorites_listview, radioName);

        this.context=context;
        this.radioName=radioName;
        this.listenerNumbers=listenerNumbers;
    }

    @NonNull
    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {

        View r= convertView;
        FavoritesViewHolder viewHolder = null;

        if (r == null) {
            LayoutInflater layoutInflater= context.getLayoutInflater();
            r= layoutInflater.inflate(R.layout.custom_favorites_listview,null,true);
            viewHolder = new FavoritesViewHolder(r);
            r.setTag(viewHolder);
        }

        else {
            viewHolder = (FavoritesViewHolder) r.getTag();
        }

        viewHolder.row_tv_favorites_radio_name.setText(radioName.get(position));
        viewHolder.row_img_favorites_radio_icon.setImageResource(R.drawable.radio_logo_renkli);
        viewHolder.row_tv_favorites_listener_numbers.setText("Listeners : " + listenerNumbers.get(position));

        viewHolder.button_remove_from_favorites_list.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                listenerNumbers.remove(position);
                radioName.remove(position);

                SharedPreferences settings = context.getSharedPreferences("PREFS", 0);
                final SharedPreferences.Editor editor = settings.edit();
                editor.remove("radio_link"+ position);
                editor.remove("radio_name" + position);
                editor.remove("listener_number" + position);
               // editor.clear();
                editor.commit();
               // FavoritesFragment.radio_name_list.remove(position);
               // FavoritesFragment.radio_link_list.remove(position);
               // FavoritesFragment.listener_numbers.remove(position);
                notifyDataSetChanged();
            }
        });

        return r;

    }

    class FavoritesViewHolder {
        TextView row_tv_favorites_radio_name;
        TextView row_tv_favorites_listener_numbers;
        ImageView row_img_favorites_radio_icon;
        Button button_remove_from_favorites_list;
        FavoritesViewHolder(View v) {
            row_tv_favorites_radio_name= (TextView) v.findViewById(R.id.txt_radio_name_favorites);
            row_tv_favorites_listener_numbers = (TextView) v.findViewById(R.id.txt_listener_numbers_favorites);
            row_img_favorites_radio_icon = (ImageView) v.findViewById(R.id.img_radio_icon_favorites);
            button_remove_from_favorites_list = (Button) v.findViewById(R.id.remove_favorites_from_list_button);
        }
    }

}

在我的PlayRadioFragment中,用户添加了收藏夹按钮,并使用唯一键发送了FavoritesFragment值

 public static int incrementedValue = 0;



     SharedPreferences settings = getActivity().getSharedPreferences("PREFS", 0);
        final SharedPreferences.Editor editor = settings.edit();

        add_favorites_button= (Button) view.findViewById(R.id.add_favorites_button);
        add_favorites_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                editor.putString("radio_link"+ incrementedValue, radio_play_link);
                editor.putString("radio_name" + incrementedValue, radio_name);
                editor.putString("listener_number" + incrementedValue, listener_number);
               // editor.clear();
                editor.commit();

                incrementedValue++;
            }

        });

这是我的FavoritesFragment:

public class FavoritesFragment extends Fragment {



    public static FavoritesListAdapter adapter_2;

    public FavoritesFragment() {
        // Required empty public constructor
    }

    TextView radio_name_txt, radio_link_txt;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=  inflater.inflate(R.layout.fragment_favorites, container, false);


        final List<String> radio_name_list = new ArrayList<>();
        final List<String> radio_link_list = new ArrayList<>();
        final List<String> listener_numbers = new ArrayList<>();



        for (int i=0; i<Integer.MAX_VALUE; i++) {
            SharedPreferences settings = getActivity().getSharedPreferences("PREFS",0);
            String radio_name = settings.getString("radio_name" +i, "");
            String radio_link = settings.getString("radio_link" +i, "");
            String listener_number = settings.getString("listener_number" +i, "");

            if (TextUtils.isEmpty(radio_name)){break;}
            else {radio_name_list.add(radio_name);}

            if (TextUtils.isEmpty(radio_link)){break;}
            else {radio_link_list.add(radio_link);}

            listener_numbers.add(listener_number);
        }



        PlayRadioFragment.incrementedValue = radio_name_list.size();

        ListView listView_favorites = (ListView) view.findViewById(R.id.listview_favorites);
        adapter_2 = new FavoritesListAdapter(getActivity(), radio_name_list, listener_numbers);
        listView_favorites.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                PlayRadioFragment fragment= new PlayRadioFragment();
                Bundle bundle= new Bundle();
                FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
                bundle.putString("radio_play_links", radio_link_list.get(position));
                bundle.putString("radio_name", radio_name_list.get(position));
                bundle.putString("listener_number", listener_numbers.get(position));
                fragment.setArguments(bundle);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.replace(R.id.frame_layout,fragment);
                fragmentTransaction.commit();

            }
        });
        listView_favorites.setAdapter(adapter_2);



        return view;
    }

2 个答案:

答案 0 :(得分:1)

这不会从适配器中删除元素,只能从您的自定义数组中删除:

 listenerNumbers.remove(position);
 radioName.remove(position);

此外,您需要从适配器的内部ArrayList中删除该项。这样做:

remove(getItem(position));

通常,在适配器中管理2个并行自定义数组(在您的情况下为listenerNumbers,radioName)并不是一个好方法。最好定义包含这两个字段的Item类,并将此类对象的ArrayList作为参数提供给适配器(而不是像你的情况那样使用字符串数组)。并且还为项目实现支持XML,它将包含两个项目(TextView和RadioButton)。

答案 1 :(得分:1)

创建您的Object类:

public class DemoGson {

    private  String radio_link,radio_name,listener_number;

    public String getRadio_link() {
        return radio_link;
    }

    public void setRadio_link(String radio_link) {
        this.radio_link = radio_link;
    }

    public String getRadio_name() {
        return radio_name;
    }

    public void setRadio_name(String radio_name) {
        this.radio_name = radio_name;
    }

    public String getListener_number() {
        return listener_number;
    }

    public void setListener_number(String listener_number) {
        this.listener_number = listener_number;
    }
}

向其添加数据时创建新对象并添加数据,然后将该特定对象插入arraylist并将arraylist传递给适配器

ArrayList<DemoGson> arrayList = new ArrayList<>();

        DemoGson demoGson = new DemoGson();
        demoGson.setRadio_link("yourlink");
        demoGson.setRadio_name("yourname");
        demoGson.setListener_number("2");

        arrayList.add(demoGson);

然后

  adapter_2 = new FavoritesListAdapter(getActivity(),arrayList);

并在适配器中删除你的arraylist中的位置