我正在开展一个小项目,用户在尝试登录后会看到他的结果,并且他已经在我的应用上进行了测验。
So i'm using a page like this to display result.
但我无法显示我的分数。此页面还有一个分享您的分数的选项,该分数工作正常并显示分数。Share option containing result.
我很友好地附上我的XML和Java代码,请帮助我解决那些会给你很大帮助的问题。
谢谢&此致
BugAdder
XML代码:
sectionIndexTitles
Java代码:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center|top">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:padding="20dp"
android:textAlignment="center"
android:textSize="25sp"
android:textStyle="bold"
android:text="@string/login_score_title"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:padding="20dp"
android:textSize="25sp"
android:textStyle="bold"
android:text="@string/score_result"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textStyle="bold"
android:id="@+id/login_score_text"/>
<Button
android:padding="20dp"
android:layout_marginTop="75dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/login_share_button"
android:text="@string/share_button"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
android:text="@string/exit_button"
android:drawableEnd="@drawable/arrow_right"
android:drawableRight="@drawable/arrow_right"
android:id="@+id/login_exit_button"/>
</LinearLayout>
</ScrollView>
答案 0 :(得分:2)
mScore设置为异步任务。所以你必须在onPostExecute中设置文本。尝试以下代码:
public class QuizLoginScore extends AppCompatActivity {
private List<ScoreItem> mScoreItems = new ArrayList<>();
private ProgressDialog mProgressDialog;
private String mScore;
private TextView mScoreTextView;
private Button mShareButton;
private Button mExitButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.score_login_quiz);
mScoreTextView = (TextView)findViewById(R.id.login_score_text);
mShareButton = (Button)findViewById(R.id.login_share_button);
mExitButton = (Button)findViewById(R.id.login_exit_button);
new FetchScoresTask().execute();
mScoreTextView.setText(mScore);
mExitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(Build.VERSION.SDK_INT >= 21){
finishAndRemoveTask();
}else{
finishAffinity();
System.exit(0);
}
}
});
mShareButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_TEXT,getScoreShareReport(mScore));
i.putExtra(Intent.EXTRA_SUBJECT,getString(R.string.score_report_subject));
i = Intent.createChooser(i,getString(R.string.send_report));
startActivity(i);
}
});
}
public String getScoreShareReport(String score){
String result;
StringBuilder sb = new StringBuilder();
sb.append("QuizApp!").
append("\n\n").
append("I Just Scored").
append(" \' ").
append(score).
append(" \' ").
append("marks out of \' 100 \' in Latest Quiz on QuizApp!\nWhats your Score Huh?");
result = sb.toString();
return result;
}
private class FetchScoresTask extends AsyncTask<Void, Void, List<ScoreItem>> {
@Override
protected void onPreExecute() {
mProgressDialog = new ProgressDialog(QuizLoginScore.this);
mProgressDialog.setTitle("Downloading Score");
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
mProgressDialog.setMessage("Please wait while the score is being downloaded!");
mProgressDialog.setCancelable(false);
mProgressDialog.show();
}
@Override
protected List<ScoreItem> doInBackground(Void... params) {
return new ScoreFetcher().fetchScoreItems();
}
@Override
protected void onPostExecute(List<ScoreItem> scoreItems) {
mScoreItems = scoreItems;
for(int i = 0; i < mScoreItems.size(); i++){
if(mScoreItems.get(i).getUserId().equals(QuizLogin.mUserIdString)){
mScore = String.valueOf(mScoreItems.get(i).getScore());
mScoreTextView.setText(mScore);
break;
}
}
mProgressDialog.dismiss();
}
}
}
答案 1 :(得分:0)
将您的mScore字符串放入日志中,以查看您是否在logcat中获取数据。还可以尝试更改textview的颜色。还将textview.settext()放在onpostexecute();方法
答案 2 :(得分:0)
从onPostExecute()
设置文字:
@Override
protected void onPostExecute(List<ScoreItem> scoreItems) {
mScoreItems = scoreItems;
for(int i = 0; i < mScoreItems.size(); i++){
if(mScoreItems.get(i).getUserId().equals(QuizLogin.mUserIdString)){
mScore = String.valueOf(mScoreItems.get(i).getScore());
mScoreTextView.setText(mScore);
break;
}
}
mProgressDialog.dismiss();
}