大家好我是android开发的新手。现在我正在学习recyclerView。 所以我想在回收站视图中显示1到18的数字。我知道这很简单,但我不能这样做。当我运行我的应用程序时出现错误。我的代码找不到任何错误。
这是我的MainActivity
package com.example.niyamat.golfscoreboard;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
public static final String TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
ScoreboardAdapter scoreboardAdapter = new ScoreboardAdapter(getData());
recyclerView.setAdapter(scoreboardAdapter);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
}
private ArrayList<HoleNumber> getData() {
ArrayList results = new ArrayList<HoleNumber>();
for (int i = 0; i < 19; i++) {
HoleNumber holeNumber = new HoleNumber(i);
results.add(holeNumber);
}
return results;
}
}
这是我的适配器
package com.example.niyamat.golfscoreboard;
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.ArrayList;
/**
* Created by Niyamat on 12/2/2015.
*/
public class ScoreboardAdapter extends RecyclerView.Adapter<ScoreboardAdapter.ScoreBoardViewHolder> {
private ArrayList<HoleNumber> mHoleNumbers;
public ScoreboardAdapter(ArrayList<HoleNumber> holeNumbers) {
mHoleNumbers = holeNumbers;
}
@Override
public ScoreBoardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.scoreboard_layout, parent, false);
ScoreBoardViewHolder scoreBoardViewHolder = new ScoreBoardViewHolder(view);
return scoreBoardViewHolder;
}
@Override
public void onBindViewHolder(ScoreBoardViewHolder holder, int position) {
holder.mHoleNumber.setText(mHoleNumbers.get(position).getHoleNumber());
}
@Override
public int getItemCount() {
return mHoleNumbers.size();
}
public class ScoreBoardViewHolder extends RecyclerView.ViewHolder {
public TextView mHoleNumber;
public ScoreBoardViewHolder(View itemView) {
super(itemView);
mHoleNumber = (TextView) itemView.findViewById(R.id.holeNumber);
}
}
}
这是我的模特
package com.example.niyamat.golfscoreboard;
/**
* Created by Niyamat on 12/2/2015.
*/
public class HoleNumber {
private int mHoleNumber;
public HoleNumber(int holeNumber) {
mHoleNumber = holeNumber;
}
public int getHoleNumber() {
return mHoleNumber;
}
public void setHoleNumber(int holeNumber) {
mHoleNumber = holeNumber;
}
}
最后这是我运行应用程序时在我的logcat中显示的错误
FATAL EXCEPTION: main
Process: com.example.niyamat.golfscoreboard, PID: 2065
android.content.res.Resources$NotFoundException: String resource ID #0x0
at android.content.res.Resources.getText(Resources.java:312)
at android.widget.TextView.setText(TextView.java:4417)
at com.example.niyamat.golfscoreboard.ScoreboardAdapter.onBindViewHolder(ScoreboardAdapter.java:31)
at com.example.niyamat.golfscoreboard.ScoreboardAdapter.onBindViewHolder(ScoreboardAdapter.java:14)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5217)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5250)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4487)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4363)
我还在github中更新了我的代码 这是链接 https://github.com/niyamatalmass/Golf_Scoreboard
答案 0 :(得分:1)
像这样修改onBindViewHolder的代码
holder.mHoleNumber.setText(mHoleNumbers.get(position).getHoleNumber()+"");