sharedPreference OnItemClick

时间:2016-12-11 09:29:01

标签: android sharedpreferences

我有2个片段,一个包含List类,另一个是从列表中共享List的Favorites类。

如何从收藏夹片段中打开正确的活动

最喜欢的课程

public class FavoriteListFragment extends Fragment {

public static final String ARG_ITEM_ID = "favorite_list";
ListView favoriteList;
SharedPreference sharedPreference;
List<Station> favorites;
Activity activity;
ListAdapter listAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    activity = getActivity();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_station_list, container,
            false);
    // Get favorite items from SharedPreferences.
    sharedPreference = new SharedPreference();
    favorites = sharedPreference.getFavorites(activity);

    if (favorites == null) {
        showAlert(getResources().getString(R.string.no_favorites_items),
                getResources().getString(R.string.no_favorites_msg));
    } else {

        if (favorites.size() == 0) {
            showAlert(
                    getResources().getString(R.string.no_favorites_items),
                    getResources().getString(R.string.no_favorites_msg));
        }

        favoriteList = (ListView) view.findViewById(R.id.list_product);
        if (favorites != null) {
            listAdapter = new ListAdapter(activity, favorites);
            favoriteList.setAdapter(listAdapter);

            favoriteList.setOnItemClickListener(new OnItemClickListener() {

                public void onItemClick(AdapterView<?> parent, View arg1,int position, long arg3) {
                    Intent i;
                    switch (position)
                    {
                        case 0:
                            i = new Intent(getActivity(), Citizen.class);
                            startActivity(i);
                            break;
                        case 1:
                            i = new Intent(getActivity(), Ntv.class);
                            startActivity(i);
                            break;
                    }


                }
            });

            favoriteList
                    .setOnItemLongClickListener(new OnItemLongClickListener() {

                        @Override
                        public boolean onItemLongClick(
                                AdapterView<?> parent, View view,
                                int position, long id) {

                            ImageView button = (ImageView) view
                                    .findViewById(R.id.imgbtn_favorite);

                            String tag = button.getTag().toString();
                            if (tag.equalsIgnoreCase("grey")) {
                                sharedPreference.addFavorite(activity,
                                        favorites.get(position));
                                Toast.makeText(
                                        activity,
                                        activity.getResources().getString(
                                                R.string.add_favr),
                                        Toast.LENGTH_SHORT).show();

                                button.setTag("red");
                                button.setImageResource(R.drawable.star_fill);
                            } else {
                                sharedPreference.removeFavorite(activity,
                                        favorites.get(position));
                                button.setTag("grey");
                                button.setImageResource(R.drawable.star_blank);
                                listAdapter.remove(favorites
                                        .get(position));
                                Toast.makeText(
                                        activity,
                                        activity.getResources().getString(
                                                R.string.remove_favr),
                                        Toast.LENGTH_SHORT).show();
                            }
                            return true;
                        }
                    });
        }
    }
    return view;
}

public void showAlert(String title, String message) {
    if (activity != null && !activity.isFinishing()) {
        AlertDialog alertDialog = new AlertDialog.Builder(activity)
                .create();
        alertDialog.setTitle(title);
        alertDialog.setMessage(message);
        alertDialog.setCancelable(false);

        // setting OK Button
        alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK",
                new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        // activity.finish();
                        getFragmentManager().popBackStackImmediate();
                    }
                });
        alertDialog.show();
    }
}

@Override
public void onResume() {
    getActivity().setTitle(R.string.favorites);
    getActivity().getActionBar().setTitle(R.string.favorites);
    super.onResume();
}
}

列出类

public class ListFragment extends Fragment implements
    OnItemClickListener, OnItemLongClickListener {

public static final String ARG_ITEM_ID = "product_list";

Activity activity;
ListView stationListView;
List<Station> stations;
ListAdapter listAdapter;

SharedPreference sharedPreference;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    activity = getActivity();
    sharedPreference = new SharedPreference();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) 
{
    View view = inflater.inflate(R.layout.fragment_station_list, container,
            false);
    findViewsById(view);


    setStations();

    listAdapter = new ListAdapter(activity, stations);
    stationListView.setAdapter(listAdapter);
    stationListView.setOnItemClickListener(this);
    stationListView.setOnItemLongClickListener(this);
    return view;
}
private void findViewsById(View view) {
    stationListView = (ListView) view.findViewById(R.id.list_product);
}

private void setStations() {

    Station station1 = new Station(1, R.drawable.ic_launcher, "Ntv", "Programme",1);
    Station station2 = new Station(2, R.drawable.albania_tave, "Ntv", "Programme",2);
    Station station3 = new Station(3, R.drawable.albania_tave,"Kiss tv", "Programme",3);
    Station station4 = new Station(4, R.drawable.albania_tave, "Ntv", "Programme",4);
    Station station5 = new Station(5, R.drawable.ic_launcher, "Ntv", "Citizen",5);
    Station station6 = new Station(6, R.drawable.ic_launcher, "Citizen", "Citizen",6);
    Station station7 = new Station(7, R.drawable.ic_launcher, "Ntv", "Programme",7);
    Station station8 = new Station(8,R.drawable.ic_launcher, "Ntv", "Programme",8);
    Station station9 = new Station(9,R.drawable.ic_launcher, "Ntv", "Programme",9);
    Station station10 = new Station(10,R.drawable.ic_launcher,"Ntv", "Programme",10);

    stations = new ArrayList<Station>();
    stations.add(station1);
    stations.add(station2);
    stations.add(station3);
    stations.add(station4);
    stations.add(station5);
    stations.add(station6);
    stations.add(station7);
    stations.add(station8);
    stations.add(station9);
    stations.add(station10);
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Intent i;
    switch (position)
    {
        case 0:
            i = new Intent(getActivity(), Citizen.class);
            startActivity(i);
            break;
        case 1:
            i = new Intent(getActivity(), Ntv.class);
            startActivity(i);
            break;
    }
}


@Override
public boolean onItemLongClick(AdapterView<?> arg0, View view, int position, long arg3) {
    ImageView button = (ImageView) view.findViewById(R.id.imgbtn_favorite);

    String tag = button.getTag().toString();
    if (tag.equalsIgnoreCase("grey")) {
        sharedPreference.addFavorite(activity, stations.get(position));
        Toast.makeText(activity,
                activity.getResources().getString(R.string.add_favr),
                Toast.LENGTH_SHORT).show();

        button.setTag("red");
        button.setImageResource(R.drawable.star_fill);
    } else {
        sharedPreference.removeFavorite(activity, stations.get(position));
        button.setTag("grey");
        button.setImageResource(R.drawable.star_blank);
        Toast.makeText(activity,
                activity.getResources().getString(R.string.remove_favr),
                Toast.LENGTH_SHORT).show();
    }

    return true;
}

@Override
public void onResume() {
    getActivity().setTitle(R.string.app_name);
    getActivity().getActionBar().setTitle(R.string.app_name);
    super.onResume();
}
}

0 个答案:

没有答案