从具有listview适配器的片段移动到所选的每个项目的新活动基础

时间:2016-05-23 22:11:29

标签: java android listview android-activity

我已经完成了一系列关于如何在片段活动中使listview的每个项目移动到另一个具有swipeListadapter的getView()的活动的研究。下面的代码包含选项卡片段,其中包含列表视图的滑动listadapter和setonitemclicklistener。

public class SwipeListAdapter extends BaseAdapter {
    private Activity activity;
    private LayoutInflater inflater;
    private List<Movie> movieList;
    private String[] bgColors;


    public SwipeListAdapter(Activity tab1, List<Movie> movieList) {
        this.activity = tab1;
        this.movieList = movieList;
        bgColors = activity.getApplicationContext().getResources().getStringArray(R.array.movie_serial_bg);
    }

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

    @Override
    public Object getItem(int location) {
        return movieList.get(location);
    }

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

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

        if (inflater == null)
            inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null)
            convertView = inflater.inflate(R.layout.list_rows, null);

        TextView serial = (TextView) convertView.findViewById(R.id.serial);
        TextView title = (TextView) convertView.findViewById(R.id.title);

        serial.setText(String.valueOf(movieList.get(position).id));
        title.setText(movieList.get(position).title);

        String color = bgColors[position % bgColors.length];
        serial.setBackgroundColor(Color.parseColor(color));

        return convertView;
    }

}

下面的代码是我的片段标签类

public class Tab1 extends Fragment implements ViewSwitcher.ViewFactory, SwipeRefreshLayout.OnRefreshListener {

    private int index;
    private int[] images = new int[] { R.drawable.gallery1, R.drawable.gallery2, R.drawable.gallery3, R.drawable.gallery4, R.drawable.gallery5, R.drawable.gallery6, R.drawable.gallery7, R.drawable.gallery8 };
    ImageSwitcher switcher;
    android.os.Handler Handler = new Handler();

    private SwipeRefreshLayout swipeRefreshLayout;
    private SwipeListAdapter adapter;
    private List<Movie> movieList;
    private ListView listView;

   // private static final String url = "http://api.androidhive.info/json/movies.json";

    private String URL_TOP_250 = "http://192.177.53.152/locator/test/refractor.php?offset=";

    // initially offset will be 0, later will be updated while parsing the json
    private int offSet = 0;

    private static final String TAG = Tab1.class.getSimpleName();

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View vi = inflater.inflate(R.layout.tab_1,container,false);


        listView = (ListView) vi.findViewById(R.id.list);

        listView.setBackgroundColor(Color.WHITE);

        swipeRefreshLayout = (SwipeRefreshLayout) vi.findViewById(R.id.swipe_refresh_layout);

        movieList = new ArrayList<>();
        adapter = new SwipeListAdapter(getActivity(), movieList);
        listView.setAdapter(adapter);

        //getView().setOnClickListener();

        swipeRefreshLayout.setOnRefreshListener(this);

        swipeRefreshLayout.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        swipeRefreshLayout.setRefreshing(true);

                                        fetchMovies();
                                    }
                                }
        );



        return vi;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {


                switch(position) {
                    case 1:
                        intent = new Intent(getActivity().getApplicationContext(), New1.class);
                        startActivity(intent);
                        break;
                    case 2:
                        intent = new Intent(getActivity().getApplicationContext(), New2.class);
                        startActivity(intent);
                        break;
                    default:
                        intent = new Intent(getActivity().getApplicationContext(), New3.class);
                        startActivity(intent);
                }


            }
        });


        switcher = (ImageSwitcher) getActivity().findViewById(R.id.imageSwitcher1);
        switcher.setFactory(this);

        switcher.setImageResource(images[index]);
        switcher.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                index++;
                if (index >= images.length) {
                    index = 0;
                }
                switcher.setImageResource(images[index]);
            }
        });
        switcher.setInAnimation(AnimationUtils.loadAnimation(getActivity(), android.R.anim.fade_in));
        switcher.setOutAnimation(AnimationUtils.loadAnimation(getActivity(), android.R.anim.fade_out));

        //auto change image
        Handler.post(UpdateImage);

    }




    @Override
    public void onRefresh() {
        fetchMovies();
    }


    private void fetchMovies() {

        // showing refresh animation before making http call
        swipeRefreshLayout.setRefreshing(true);

        // appending offset to url
        String url = URL_TOP_250 + offSet;

        // Volley's json array request object
        JsonArrayRequest req = new JsonArrayRequest(url,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        Log.d(TAG, response.toString());

                        if (response.length() > 0) {

                            // looping through json and adding to movies list
                            for (int i = 0; i < response.length(); i++) {
                                try {
                                    JSONObject movieObj = response.getJSONObject(i);

                                    int rank = movieObj.getInt("rank");
                                    String title = movieObj.getString("postTitle");

                                    Movie m = new Movie(rank, title);

                                    movieList.add(0, m);

                                    // updating offset value to highest value
                                    if (rank >= offSet)
                                        offSet = rank;

                                } catch (JSONException e) {
                                    Log.e(TAG, "JSON Parsing error: " + e.getMessage());
                                }
                            }

                            adapter.notifyDataSetChanged();
                        }

                        // stopping swipe refresh
                        swipeRefreshLayout.setRefreshing(false);

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(TAG, "Server Error: " + error.getMessage());

                Toast.makeText(getActivity().getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();

                // stopping swipe refresh
                swipeRefreshLayout.setRefreshing(false);
            }
        });

        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(req);
    }


    Runnable UpdateImage = new Runnable() {
        public void run() {
            // Increment index
            index++;
            if (index > (images.length - 1)) {
                index = 0;
            }
            switcher.setImageResource(images[index]);
            // Set the execution after 5 seconds
            Handler.postDelayed(this, (3 * 1000));
        }
    };

    @Override
    public View makeView() {
        ImageView myView = new ImageView(getActivity());
        myView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        myView.setLayoutParams(new ImageSwitcher.LayoutParams(Gallery.LayoutParams.
                FILL_PARENT, Gallery.LayoutParams.FILL_PARENT));
        return myView;
    }

}

简而言之,每当我点击任何项目列表视图时,应用程序崩溃并且系统logcat没有给出任何线索。我希望能够在d片段中单击列表视图上的项目并将其定向到新活动。任何帮助将不胜感激。感谢。

0 个答案:

没有答案