我正在开发一个简单的应用程序,用于跟踪我正在观看的节目,起初我使用的是 ListView,但现在我想使用 RecyclerView。我将所有节目存储在本地数据库中。但是,我无法正确地将代码转换为 RecyclerView。我还无法测试它,因为我正在努力解决以下行的错误:
showAdapter = new ArrayAdapter<Show>(this, showArrayAdapter); // the error
Error:(62, 56) error: incompatible types: List cannot be converted to int
public class MainActivity extends AppCompatActivity {
private Button btn;
private RecyclerView recyclerView;
private List<Show> showArrayAdapter;
private ShowsAdapter showAdapter;
private DataSource datasource;
List<Show> shows = datasource.getAllShows();
public static final String INTENT_DETAIL_ROW_NUMBER = "Row number";
public static final String INTENT_DETAIL_REMINDER_TEXT = "Reminder text";
public static final int REQUESTCODE = 2;
public static final String EXTRA_SHOW_ID = "extraShowId";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
datasource = new DataSource(this);
shows = datasource.getAllShows();
recyclerView = (RecyclerView) findViewById(R.id.main_list);
showArrayAdapter = new ArrayList<Show>();
recyclerView.setAdapter(showAdapter);
registerForContextMenu(recyclerView);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.taskView);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivityForResult(new Intent(MainActivity.this, CreateShowActivity.class), 1);
}
});
}
private void updateUI() {
if (showAdapter == null) {
showAdapter = new ArrayAdapter<Show>(this, showArrayAdapter); // the error
recyclerView.setAdapter(showAdapter);
} else {
showAdapter.notifyDataSetChanged();
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
//Handle data
long showId = data.getLongExtra(EXTRA_SHOW_ID, -1);
if (showId != -1) {
Show show = datasource.getShow(showId);
showArrayAdapter.add(show);
updateUI();
}
}
}
if (requestCode == 2) {
if (resultCode == RESULT_OK) {
updateUI();
}
}
}
}
我的适配器
public class ShowsAdapter extends RecyclerView.Adapter<ShowsAdapter.ViewHolder> {
private ArrayAdapter<Show> mShows;
private Context mContext;
public ShowsAdapter(Context mContext, ArrayAdapter<Show> mShows) {
this.mContext = mContext;
this.mShows = mShows;
}
@Override
public ShowsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(android.R.layout.activity_list_item, null);
// Return a new holder instance
ShowsAdapter.ViewHolder viewHolder = new ShowsAdapter.ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(ShowsAdapter.ViewHolder holder, final int position) {
final Show show = mShows.getItem(position);
holder.textView.setText(show.getShow());
holder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(mContext, EditShowActivity.class);
intent.putExtra(INTENT_DETAIL_ROW_NUMBER, position);
intent.putExtra(INTENT_DETAIL_REMINDER_TEXT, show.getShow());
((MainActivity) mContext).startActivityForResult(intent, REQUESTCODE);
}
});
holder.mView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
mShows.remove(mShows.getItem(position));
notifyItemRemoved(position);
notifyItemRangeChanged(position,mShows.getCount());
return true;
}
});
}
@Override
public int getItemCount() {
return mShows.getCount();
}
public static class ViewHolder extends RecyclerView.ViewHolder{
public View mView;
public TextView textView;
//Constructor
public ViewHolder(View v) {
super(v);
textView = (TextView) v.findViewById(android.R.id.text1);
mView = v;
}
}
}
由于我目前正在努力解决这个问题并尝试了许多不同的事情,因此我的代码可能无法工作。如果您看到其他任何内容请告诉我
更新:
根据大家收到的意见,我做了很多改变:
private DataSource datasource;
private Button btn;
private RecyclerView recyclerView;
private List<Show> shows;
private ShowsAdapter showAdapter;
在初始化之前添加布局管理器并删除数据源
DataSource datasource = new DataSource(this);
List<Show> shows = datasource.getAllShows();
recyclerView = (RecyclerView) findViewById(R.id.main_list);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setAdapter(showAdapter);
更新了我的 UpdateUI()
private void updateUI() {
if (showAdapter == null) {
showAdapter = new ShowsAdapter(this, shows); // the error
recyclerView.setAdapter(showAdapter);
} else {
showAdapter.notifyDataSetChanged();
}
}
并匹配showAdapters.java中的类型
public ShowsAdapter(Context mContext, List<Show> mShows) {
this.mContext = mContext;
this.mShows = mShows;
}
但是在启动时我收到以下错误
E/RecyclerView: No adapter attached; skipping layout E/RecyclerView: No adapter attached; skipping layout
和
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { (has extras) }} to activity {com.example.kevin.androidapp/com.example.kevin.androidapp.MainActivity}: java.lang.NullPointerException
Caused by: java.lang.NullPointerException at com.example.kevin.androidapp.MainActivity.onActivityResult(MainActivity.java:77)
位于 onActivityResult Show show = datasource.getShow(showId);
我的createShowActivity
public class CreateShowActivity extends AppCompatActivity {
private Button btn;
private EditText showDescription;
private DataSource datasource;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
showDescription = (EditText)this.findViewById(R.id.showDescription);
datasource = new DataSource(this);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.createButton);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
long assignmentId = datasource.createShows(showDescription.getText().toString());
Intent resultIntent = new Intent();
resultIntent.putExtra(MainActivity.EXTRA_SHOW_ID, assignmentId);
setResult(Activity.RESULT_OK, resultIntent);
finish();
}
});
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
最佳答案
列表无法转换为int?
这不是您标记的行,除非 getAllShows()
返回一个整数...
不过,您正在混合适配器。 Recyclerview不使用ArrayAdapter。
你完成了这门课
ShowsAdapter extends RecyclerView.Adapter
但是你不使用它
showAdapter = new ArrayAdapter<Show>(this, showArrayAdapter); // the error
recyclerView.setAdapter(showAdapter);
<小时/>
还有showArrayAdapter = new ArrayList<Show>();
根本不是适配器。您已经拥有 List<Show> shows
和
datasource = new DataSource(this);
shows = datasource.getAllShows();
首先,如果您不想出现空指针异常,则需要停止获取字段中的显示。
private List<Show> shows; // = datasource.getAllShows();
<小时/>
综上所述,要使用Recyclerview
1) 添加布局管理器
2)使用正确的Adapter类
3)使用正确的列表
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(mLayoutManager);
showAdapter = new ShowsAdapter(this, shows);
recyclerView.setAdapter(showAdapter);
<小时/>
附加说明。
这种对 Activity 的强制转换是不必要的,并且当您想在其他 Activity 中使用此适配器时会导致问题。
((MainActivity) mContext).startActivityForResult
关于java - 从ListView到RecyclerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43345553/