RecyclerView不加载数据,不显示任何内容

时间:2017-03-16 16:04:07

标签: java android

我是Android的初学者,并且正在努力实施RecyclerView。正在编写教程并将值更改为我需要的值。但是当我开始时,视图保持空白。就我的计划而言,它当前应该显示相同项目的20倍。

ExerciseList.java (开始活动):

package com.example.erik.orlandoapp;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

public class ExerciseList extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle(getString(R.string.title_activity_exercise_list));
        setContentView(R.layout.activity_exercise_list);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    }
}

ExerciseBlock.java:

package com.example.erik.orlandoapp;

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

public class ExerciseBlock {

    private String title;
    private int difficulty;
    private int exerciseId;

    private static int lastId = 0;

    public ExerciseBlock(String title, int difficulty, int exerciseId) {
        this.title = title;
        this.difficulty = difficulty;
        this.exerciseId = exerciseId;
    }

    public String getTitle() {
        return title;
    }

    public int getDifficulty() {
        return difficulty;
    }

    public int getExerciseId() {
        return exerciseId;
    }

    public static ArrayList<ExerciseBlock> createContactsList(int numContacts) {
        ArrayList<ExerciseBlock> exerciseBlocks = new ArrayList<ExerciseBlock>();

        for (int i = 1; i <= numContacts; i++) {
            exerciseBlocks.add(new ExerciseBlock("Title",1,1));
        }

        return exerciseBlocks;
    }

}

ExerciseBlockAdapter.java:

package com.example.erik.orlandoapp;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;


public class ExerciseBlockAdapter extends RecyclerView.Adapter<ExerciseBlockAdapter.ViewHolder> {

    private List<ExerciseBlock> exList;
    private Context mContext;

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(context);
        View contactView = inflater.inflate(R.layout.exercise_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(contactView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {
        ExerciseBlock exBlock = exList.get(position);
        TextView textView = viewHolder.getTxtDiff();
        textView.setText(exBlock.getDifficulty());

        textView = viewHolder.getTxtExercise();
        textView.setText(exBlock.getExerciseId());
    }

    @Override
    public int getItemCount() {
        return exList.size();
    }

    public ExerciseBlockAdapter(Context context, List<ExerciseBlock> exList) {
        this.exList = exList;
        this.mContext = context;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        private TextView txtExercise;
        private TextView txtDiff;

        public TextView getTxtExercise() {
            return this.txtExercise;
        }

        public TextView getTxtDiff() {
            return this.txtDiff;
        }

        public ViewHolder(View itemView) {
            super(itemView);

            txtDiff = (TextView) itemView.findViewById(R.id.txtExDiff);
            txtExercise = (TextView) itemView.findViewById(R.id.txtExName);
        }
    }
}

UserListActivity:

package com.example.erik.orlandoapp;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;

public class UserListActivity extends AppCompatActivity {

    ArrayList<ExerciseBlock> exerciseBlocks;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        RecyclerView rvContacts = (RecyclerView) findViewById(R.id.exerciseList);

        exerciseBlocks = ExerciseBlock.createContactsList(20);
        ExerciseBlockAdapter adapter = new ExerciseBlockAdapter(this, exerciseBlocks);
        rvContacts.setAdapter(adapter);
        rvContacts.setLayoutManager(new LinearLayoutManager(this));
    }
}

activity_exercise_list.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.example.erik.orlandoapp.ExerciseList">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="@dimen/appbar_padding_top"
            android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/AppTheme.PopupOverlay">

            </android.support.v7.widget.Toolbar>

        </android.support.design.widget.AppBarLayout>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/exerciseList"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:layout_marginBottom="20dp"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    </LinearLayout>

</android.support.design.widget.CoordinatorLayout>

exercise_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:weightSum="1">


    <TextView
        android:id="@+id/txtExName"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Whatever" />

    <TextView
        android:id="@+id/txtExDiff"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/colorDifEasy"
        android:paddingBottom="6dp"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:paddingTop="6dp"
        android:text="Leicht"
        android:textSize="10sp" />


</LinearLayout>

非常感谢!

1 个答案:

答案 0 :(得分:0)

我没有看到UserListActivity调用setContentView() 将此代码放在ExersiceList活动

$query = "SELECT name, details FROM recent_properties";

$result = $mysqli->query($query);

if($result->num_rows()>0)
{
    $html = "<table>";
    $html .= '<tr><th>Name</th><th>Details</th></tr>';
    while ($row = $result->fetch_assoc()) {
        $html .= '<tr>';
        $html .= '<td>' . $row["name"] . '<td>';
        $html .= '<td>' . $row["details"] . '<td>';
        $html .= '</tr>';
    }
    $html .= "</table>";
    echo $html;
}

$mysqli->close();

或在UserListActivity上调用setContentView(R.layout.activity_exercise_layout)