在android studio中将数组列表显示为高分

在android studio中将数组列表显示为高分,android,android-fragments,Android,Android Fragments,我对安卓系统真的很陌生,有一个项目需要在测验中保持高分。游戏结束后,如何在碎片上显示阵列列表 public void saveHighScores(int score) { ArrayList<String> topFiveHighScoresList; // store the saved scores in an ArrayList topFiveHighScoresList = new ArrayList<String>

我对安卓系统真的很陌生,有一个项目需要在测验中保持高分。游戏结束后,如何在碎片上显示阵列列表

  public void saveHighScores(int score) {
       ArrayList<String> topFiveHighScoresList;

       // store the saved scores in an ArrayList
       topFiveHighScoresList = new ArrayList<String>(topFiveHighScoresPreference.getAll().keySet());

       // add the latest score to the list
       topFiveHighScoresList.add(Integer.toString(score));

       // sort the scores
       Collections.sort(topFiveHighScoresList, String.CASE_INSENSITIVE_ORDER);
            // remove the last score from the list if the list exceeds 5 items.
       if (topFiveHighScoresList.size() > 5)
           topFiveHighScoresList.remove(5);

       SharedPreferences.Editor preferencesEditor;
       preferencesEditor = topFiveHighScoresPreference.edit();

       preferencesEditor.clear();
       preferencesEditor.apply();

   }

您可以使用自定义对话框扩展对话框

在下一段代码中,您将显示一个带有分数的对话框

首先是XML:

使用addKeyValuePairString key,String value方法,您将在显示对话框之前将分数添加到对话框中

您仍然需要XML作为每行的键值:

row_key.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="dummy_text"
    android:id="@+id/row_textView_key"
    android:layout_marginTop="20sp"
    android:textColor="@color/white"
    android:textStyle="bold"
    android:textSize="30sp"
    android:layout_marginLeft="20sp">
</TextView>

仅此而已。

游戏结束后你说的是什么意思?你找到有用的答案了吗?还是你还需要别的什么
import android.app.Dialog;
import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CustomDialog extends Dialog {

    private LinearLayout layout;
    private Context mContext;

    public CustomDialog(Context context) {

        super(context, android.R.style.Theme_Translucent_NoTitleBar);
        setContentView(R.layout.dialog_content);
        setCancelable(false);
        mContext = context;

        Button buttonClose = (Button)findViewById(R.id.button_close);
        buttonClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });

        layout = (LinearLayout)findViewById(R.id.linear_layout);
    }

    public void addKeyValuePair(String key, String value) {

        TextView textView_key = (TextView)getLayoutInflater().inflate(R.layout.row_key, layout, false);
        TextView textView_value = (TextView)getLayoutInflater().inflate(R.layout.row_value, layout, false);

        textView_key.setText(key);
        textView_value.setText(value);

        layout.addView(textView_key);
        layout.addView(textView_value);
    }
}
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="dummy_text"
    android:id="@+id/row_textView_key"
    android:layout_marginTop="20sp"
    android:textColor="@color/white"
    android:textStyle="bold"
    android:textSize="30sp"
    android:layout_marginLeft="20sp">
</TextView>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="dummy_value"
    android:id="@+id/row_textView_value"
    android:layout_marginTop="5sp"
    android:layout_marginLeft="20sp"
    android:textColor="@color/white"
    android:textSize="30sp">
</TextView>
CustomDialog cd = new CustomDialog(this);
cd.addKeyValuePair("key","value");