Listview项目顺序错误,订单不断变化

时间:2017-09-29 09:25:54

标签: android listview hashmap sharedpreferences

我有收藏夹片段和FavoritesListAdapter。在PlayRadioFragment中,可以使用按钮添加收藏夹列表。当我这样做时,收藏夹列表视图以错误的顺序填充。并且无线电名称和无线电播放链接无法正确匹配。与删除收藏夹按钮相同。我想这是因为使用HashMap获取键值。我该怎么办?提前谢谢。

PlayRadioFragment中的add_favorites_button:

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

        SharedPreferences settings = getActivity().getSharedPreferences("PREFS_", 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(radio_name, radio_name);
        editor.commit();

        SharedPreferences settings_2 = getActivity().getSharedPreferences("PREFS_2",0);
        SharedPreferences.Editor editor_2 = settings_2.edit();
        editor_2.putString(radio_play_link, radio_play_link);

        editor_2.commit();

        }

        });

键值在FavoritesFragment中发送并发送到FavoriteListAdapter:

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

        String radio_name = "";
        String radio_name_key = "";
        String radio_link = "";
        String radio_link_key = "";

        Map<String, ?> allEntries = getActivity().getSharedPreferences("PREFS_",0).getAll();
        for (Map.Entry<String, ?> entry : allEntries.entrySet()) {
        radio_name_key = entry.getKey();
        radio_name_list_key.add(radio_name_key);
        radio_name = entry.getValue().toString();
        radio_name_list.add(radio_name);
        }

        Map<String, ?> allEntries_2 = getActivity().getSharedPreferences("PREFS_2",0).getAll();
        for (Map.Entry<String, ?> entry : allEntries_2.entrySet()) {
        radio_link_key = entry.getKey();
        radio_link_list_key.add(radio_link_key);

        radio_link = entry.getValue().toString();
        radio_link_list.add(radio_link);
        }



        ListView listView_favorites = (ListView) view.findViewById(R.id.listview_favorites);
        adapter_2 = new FavoritesListAdapter(getActivity(), radio_name_list, radio_name_list_key, radio_link_list, radio_link_list_key);
        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));
        fragment.setArguments(bundle);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.replace(R.id.frame_layout,fragment);
        fragmentTransaction.commit();

        }
        });

和FavoriteListAdapter,相关的广播和链接正在删除列表元素上的删除按钮:

public class FavoritesListAdapter extends ArrayAdapter<String > {

    private Activity context;
    private List<String> radioName;
    private List<String> radio_links;
    private List<String> radio_links_key;
    private List<String> radioname_key;


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

        this.context=context;

        this.radio_links_key= radio_links_key;
        this.radio_links= radio_links;
        this.radioName=radioName;
        this.radioname_key= radioname_key;

    }

    @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.button_remove_from_favorites_list.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                SharedPreferences settings = context.getSharedPreferences("PREFS_", 0);
                SharedPreferences.Editor editor = settings.edit();
                editor.remove(radioName.get(position));
                editor.remove(radioname_key.get(position));
                editor.commit();

                SharedPreferences settings_2 = context.getSharedPreferences("PREFS_2",0);
                SharedPreferences.Editor editor_2 = settings_2.edit();
                editor_2.remove(radio_links.get(position));
                editor_2.remove(radio_links_key.get(position));
                editor_2.commit();

                radioName.remove(getItem(position));
                //  notifyDataSetChanged();

                FavoritesFragment.adapter_2.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);
        }
    }



}

0 个答案:

没有答案