我一遍又一遍地检查了代码。我没有收到任何错误,但我的recylcerview没有显示我的数据。任何帮助,将不胜感激。
这是我的layout / Activity,它包含了recyclerview对象:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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:layout_margin="16dp"
tools:context="com.checkinsystems.ez_score.MatchActivity">
<Button
android:id="@+id/create_match_button"
android:layout_width="0dp"
android:layout_height="65dp"
android:onClick="goToNewMatchFormActivity"
android:text="@string/create_match_button"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<Button
android:id="@+id/edit_match_button"
android:layout_width="0dp"
android:layout_height="65dp"
android:layout_marginTop="15dp"
android:onClick="goToNewMatchToEdit"
android:text="@string/edit_match_button"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/create_match_button"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/match_list_view"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edit_match_button"
android:layout_marginTop="8dp"
app:layoutManager="LinearLayoutManager"/>
</android.support.constraint.ConstraintLayout>
以下是我创建的列表行布局(我希望在recylcerview中显示的内容):
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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="wrap_content"
android:orientation="vertical"
android:padding="8dp">
<TextView
android:id="@+id/match_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="0dp"
android:text="Match Name"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/imageView"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.502"/>
<TextView
android:id="@+id/match_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Match Date"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/imageView"
app:layout_constraintTop_toBottomOf="@+id/match_name"/>
<ImageView
android:id="@+id/imageView"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="0dp"
android:layout_weight="1"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_more_vert_black_24dp"/>
</android.support.constraint.ConstraintLayout>
这是我的Adapter类:
package com.checkinsystems.ez_score;
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.ImageView;
import android.widget.TextView;
import com.checkinsystems.ez_score.model.Match;
import java.util.List;
public class MatchItemAdapter extends RecyclerView.Adapter<MatchItemAdapter.ViewHolder> {
private List<Match> mMatches;
private Context mContext;
public MatchItemAdapter(Context context, List<Match> items){
this.mContext = context;
this.mMatches = items;
}
@Override
public MatchItemAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View itemView = inflater.inflate(R.layout.list_item_match, parent, false);
ViewHolder viewHolder = new ViewHolder(itemView);
return viewHolder;
}
@Override
public void onBindViewHolder(MatchItemAdapter.ViewHolder holder, int position) {
Match match = mMatches.get(position);
try {
holder.tvName.setText(match.getMatchName());
holder.tvDate.setText(match.getMatchDate());
holder.imageView.findViewById(R.id.imageView);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public int getItemCount() {
return mMatches.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder{
public TextView tvName;
public TextView tvDate;
public ImageView imageView;
public ViewHolder(View itemView){
super(itemView);
tvName = (TextView)itemView.findViewById(R.id.match_name);
tvDate = (TextView)itemView.findViewById(R.id.match_date);
imageView = (ImageView)itemView.findViewById(R.id.imageView);
}
}
}
这是我的活动(控制器):
package com.checkinsystems.ez_score;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import com.checkinsystems.ez_score.Sample.SampleDataProvider;
import com.checkinsystems.ez_score.model.Match;
import java.io.File;
import java.util.List;
import static com.checkinsystems.ez_score.MainActivity.CURRENT_MATCH_NAME;
import static com.checkinsystems.ez_score.MainActivity.MY_GLOBAL_PREFS;
public class MatchActivity extends AppCompatActivity {
private static final String TAG = "MatchActivity";
private static RecyclerView mRecyclerView;
List<Match> mMatchList = SampleDataProvider.sMatchList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_match);
// place the current match name in the action bar
SharedPreferences sharedPreferences = getSharedPreferences(MY_GLOBAL_PREFS, MODE_PRIVATE);
String currentMatchName = sharedPreferences.getString(CURRENT_MATCH_NAME, "current_match_name");
if (!(currentMatchName.equals(""))) {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setTitle(currentMatchName);
}
}
MatchItemAdapter adapter = new MatchItemAdapter(this, mMatchList);
mRecyclerView = (RecyclerView)findViewById(R.id.match_list_view);
mRecyclerView.setAdapter(adapter);
@Override
public void onResume(){
super.onResume();
SharedPreferences sharedPreferences = getSharedPreferences(MY_GLOBAL_PREFS, MODE_PRIVATE);
String currentMatchName = sharedPreferences.getString(CURRENT_MATCH_NAME, "current_match_name");
if (!(currentMatchName.equals(""))) {
ActionBar actionBar = getSupportActionBar();
Log.d(TAG, "action bar value: " + actionBar);
if (actionBar != null) {
actionBar.setTitle(currentMatchName);
}
}
}
// onClick handler to go to NewMatchFormActivity
public void goToNewMatchFormActivity(View view) {
Intent intent = new Intent(this, NewMatchFormActivity.class);
startActivity(intent);
}
public void goToNewMatchToEdit(View view) {
Intent intent = new Intent(this, EditMatchActivity.class);
startActivity(intent);
}
}
这是Match对象(Data Model)类:
package com.checkinsystems.ez_score.model;
导入android.os.Parcel; import android.os.Parcelable;
import com.checkinsystems.ez_score.utils.MatchHelper;
import java.util.ArrayList;
public class Match {
private String mMatchId;
private String mClubId;
private String mMatchName;
private String mMatchDate;
private String mMatchLevel;
private ArrayList<MatchStage> mStages;
private ArrayList<Competitors> mCompetitors;
public Match() {
}
public Match(String matchId, String clubID, String time, String matchName, String matchDate,
String matchLevel, ArrayList<MatchStage> stages, ArrayList<Competitors> competitors) {
if(matchId == null){
mMatchId = MatchHelper.createID(matchDate, clubID, time);
}
mClubId = clubID;
mMatchName = matchName;
mMatchDate = matchDate;
mMatchLevel = matchLevel;
mStages = stages;
mCompetitors = competitors;
}
public String getMatchId() {
return mMatchId;
}
public void setMatchId(String matchId) {
mMatchId = matchId;
}
public String getClubId() {
return mClubId;
}
public void setClubId(String clubId) {
mClubId = clubId;
}
public String getMatchName() {
return mMatchName;
}
public void setMatchName(String matchName) {
mMatchName = matchName;
}
public String getMatchDate() {
return mMatchDate;
}
public void setMatchDate(String matchDate) {
mMatchDate = matchDate;
}
public String getMatchLevel() {
return mMatchLevel;
}
public void setMatchLevel(String matchLevel) {
mMatchLevel = matchLevel;
}
public ArrayList<MatchStage> getStages() {
return mStages;
}
public void setStages(ArrayList<MatchStage> stages) {
mStages = stages;
}
public ArrayList<Competitors> getCompetitors() {
return mCompetitors;
}
public void setCompetitors(ArrayList<Competitors> competitors) {
mCompetitors = competitors;
}
@Override
public String toString() {
return "Match{" +
"mMatchId='" + mMatchId + '\'' +
", mMatchName='" + mMatchName + '\'' +
", mMatchDate='" + mMatchDate + '\'' +
", mMatchLevel='" + mMatchLevel + '\'' +
", mStages=" + mStages +
", mCompetitors=" + mCompetitors +
'}';
}
}
以下是我的样本数据提供者:
package com.checkinsystems.ez_score.Sample;
import com.checkinsystems.ez_score.model.Match;
import com.checkinsystems.ez_score.model.MatchStage;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class SampleDataProvider {
public static List<Match> sMatchList;
public static Map<String, Match> sMatchMap;
public static List<MatchStage> sMatchStages;
public static Map<String, MatchStage> sMatchStageMap;
static {
sMatchList = new ArrayList<>();
sMatchMap = new HashMap<>();
sMatchStages = new ArrayList<>();
sMatchStageMap = new HashMap<>();
addStage(new MatchStage(null, 1, "Zombie Apocalypse", 2, 4, 3, 1, 10, 45, "comstock"));
addStage(new MatchStage(null, 2, "Police Training", 2, 3, 3, 1, 11, 55, "comstock"));
addStage(new MatchStage(null, 3, "Get Those Guys", 2, 4, 3, 1, 13, 65, "comstock"));
addStage(new MatchStage(null, 4, "Self Defense", 2, 2, 3, 1, 10, 62, "comstock"));
addStage(new MatchStage(null, 5, "Stand Your Ground", 2, 5, 3, 1, 9, 35, "comstock"));
//
// addMatch(new Match(null, "Dog Fight", "May 31, 2016", "17:00:00", "2", (ArrayList)sMatchStages));
// addMatch(new Match(null, "Kick Some Ass", "April 16, 2017", "17:00:00", "2", (ArrayList)sMatchStages));
// addMatch(new Match(null, "Sharp Shooter", "October 10, 2017", "17:00:00", "2", (ArrayList)sMatchStages));
// addMatch(new Match(null, "Destroy", "January 1, 2016", "17:00:00", "2", (ArrayList)sMatchStages));
// addMatch(new Match(null, "Marksman", "May 14, 2017", "17:00:00", "2", (ArrayList)sMatchStages));
// addMatch(new Match(null, "Training Day", "November 15, 2017", "17:00:00", "2", (ArrayList)sMatchStages));
// addMatch(new Match(null, "Last Stand", "December 31, 2017", "17:00:00", "2", (ArrayList)sMatchStages));
}
private static void addMatch(Match match){
sMatchList.add(match);
sMatchMap.put(match.getMatchId(), match);
}
private static void addStage(MatchStage stage){
sMatchStages.add(stage);
sMatchStageMap.put(stage.getStageId(), stage);
}
}
我使用静态初始化程序块来创建Match对象列表以进行测试。这是我尝试用来填充recyclerview的内容。您会在我的MatchActivity顶部看到这一点。
答案 0 :(得分:1)
您的sMatchList对象为空,bcz您已注释了下一行代码:
// addMatch(new Match(null, "Dog Fight", "May 31, 2016", "17:00:00", "2", (ArrayList)sMatchStages));
// addMatch(new Match(null, "Kick Some Ass", "April 16, 2017", "17:00:00", "2", (ArrayList)sMatchStages));
// addMatch(new Match(null, "Sharp Shooter", "October 10, 2017", "17:00:00", "2", (ArrayList)sMatchStages));
// addMatch(new Match(null, "Destroy", "January 1, 2016", "17:00:00", "2", (ArrayList)sMatchStages));
// addMatch(new Match(null, "Marksman", "May 14, 2017", "17:00:00", "2", (ArrayList)sMatchStages));
// addMatch(new Match(null, "Training Day", "November 15, 2017", "17:00:00", "2", (ArrayList)sMatchStages)); }
取消对它们的注释,我想这一切都会好起来的。
答案 1 :(得分:0)
您已在xml布局中将宽度和高度属性指定为0dp,并且您没有使用权重,因此不会使用wrap_content或match_parent。
答案 2 :(得分:0)
sMatchList永远不会更新,因为代码已被注释掉。
答案 3 :(得分:0)
似乎您没有为LayoutManager
RecyclerView
在设置适配器之前添加此行:
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));