RecycleView没有填充,没有任何错误

时间:2017-04-15 18:34:20

标签: android android-layout android-recyclerview

我创建了一个使用RecycleView的简单应用。所以前端看起来像:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/favoriteWashes"
            android:layout_width="189dp"
            android:layout_height="wrap_content"
            android:background="@color/colorAccent"
            android:text="Wyszukaj" />

        <Spinner
            android:id="@+id/spinner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>


    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:map="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="361dp"
        tools:context="com.example.micha.locationtest.MapsActivity">

    </fragment>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="215dp" />

</LinearLayout>


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="1">

    <TextView
        android:id="@+id/textView"
        android:layout_width="248dp"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="CheckBox" />
</LinearLayout>

Java课程: 适配器:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.WashLocationViewHolder> {
    private List<WashLocation> washLocations;
    Context context;


    public static class WashLocationViewHolder extends RecyclerView.ViewHolder {
        public TextView info;
        public CheckBox favorite;

        public WashLocationViewHolder(View itemView) {
            super(itemView);
            info = (TextView) itemView.findViewById(R.id.textView);
            favorite = (CheckBox) itemView.findViewById(R.id.checkBox);
        }
    }

    public MyAdapter(List<WashLocation> washLocations, Context context) {
        this.washLocations = washLocations;
        this.context = context;
    }

    private Context getContext() {
        return context;
    }

    @Override
    public MyAdapter.WashLocationViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(context);

        // Inflate the custom layout
        View view = inflater.inflate(R.layout.row_recycleview, parent, false);

        // Return a new holder instance
        WashLocationViewHolder viewHolder = new WashLocationViewHolder(view);
        return viewHolder;
    }

    // Involves populating data into the item through holder
    @Override
    public void onBindViewHolder(MyAdapter.WashLocationViewHolder viewHolder, int position) {
        // Get the data model based on position
        WashLocation w = washLocations.get(position);
        String info = w.getWashName() + " - " + w.getCity() + " ul. " + w.getStreet();

        // Set item views based on your views and data model
        TextView textView = viewHolder.info;
        textView.setText(info);

        Integer fav = w.getFav();
        Boolean favorite = fav == 1 ? true : false;
        CheckBox checkBox = viewHolder.favorite;
        checkBox.setChecked(favorite);
    }

    // Returns the total count of items in the list
    @Override
    public int getItemCount() {
        return washLocations.size();
    }
}

onCreate方法:

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        gpsTracker = new GPSTracker(getApplicationContext());
        location = gpsTracker.getLocation();
        lat = location.getLatitude();
        lng = location.getLongitude();
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        List<String> spinnerArray =  new ArrayList<String>();
        spinnerArray.add("100m");
        spinnerArray.add("250m");
        spinnerArray.add("500m");

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                this, android.R.layout.simple_spinner_item, spinnerArray);

        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        Spinner sItems = (Spinner) findViewById(R.id.spinner);
        sItems.setAdapter(adapter);

        mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
        //DataBaseHelper dataBaseHelper = new DataBaseHelper(getApplicationContext());
        //washLocations = dataBaseHelper.getWashLocation();
        washLocations = new ArrayList<>();
        WashLocation w1 = new WashLocation("Clean Car", "52.14276459999999", "21.02135450000003", 1, "Warszawa", "Sojki");
        WashLocation w2 = new WashLocation("Clean Car", "52.14276459999999", "21.02135450000003", 1, "Warszawa", "Sojki");
        washLocations.add(w1);
        washLocations.add(w2);
        mAdapter = new MyAdapter(washLocations, this);
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setLayoutManager(mLayoutManager);

问题在于每当我运行应用程序时,一切正常,但我的RecycleView组件不会被数据填充。

0 个答案:

没有答案