Android适配器未更新

时间:2018-03-04 14:44:03

标签: java android class android-recyclerview android-adapter

我想在适配器的帮助下在回收站视图中显示匹配对象列表(匹配=两个彼此喜欢的用户)。

这是我的活动,旨在显示这些匹配:

public class MatchesActivity extends AppCompatActivity {

// variables:
private RecyclerView mMatchesRecyclerView;
private RecyclerView.Adapter mMatchItemAdapter;
private RecyclerView.LayoutManager mMatchesLayoutManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_matches);

    mMatchesRecyclerView = findViewById(R.id.matches_recyclerView);
    mMatchesRecyclerView.setNestedScrollingEnabled(false);
    mMatchesRecyclerView.setHasFixedSize(true);

    // set layout manager & pass it to the recycler view:
    mMatchesLayoutManager = new LinearLayoutManager(MatchesActivity.this);
    mMatchesRecyclerView.setLayoutManager(mMatchesLayoutManager);

    // set match adapter & pass it to the recycler view:
    mMatchItemAdapter = new MatchItemAdapter(getMatchesList(), MatchesActivity.this);
    mMatchesRecyclerView.setAdapter(mMatchItemAdapter);

    // add test items to the recycler view:
    Match testMatch = new Match("abcdefgh");
    matchesList.add(testMatch);
    mMatchItemAdapter.notifyDataSetChanged();
    Log.d("MatchesActivity", "TEST LIST: " + matchesList.toString());
}

private ArrayList<Match> matchesList = new ArrayList<Match>();
private List<Match> getMatchesList() {
    Log.d("MatchesActivity", "getMatchesList function: " + matchesList.toString());
    return matchesList;
}
}

这是我的适配器,应该膨胀相关的布局&amp;用相关的对象数据填充它:

public class MatchItemAdapter extends RecyclerView.Adapter<MatchViewholder> {

private List<Match> mMatchesList;
private Context mViewContext;

public MatchItemAdapter(List<Match> matchesList, Context context) {
    this.mMatchesList = matchesList;
    this.mViewContext = context;
    Log.d("MatchItemAdapter", "Constructor: " + mMatchesList.toString());
}

// inflate the layout:
@Override
public MatchViewholder onCreateViewHolder(ViewGroup parent, int viewType) {
    View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_matches, null, false);
    RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    layoutView.setLayoutParams(lp);
    MatchViewholder matchViewholder = new MatchViewholder(layoutView);
    Log.d("MatchItemAdapter", "onCreateViewHolder: " + mMatchesList.toString());
    return matchViewholder;
}

// populate each row within the layout:
@Override
public void onBindViewHolder(MatchViewholder holder, int position) {
    Log.d("MatchItemAdapter", "onBindViewHolder: " + mMatchesList.toString());
    holder.mMatchID.setText(mMatchesList.get(position).getMatchID());
}

@Override
public int getItemCount() {
    return 0;
}

}

Match类目前只接受matchID参数,即string。使用默认图像和此matchID字符串创建对象。 目前,我没有准备好数据库的真实匹配对象,所以我想检查回收器视图和适配器是否正常工作,然后再继续。

然而,当我去Matches Activity时,它是空的,根本没有显示任何内容。正如您在MatchesActivity onCreate方法中看到的,我创建了一个测试Match对象,其matchID =“abcdefgh”,然后将其添加到matchesList。所以我期待将“abcdefgh”文本传递给适配器并显示在MatchesActivity中。

我的日志语句表明Match对象已经创建并成功添加到列表中,但是,getMatchesList()函数返回一个空列表,然后在Adapter构造函数中使用它(我认为这是)导致Activity没有什么都有。

我对Android和Java开发相对较新,特别是回收站视图和适配器,但从我收集的内容看来似乎就好了

mMatchItemAdapter.notifyDataSetChanged();

无法正常工作,因为在此之前一切似乎都很好。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

您将返回0.您应该做的是返回mMatchesList列表的长度。

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