Issue
I have been attempting integrate an options menu in to my listview by following the following tutorial:
[https://www.simplifiedcoding.net/create-options-menu-recyclerview-item-tutorial/]
I have been able to get the icon for the menu to appear next to the items in the listview, but I am unable to click on it to get access to a popupmenu. I am attempting to implement this in side my public void onBindViewHolder(MyViewHolder holder, int position) method.
I cannot figure out why I am unable to perform an onClick. I have posted my code for the adapter
public class LeagueAdapter extends RecyclerView.Adapter<LeagueAdapter.MyViewHolder> {
private Context context;
private List<League> leaguesList;
public TextView buttonViewOption;
public void notifyDatasetChanged(List<League> newleagueslist) {
leaguesList.clear();
leaguesList.addAll(newleagueslist);
super.notifyDataSetChanged();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView name;
public TextView basescore;
public TextView basescorepercentage;
private TextView leagueAverage;
public TextView id;
public TextView timestamp;
public TextView buttonViewOption;
public MyViewHolder(View view) {
super(view);
id = view.findViewById( R.id.tvLeagueId);
name = view.findViewById(R.id.tvSeriesName );
basescore = view.findViewById(R.id.tvBaseScore );
basescorepercentage = view.findViewById(R.id.tvBaseScorePercentage );
leagueAverage = view.findViewById(R.id.tvLeagueAverage);
timestamp = view.findViewById(R.id.timestamp);
buttonViewOption = (TextView) itemView.findViewById(R.id.textViewOptions);
}
}
public LeagueAdapter(Context context, List<League> leaguesList) {
this.context = context;
this.leaguesList = leaguesList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.listview_league, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
League league = leaguesList.get(position);
int id = league.getId();
String leagueId = String.valueOf(id);
holder.id.setText(leagueId);
holder.name.setText(league.getName());
holder.basescore.setText(league.getBaseScore());
holder.basescorepercentage.setText(league.getBaseScorePercentage());
if (league.getAverage() != "") {
holder.leagueAverage.setText(String.format("League Avg: %s", league.getAverage()));
} else {
holder.leagueAverage.setText(String.format("League Avg: %s", "0"));
}
//Formatting And Displaying Timestamp
holder.timestamp.setText(formatDate(league.getTimestamp()));
holder.buttonViewOption.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//creating a popup menu
PopupMenu popup = new PopupMenu(context, holder.buttonViewOption);
//inflating menu from xml resource
popup.inflate(R.menu.options_menu);
//adding click listener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu1:
//handle menu1 click
break;
case R.id.menu2:
//handle menu2 click
break;
case R.id.menu3:
//handle menu3 click
break;
}
return false;
}
});
//displaying the popup
popup.show();
}
});
}
@Override
public int getItemCount() {
return leaguesList.size();
}
//Formatting TimeStamp to 'EEE MMM dd yyyy (HH:mm:ss)'
//Input : 2018-05-23 9:59:01
//Output : Wed May 23 2018 (9:59:01)
private String formatDate(String dateStr) {
try {
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = fmt.parse(dateStr);
SimpleDateFormat fmtOut = new SimpleDateFormat("EEE MMM dd yyyy (HH:mm:ss)");
return fmtOut.format(date);
} catch (ParseException e) {
}
return "";
}
}
This is my listview xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:autofit="http://schemas.android.com/apk/res-auto"
android:clickable="true"
android:foreground="?attr/selectableItemBackground"
android:paddingBottom="@dimen/dimen_10"
android:paddingLeft="@dimen/activity_margin"
android:paddingRight="@dimen/activity_margin"
android:paddingTop="@dimen/dimen_10"
android:focusable="true">
<me.grantland.widget.AutofitTextView
android:id="@+id/tvSeriesName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="2"
android:singleLine="true"
android:text="@string/leagueValue"
android:textColor="?attr/colorAccent"
android:textSize="24sp"
android:textStyle="bold"
autofit:minTextSize="16sp" />
<TextView
android:id="@+id/tvLeagueAverage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@+id/tvSeriesName"
android:layout_marginStart="0dp"
android:text="League Average: 300"
android:textColor="#000000"
android:textSize="12sp" />
<TextView
android:id="@+id/timestamp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tvSeriesName"
android:layout_centerHorizontal="true"
android:text="Fri May 18 2018"
android:textColor="?attr/colorText1"
android:textSize="10sp"
android:visibility="gone" />
<TextView
android:id="@+id/tvLeagueId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/tvSeriesName"
android:layout_below="@+id/timestamp"
android:text="TextView"
android:textColor="?attr/colorText1"
android:visibility="gone" />
<TextView
android:id="@+id/tvBaseScore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="?attr/colorText1"
android:visibility="gone" />
<TextView
android:id="@+id/tvBaseScorePercentage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="?attr/colorText1"
android:visibility="gone" />
<View
android:id="@+id/divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="66dp"
android:background="?attr/colorAccent" />
<TextView
android:id="@+id/textViewOptions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:text="⋮"
android:textAppearance="?android:textAppearanceLarge" />
android:background="?android:attr/listDivider" />
</RelativeLayout>
Any help would be appreciated
I did a little digging and found something similar to the issue that I am experiencing:
Create Options Menu for RecyclerView-Item
The second answer which looks close to what I am trying to implement was done in onBindViewHolder(), however when I moved my code to my onBindViewHolder the results where identical as before. The dots appear, but they are not click-able.
I went back to the tutorial and noticed that I was missing a couple of lines which I added. These lines where dealing with mCtx. Updated code below.
public LeagueAdapter(Context context, List<League> leaguesList) {
this.mCtx=mCtx;
this.context = context;
this.leaguesList = leaguesList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.listview_league, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
League league = leaguesList.get(position);
int id = league.getId();
String leagueId = String.valueOf(id);
holder.id.setText(leagueId);
holder.name.setText(league.getName());
holder.basescore.setText(league.getBaseScore());
holder.basescorepercentage.setText(league.getBaseScorePercentage());
if (league.getAverage() != "") {
holder.leagueAverage.setText(String.format("League Avg: %s", league.getAverage()));
} else {
holder.leagueAverage.setText(String.format("League Avg: %s", "0"));
}
//Formatting And Displaying Timestamp
holder.timestamp.setText(formatDate(league.getTimestamp()));
holder.buttonViewOption.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//creating a popup menu
PopupMenu popup = new PopupMenu(mCtx, holder.buttonViewOption);
//inflating menu from xml resource
popup.inflate(R.menu.options_menu);
//adding click listener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu1:
//handle menu1 click
break;
case R.id.menu2:
//handle menu2 click
break;
case R.id.menu3:
//handle menu3 click
break;
}
return false;
}
});
//displaying the popup
popup.show();
}
});
}
I am now getting the following error from Logcat
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at com.android.internal.view.menu.MenuBuilder.<init>(MenuBuilder.java:189)
at android.widget.PopupMenu.<init>(PopupMenu.java:99)
at android.widget.PopupMenu.<init>(PopupMenu.java:74)
at android.widget.PopupMenu.<init>(PopupMenu.java:59)
at ca.rvogl.tpbcui.views.LeagueAdapter$1.onClick(LeagueAdapter.java:92)
LeagueAdapter.java:92 is the following line
//creating a popup menu
PopupMenu popup = new PopupMenu(mCtx, holder.buttonViewOption);
I have also posted my MainActivity incase it is required
public class MainActivity extends AppCompatActivity {
private LeagueAdapter mAdapter;
private List<League> leaguesList = new ArrayList<>();
private CoordinatorLayout coordinatorLayout;
private RecyclerView recyclerView;
private TextView noLeaguesView;
private DatabaseHelper db;
private TextView leagueAverage;
private String savedLeagueAverage;
private static final String PREFS_NAME = "prefs";
private static final String PREF_BLUE_THEME = "blue_theme";
private static final String PREF_GREEN_THEME = "green_theme";
private static final String PREF_ORANGE_THEME = "purple_theme";
private static final String PREF_RED_THEME = "red_theme";
private static final String PREF_YELLOW_THEME = "yellow_theme";
@Override
protected void onResume() {
super.onResume();
db = new DatabaseHelper(this);
mAdapter.notifyDatasetChanged(db.getAllLeagues());
}
@Override
protected void onCreate(Bundle savedInstanceState) {
//Use Chosen Theme
SharedPreferences preferences = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
boolean useBlueTheme = preferences.getBoolean(PREF_BLUE_THEME, false);
if (useBlueTheme) {
setTheme(R.style.AppTheme_Blue_NoActionBar);
}
boolean useGreenTheme = preferences.getBoolean(PREF_GREEN_THEME, false);
if (useGreenTheme) {
setTheme(R.style.AppTheme_Green_NoActionBar);
}
boolean useOrangeTheme = preferences.getBoolean(PREF_ORANGE_THEME, false);
if (useOrangeTheme) {
setTheme(R.style.AppTheme_Orange_NoActionBar);
}
boolean useRedTheme = preferences.getBoolean(PREF_RED_THEME, false);
if (useRedTheme) {
setTheme(R.style.AppTheme_Red_NoActionBar);
}
boolean useYellowTheme = preferences.getBoolean(PREF_YELLOW_THEME, false);
if (useYellowTheme) {
setTheme(R.style.AppTheme_Yellow_NoActionBar);
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
coordinatorLayout = findViewById(R.id.coordinator_layout);
recyclerView = findViewById(R.id.recycler_view);
noLeaguesView = findViewById(R.id.empty_leagues_view);
db = new DatabaseHelper(this);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.add_league_fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showLeagueDialog(false, null, -1);
}
});
mAdapter = new LeagueAdapter(this, leaguesList);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
//recyclerView.addItemDecoration(new MyDividerItemDecoration(this, LinearLayoutManager.VERTICAL, 16));
recyclerView.setAdapter(mAdapter);
toggleEmptyLeagues();
//On Long Click On The RecyclerView Item An Alert Dialog Is Opened With The Option To Choose Edit/Delete
recyclerView.addOnItemTouchListener(new RecyclerTouchListener(this, recyclerView, new RecyclerTouchListener.ClickListener() {
@Override
public void onClick(View view, final int position) {
int leagueId = leaguesList.get(position).getId();
Intent myIntent = new Intent(MainActivity.this, BowlerActivity.class);
myIntent.putExtra("leagueId", leagueId);
startActivity(myIntent);
overridePendingTransition(0, 0);
}
@Override
public void onLongClick(View view, int position) {
showActionsDialog(position);
}
}));
}
//Inserting New League In The Database And Refreshing The List
private void createLeague(String league, String baseScore, String baseScorePercentage) {
String leagueAverage = "0";
//Inserting League In Database And Getting Newly Inserted League Id
long id = db.insertLeague(league, baseScore, baseScorePercentage, leagueAverage);
//Get The Newly Inserted League From The Database
League n = db.getLeague(id);
if (n != null) {
//Adding New League To The Array List At Position 0
leaguesList.add(0, n);
//Refreshing The List
mAdapter.notifyDataSetChanged();
toggleEmptyLeagues();
}
}
//Updating League In The Database And Updating The Item In The List By Its Position
private void updateLeague(String name, String baseScore, String baseScorePercentage, int position) {
League n = leaguesList.get(position);
//Updating League Text
n.setName(name);
n.setBaseScore(baseScore);
n.setBaseScorePercentage(baseScorePercentage);
//Updating The League In The Database
db.updateLeague(n);
//Refreshing The List
leaguesList.set(position, n);
mAdapter.notifyItemChanged(position);
toggleEmptyLeagues();
}
//Deleting League From SQLite Database And Removing The League Item From The List By Its Position
private void deleteLeague(int position) {
//Deleting The League From The Database
db.deleteLeague(leaguesList.get(position));
//Removing League From The List
leaguesList.remove(position);
mAdapter.notifyItemRemoved(position);
toggleEmptyLeagues();
}
private void showActionsDialog(final int position) {
LayoutInflater layoutInflaterAndroid = LayoutInflater.from(getApplicationContext());
View view = View.inflate(this, R.layout.dialog_options_1, null);
final AlertDialog.Builder alertDialogBuilderUserInput = new AlertDialog.Builder(new ContextThemeWrapper(MainActivity.this, R.style.AppTheme));
alertDialogBuilderUserInput.setView(view);
alertDialogBuilderUserInput.setCancelable(true);
final AlertDialog alertDialog = alertDialogBuilderUserInput.create();
//Cancel
final ImageButton cancel_btn = (ImageButton) view.findViewById(R.id.cancel);
cancel_btn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
alertDialog.cancel();
}
});
//Edit
ImageButton edit_btn = (ImageButton) view.findViewById(R.id.edit);
edit_btn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showLeagueDialog(true, leaguesList.get(position), position);
alertDialog.dismiss();
}
});
ImageButton delete_btn = (ImageButton) view.findViewById(R.id.delete);
delete_btn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Snackbar snackbar = Snackbar.make(findViewById(android.R.id.content), "League will be deleted.", Snackbar.LENGTH_LONG)
.setAction("OK", new View.OnClickListener() {
@Override
public void onClick(View v) {
deleteLeague(position);
}
});
snackbar.show();
alertDialog.dismiss();
}
});
Window window = alertDialog.getWindow();
window.setGravity(Gravity.TOP);
alertDialog.show();
}
//Show Alert Dialog With EditText Options to Enter/Edit A League
//When shouldUpdate = true, It Will Automatically Display Old League Name And Change The Button Text To UPDATE
private void showLeagueDialog(final boolean shouldUpdate, final League league, final int position) {
LayoutInflater layoutInflaterAndroid = LayoutInflater.from(getApplicationContext());
View view = View.inflate(this, R.layout.dialog_league, null);
AlertDialog.Builder alertDialogBuilderUserInput = new AlertDialog.Builder(new ContextThemeWrapper(MainActivity.this, R.style.AppTheme));
alertDialogBuilderUserInput.setView(view);
final EditText inputLeague = view.findViewById(R.id.etLeagueNameInput);
final EditText inputBaseScore = view.findViewById(R.id.etBaseScore);
final EditText inputBaseScorePercentage = view.findViewById(R.id.etBaseScorePercentage);
TextView dialogTitle = view.findViewById(R.id.dialog_title);
dialogTitle.setText(!shouldUpdate ? getString(R.string.lbl_new_league_title) : getString(R.string.lbl_edit_league_title));
if (shouldUpdate && league != null) {
inputLeague.setText(league.getName());
inputBaseScore.setText(league.getBaseScore());
inputBaseScorePercentage.setText(league.getBaseScorePercentage());
}
alertDialogBuilderUserInput.setCancelable(true).setPositiveButton(shouldUpdate ? "update" : "save", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogBox, int id) {
}
}).setNegativeButton("cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogBox, int id) {
dialogBox.cancel();
}
});
final AlertDialog alertDialog = alertDialogBuilderUserInput.create();
alertDialog.show();
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Show Toast Message When No Text Is Entered
if (TextUtils.isEmpty(inputLeague.getText().toString())) {
Toast.makeText(MainActivity.this, "Enter League!", Toast.LENGTH_SHORT).show();
return;
} else {
alertDialog.dismiss();
}
//Check If User Is Updating League
if (shouldUpdate && league != null) {
// update note by it's id
updateLeague(inputLeague.getText().toString(), inputBaseScore.getText().toString(), inputBaseScorePercentage.getText().toString(), position);
} else {
// create new note
createLeague(inputLeague.getText().toString(), inputBaseScore.getText().toString(), inputBaseScorePercentage.getText().toString());
}
}
});
}
//Toggling List And Empty League View
private void toggleEmptyLeagues() {
// you can check notesList.size() > 0
if (db.getLeaguesCount() > 0) {
noLeaguesView.setVisibility(View.GONE);
} else {
noLeaguesView.setVisibility(View.VISIBLE);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
Intent intent = new Intent(this, SettingsActivity.class);
startActivity(intent);
overridePendingTransition(0, 0);
return true;
}
return super.onOptionsItemSelected(item);
}
}
I think I have found the root of my issue, when I REM out a section of code that I have in my onClick() in the MainActivity, the option menus appear for each row in my listview. The code I commented out is as follows:
//On Long Click On The RecyclerView Item An Alert Dialog Is Opened With The Option To Choose Edit/Delete
recyclerView.addOnItemTouchListener(new RecyclerTouchListener(this, recyclerView, new RecyclerTouchListener.ClickListener() {
@Override
public void onClick(View view, final int position) {
/* int leagueId = leaguesList.get(position).getId();
Intent myIntent = new Intent(MainActivity.this, BowlerActivity.class);
myIntent.putExtra("leagueId", leagueId);
startActivity(myIntent);
overridePendingTransition(0, 0);*/
}
@Override
public void onLongClick(View view, int position) {
showActionsDialog(position);
}
}));
}
How can I add onClickListener for this section? This section is dealing with the entire row, when clicked it takes the user to a new activity.
Solution
you initialize the wrong context on public LeagueAdapter(Context context, List<League> leaguesList)
, change from this.mCtx=mCtx
to
this.mCtx = context
will resolved problem.
Answered By - Youhai LU
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.