RecyclerView没有显示任何内容

时间:2017-04-09 06:41:42

标签: java android arrays android-recyclerview

   package com.example.sander.app;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by Sander on 6-4-2017.
 */

public class RecycleFrame extends Fragment {
    ArrayList<String> names = new ArrayList<>();
    ArrayList<String> cPoints = new ArrayList<>();
    ArrayList<String> code = new ArrayList<>();
    ArrayList<String> latitude = new ArrayList<>();
    ArrayList<String> longitude = new ArrayList<>();
    GPSTracker gps;
    ArrayList<Double> dLatitude = new ArrayList<>();
    ArrayList<Double> dLongitude = new ArrayList<>();
    ArrayList<Float> distance = new ArrayList<>();
    ArrayList<Data> dataList = new ArrayList<>();
    public RecycleFrame() {
        // Required empty public constructor
    }

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


    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.fragment_view, menu);
        super.onCreateOptionsMenu(menu,inflater);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Integer id = item.getItemId();
        if(id == R.id.action_A_Z){
            //Sorts the garages from A to Z
            Collections.sort(names);
            //Refreshes the fragment
            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            transaction.detach(this).attach(this).commit();
            return true;
        }
        else if(id == R.id.action_Z_A){
            //Sorts the garages from Z to A
            Collections.reverse(names);
            //Refreshes the fragment
            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            transaction.detach(this).attach(this).commit();
            return true;
        }
        else if (id == R.id.short_distance){
            Collections.sort(distance);
            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            transaction.detach(this).attach(this).commit();
            return true;
        }
        return super.onOptionsItemSelected(item);
        }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        setHasOptionsMenu(true);
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_recycle, container, false);
        final RecyclerView VRecyclerView = (RecyclerView) view.findViewById(R.id.rv_recycler_view);
        RequestQueue rq = Volley.newRequestQueue(getActivity().getApplicationContext());
        String url= "http://test.dontstealmywag.ga/api/parkgarage_all.php";
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        // Do something with the response
                        try{
                            JSONObject o = new JSONObject(response);
                            JSONArray values=o.getJSONArray("");
                            if(names.size() == 0) {
                                for (int i = 0; i < values.length(); i++) {

                                    JSONObject jsonObject = values.getJSONObject(i);
                                    dataList.add(new Data(jsonObject.getString("parkgarage_name"), jsonObject.getString("charging_capcatity"), jsonObject.getDouble("langitude"),
                                            jsonObject.getDouble("longitude"), jsonObject.getString("parkgarage_code"), distance.get(i)));
                                    //names.add(jsonObject.getString("parkgarage_name"));
                                    //cPoints.add(jsonObject.getString("charging_capacity"));
                                    //code.add(jsonObject.getString("parkgarage_code"));
                                    //latitude.add(jsonObject.getString("langitude"));
                                    //longitude.add(jsonObject.getString("longitude"));
                                    dLatitude.add(jsonObject.getDouble("langitude"));
                                    dLongitude.add(jsonObject.getDouble("longitude"));
                                }
                            }
                        }  catch (JSONException ex){}
                        gps = new GPSTracker(getActivity());
                        //check if gps is on
                        if(!gps.canGetLocation()){
                            gps.showSettingsAlert();
                        }
                        Location myLocation = new Location("");
                        myLocation.setLatitude(gps.getLatitude());
                        myLocation.setLongitude(gps.getLongitude());
                        for(int z = 0; z < latitude.size(); z++){
                            Location parkingGarage = new Location("");
                            parkingGarage.setLatitude(dLatitude.get(z));
                            parkingGarage.setLongitude(dLongitude.get(z));
                            distance.add(myLocation.distanceTo(parkingGarage)/1000);
                        }


                        VRecyclerView.setHasFixedSize(true);
                        //RecycleAdapter adapter = new RecycleAdapter(names, cPoints, code, latitude, longitude, distance);
                        RecycleAdapter adapter = new RecycleAdapter(dataList);
                        VRecyclerView.setAdapter(adapter);
                        LinearLayoutManager llm = new LinearLayoutManager(getActivity());
                        VRecyclerView.setLayoutManager(llm);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // Handle error
                    }
                });
        rq.add(stringRequest);
        return view;
    }
}

这是我的RecycleFrame代码,下面是我的RecyclerAdapter代码

    package com.example.sander.app;

import android.app.Fragment;
import android.media.Image;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.lang.reflect.Array;
import java.util.ArrayList;

/**
 * Created by Sander on 6-4-2017.
 */

public class RecycleAdapter extends RecyclerView.Adapter<RecycleAdapter.MyViewHolder> {
    ArrayList<Data> ArrayListData;
    public static class MyViewHolder extends RecyclerView.ViewHolder{
        public CardView mCardView;
        public TextView TextViewNames;

        public MyViewHolder(View v){
            super(v);
            mCardView = (CardView) v.findViewById(R.id.card_view);
            TextViewNames = (TextView) v.findViewById(R.id.text_view_names);

        }

    }
    public RecycleAdapter(ArrayList<Data> names){
        ArrayListData = names;
        //ArrayCharging= cPoints;
        //ArrayCode = code;
        //ArrayLatitude = latitude;
        //ArrayLongitude = longitude;
        //ArrayDistance = distance;
    }
    @Override
    public RecycleAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_card_view, parent, false);
        MyViewHolder vh = new MyViewHolder(v);
        return vh;
    }
    @Override
    public void onBindViewHolder(MyViewHolder holder, int position){

        holder.TextViewNames.setText(String.valueOf(ArrayListData.get(position)));
        //holder.TextViewNames.setText("Naam: " + ArrayNames.get(position));
        //holder.TextViewCharging.setText("Oplaadpunten: " + ArrayCharging.get(position));
        //holder.TextViewCode.setText("Code: " + ArrayCode.get(position));
        //holder.TextViewLatitude.setText("Positie: (" + ArrayLatitude.get(position) + ", " + ArrayLongitude.get(position)+ ")");
        //holder.TextViewDistance.setText(String.valueOf(ArrayDistance.get(position)));
        //holder.mImageView.setImageResource(R.drawable.ic_local_parking_black_24dp);
    }
    @Override
    public int getItemCount() { return ArrayListData.size(); }
}

数据类:

package com.example.sander.app;

/**
 * Created by Sander on 8-4-2017.
 */

public class Data {
    public String names;
    public String cPoints;
    public Double latitude;
    public Double longitude;
    public String code;
    public Float distance;
    public Data(String n, String cP, Double lat, Double lon, String c, Float dis ){
        names = n;
        cPoints = cP;
        latitude = lat;
        longitude = lon;
        code = c;
        distance = dis;
    }
    public String getNames(){
        return names;
    }
}

我想要的是在我的RecyclerView中显示名称,但遗憾的是它没有显示任何内容。任何人都可以帮我解决这个问题

3 个答案:

答案 0 :(得分:1)

 LinearLayoutManager llm = new LinearLayoutManager(getActivity());
 VRecyclerView.setLayoutManager(llm);

删除2行以上并将其放在recycleview的定义下方或设置适配器

之前

答案 1 :(得分:0)

在设置适配器之前将linearlayoutmanager设置为recyclerview,这可能是此处的问题。

答案 2 :(得分:0)

onBindViewHolder()中使用以下代码

@Override
public void onBindViewHolder(MyViewHolder holder, int position){
    ...
    holder.TextViewNames.setText(ArrayListData.get(position).getNames());
    ...
}

您的getNames()方法已经返回String,您不必使用String.valueOf()方法将其转换为String。

希望这会有所帮助。