为RecyclerView项目创建选项菜单

时间:2018-09-06 23:19:26

标签: java android listview android-recyclerview onclick

我一直按照以下教程尝试将选项菜单集成到我的列表视图中:

[https://www.simplifiedcoding.net/create-options-menu-recyclerview-item-tutorial/]

我已经能够使菜单图标显示在列表视图中的项目旁边,但是我无法单击它来访问弹出菜单。我正在尝试在我的 public void onBindViewHolder(MyViewHolder持有人,int位置)方法中实现此功能。

enter image description here

我无法弄清楚为什么我无法执行onClick。我已经发布了适配器的代码

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 "";
    }
}

这是我的listview xml文件:

<?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="&#8942;"
        android:textAppearance="?android:textAppearanceLarge" />
    android:background="?android:attr/listDivider" />

</RelativeLayout>

任何帮助将不胜感激

我做了一些挖掘,发现了与我遇到的问题类似的东西:

Create Options Menu for RecyclerView-Item

看起来很接近我要实现的第二个答案是在 onBindViewHolder()中完成的,但是,当我将代码移至onBindViewHolder时,结果与以前相同。点出现了,但它们不可单击。

我回到了本教程,发现我错过了几行添加的内容。这些行在处理mCtx。下面更新了代码。

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();

            }
        });
    }

我现在从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是以下行

//creating a popup menu
                PopupMenu popup = new PopupMenu(mCtx, holder.buttonViewOption);

我还发布了MainActivity以备不时之需

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);
    }


}

我想我找到了问题的根源,当我删除MainActivity中onClick()中的一段代码时,列表视图中的每一行都会出现选项菜单。我注释掉的代码如下:

//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);
            }
        }));
    }

如何为该部分添加onClickListener?本节处理的是整行,单击该行会将用户带到新的活动。

2 个答案:

答案 0 :(得分:1)

您在public LeagueAdapter(Context context, List<League> leaguesList)上初始化了错误的上下文,从this.mCtx=mCtx更改为
 this.mCtx = context将解决问题。

答案 1 :(得分:0)

尝试一下:

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) view.findViewById(R.id.textViewOptions);
           
           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();

            }
        });
        }
    }


    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()));

      
    }

    @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 "";
    }
}

请发布您的Java代码以获取更多帮助