Reputation: 634
I am working on a simple application that keeps track of the shows I am watching, at first I was using a ListView, but now I want to use a RecyclerView. I store all the shows in a local database. However I am having trouble correctly converting my code to a RecyclerView. I can't test it yet cause I am struggling with the error on the following line:
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();
}
}
}
}
My Adapter
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;
}
}
}
As I am currently struggling with this and tried many different things it might be that my code doesn't work. if you see anything else let me know
UPDATE:
I changed a lot based on the input received by all:
private DataSource datasource;
private Button btn;
private RecyclerView recyclerView;
private List<Show> shows;
private ShowsAdapter showAdapter;
added the layout manager and removed datasource before the initialize
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);
Updated my UpdateUI()
private void updateUI() {
if (showAdapter == null) {
showAdapter = new ShowsAdapter(this, shows); // the error
recyclerView.setAdapter(showAdapter);
} else {
showAdapter.notifyDataSetChanged();
}
}
and matched the types in showAdapters.java
public ShowsAdapter(Context mContext, List<Show> mShows) {
this.mContext = mContext;
this.mShows = mShows;
}
however on launching I get the following errors
E/RecyclerView: No adapter attached; skipping layout E/RecyclerView: No adapter attached; skipping layout
and
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)
which is in the onActivityResult Show show = datasource.getShow(showId);
My 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);
}
}
Upvotes: 1
Views: 987
Reputation:
showAdapter = new ArrayAdapter(this, showArrayAdapter);
Your ArrayAdapter
constructor doesn't match with any of these, its expecting second parameter as resourceId in integer and not the list hence that error
Upvotes: 0
Reputation: 5705
One of your error is this
private DataSource datasource;
List<Show> shows = datasource.getAllShows();
You are trying to get all shows even before you have initialized your data source. As you are already doing this in onCreate()
method so you should remove this line which you are using before onCreate();
Also you haven't provided any layout manager for your recyclerview. So your recyclerview will not be shown. Add a layout manager like this
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(MainActivity.this);
recyclerView.setLayoutManager(mLayoutManager);
Also don't forget to provide layout manager as above in your updateUI()
method.
Upvotes: 2
Reputation: 191728
List can't be converted to int?
That's not the line you marked unless getAllShows()
returns an int...
You are mixing your adapters, though. Recyclerview doesn't use ArrayAdapter.
You made this class
ShowsAdapter extends RecyclerView.Adapter
But you don't use it
showAdapter = new ArrayAdapter<Show>(this, showArrayAdapter); // the error
recyclerView.setAdapter(showAdapter);
Also showArrayAdapter = new ArrayList<Show>();
is not an adapter at all. You already have a List<Show> shows
And
datasource = new DataSource(this);
shows = datasource.getAllShows();
First, though, you need to stop getting the shows in the field if you don't want a nullpointerexception.
private List<Show> shows; // = datasource.getAllShows();
In summary, to use Recyclerview
1) add a LayoutManager
2) use the correct Adapter class
3) use the correct list
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(mLayoutManager);
showAdapter = new ShowsAdapter(this, shows);
recyclerView.setAdapter(showAdapter);
Additional note.
This cast to the Activity is unnecessary and will cause issues when you want to use this Adapter in other activities.
((MainActivity) mContext).startActivityForResult
Upvotes: 2
Reputation: 476
You cannot convert from arraylist datatype to arrayadaptor datatype. You need to change the following line in the code.
if (showAdapter == null) {
showAdapter = new ArrayAdapter<Show>(this, showArrayAdapter);
recyclerView.setAdapter(showAdapter);
}
change to
if (showAdapter == null) {
showAdapter = new ShowAdapter(this, showArrayAdapter);
recyclerView.setAdapter(showAdapter);
}
and in the ShowAdapter class change the data type of the variable mShows from ArrayAdaptor to Arraylist. see the example sample code below.
private ArrayList<Show> mShows;
public ShowsAdapter(Context mContext, ArrayList<Show> mShows) {
this.mContext = mContext;
this.mShows = mShows;
}
Upvotes: 0
Reputation: 61
In onCreate you are setting adapter before initialization
Please remove belove line in onCreate and set Orientation for Recycler view using LinearLayoutManger
Remove This line in onCreate
recyclerView.setAdapter(showAdapter);
Upvotes: 2
Reputation: 2303
Quite not sure you are setting
showAdapter = new ArrayAdapter<Show>(this, showArrayAdapter);
to showAdapter.
Looks like that is the issue create ShowsAdapter instance and try?
showAdapter = new ShowsAdapter(this, showArrayAdapter)
Upvotes: 1