修复listview数据在android

时间:2017-07-24 03:59:31

标签: android listview android-activity

我写了一个Android应用程序,它只显示一个列表视图。 一切都很好,除了一个。 我在平板电脑上安装了我的应用程序,当我运行应用程序时,就会出现问题。 ==取代。那是 。 。 。 当我按下电源按钮关闭屏幕并再次按电源按钮打开屏幕时,整个列表视图数据消失。

我认为这是活动生命周期的一些问题,但我无法解决这个问题。

请帮帮我..

这是我的代码......

package com.jo.fivemancard;

import android.content.Context;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    public ScoreListAdapter scoreListAdapter;
    class RoundScore  {
        public int score1 = 0;
        public int score2 = 0;
        public int score3 = 0;
        public int score4 = 0;
        public int score5 = 0;
        public RoundScore(){
            score1 = score2 = score3 = score4 = score5 = 0;
        }
    }

    class ScoreListAdapter extends BaseAdapter{
        public Context context;

        class ViewHolder{
            TextView textViewScore1;
            TextView textViewScore2;
            TextView textViewScore3;
            TextView textViewScore4;
            TextView textViewScore5;
        }



        public ArrayList<RoundScore> scoreList;

        public ScoreListAdapter(Context cont){
            context = cont;
            scoreList = new ArrayList<RoundScore>();
        }

        public void appendData(RoundScore rc){
            scoreList.add(rc);
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return scoreList.size();
        }

        @Override
        public Object getItem(int position) {
            return scoreList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder vh;

            if(convertView == null) {
                LayoutInflater inflater = (LayoutInflater) context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View rowView = inflater.inflate(R.layout.listviewitem_score, parent, false);

                vh = new ViewHolder();
                vh.textViewScore1 = (TextView)rowView.findViewById(R.id.textViewScore1);
                vh.textViewScore2 = (TextView)rowView.findViewById(R.id.textViewScore2);
                vh.textViewScore3 = (TextView)rowView.findViewById(R.id.textViewScore3);
                vh.textViewScore4 = (TextView)rowView.findViewById(R.id.textViewScore4);
                vh.textViewScore5 = (TextView)rowView.findViewById(R.id.textViewScore5);

                convertView = rowView;
            }else {
                vh = (ViewHolder) convertView.getTag();
            }
            RoundScore rc = scoreList.get(position);
            vh.textViewScore1.setText("" + rc.score1);
            vh.textViewScore2.setText("" + rc.score2);
            vh.textViewScore3.setText("" + rc.score3);
            vh.textViewScore4.setText("" + rc.score4);
            vh.textViewScore5.setText("" + rc.score5);

            convertView.setTag(vh);
            return convertView;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.buttonNextRound).setOnClickListener(this);
        findViewById(R.id.floatActionButtonCalculate).setOnClickListener(this);
        scoreListAdapter = new ScoreListAdapter(this);
        ((ListView)findViewById(R.id.listViewScore)).setAdapter(scoreListAdapter);

        ((TextView)findViewById(R.id.textViewPlayer1)).setText(GlobalConstant.playername1);
        ((TextView)findViewById(R.id.textViewPlayer2)).setText(GlobalConstant.playername2);
        ((TextView)findViewById(R.id.textViewPlayer3)).setText(GlobalConstant.playername3);
        ((TextView)findViewById(R.id.textViewPlayer4)).setText(GlobalConstant.playername4);
        ((TextView)findViewById(R.id.textViewPlayer5)).setText(GlobalConstant.playername5);


    }

    public int getScore(String score){
        if(score.trim().equals("")){
            return 0;
        }
        if(score.length() == 0){
            return 0;
        }
        boolean isNumber;
        if(score.charAt(0) == '-'){
            isNumber = android.text.TextUtils.isDigitsOnly(score.substring(1));
        }else{
            isNumber = android.text.TextUtils.isDigitsOnly(score);
        }
        if(isNumber) {
            return Integer.parseInt(score.trim());
        }
        return 0;
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.buttonNextRound){
            String score1 = ((EditText)findViewById(R.id.editTextNewScorePlayer1)).getText().toString();
            String score2 = ((EditText)findViewById(R.id.editTextNewScorePlayer2)).getText().toString();
            String score3 = ((EditText)findViewById(R.id.editTextNewScorePlayer3)).getText().toString();
            String score4 = ((EditText)findViewById(R.id.editTextNewScorePlayer4)).getText().toString();
            String score5 = ((EditText)findViewById(R.id.editTextNewScorePlayer5)).getText().toString();

            if(score1.trim().equals("")){
                score1 = "0";
            }

            RoundScore rc = new RoundScore();
            rc.score1 = getScore(score1);
            rc.score2 = getScore(score2);
            rc.score3 = getScore(score3);
            rc.score4 = getScore(score4);
            rc.score5 = getScore(score5);

            if(rc.score1 == 0 && rc.score2 == 0 && rc.score3 == 0 && rc.score4 == 0 && rc.score5 == 0){
                Toast.makeText(this, "Bad Input...", Toast.LENGTH_SHORT).show();
                return;
            }
            scoreListAdapter.appendData(rc);
            ((ListView)findViewById(R.id.listViewScore)).setSelection(scoreListAdapter.getCount() - 1);
            Toast.makeText(this, "Next Round...", Toast.LENGTH_SHORT).show();
            ((EditText)findViewById(R.id.editTextNewScorePlayer1)).setText("");
            ((EditText)findViewById(R.id.editTextNewScorePlayer2)).setText("");
            ((EditText)findViewById(R.id.editTextNewScorePlayer3)).setText("");
            ((EditText)findViewById(R.id.editTextNewScorePlayer4)).setText("");
            ((EditText)findViewById(R.id.editTextNewScorePlayer5)).setText("");
        }
        if(v.getId() == R.id.floatActionButtonCalculate){
            RoundScore rs = new RoundScore();
            int i;
            for(i=0;i<scoreListAdapter.scoreList.size();i++){
                rs.score1 += scoreListAdapter.scoreList.get(i).score1;
                rs.score2 += scoreListAdapter.scoreList.get(i).score2;
                rs.score3 += scoreListAdapter.scoreList.get(i).score3;
                rs.score4 += scoreListAdapter.scoreList.get(i).score4;
                rs.score5 += scoreListAdapter.scoreList.get(i).score5;
            }
            if(findViewById(R.id.buttonNextRound).isEnabled()){
                String str1 = ((EditText)findViewById(R.id.editTextNewScorePlayer1)).getText().toString().trim();
                String str2 = ((EditText)findViewById(R.id.editTextNewScorePlayer2)).getText().toString().trim();
                String str3 = ((EditText)findViewById(R.id.editTextNewScorePlayer3)).getText().toString().trim();
                String str4 = ((EditText)findViewById(R.id.editTextNewScorePlayer4)).getText().toString().trim();
                String str5 = ((EditText)findViewById(R.id.editTextNewScorePlayer5)).getText().toString().trim();
                if(!str1.equals("") || !str2.equals("") || !str3.equals("") || !str4.equals("") || !str5.equals("")){
                    Toast.makeText(this,"It seems you wanna add score info.",Toast.LENGTH_SHORT);
                    return;
                }


                findViewById(R.id.buttonNextRound).setEnabled(false);
                ((EditText)findViewById(R.id.editTextNewScorePlayer1)).setText("" + rs.score1);
                ((EditText)findViewById(R.id.editTextNewScorePlayer2)).setText("" + rs.score2);
                ((EditText)findViewById(R.id.editTextNewScorePlayer3)).setText("" + rs.score3);
                ((EditText)findViewById(R.id.editTextNewScorePlayer4)).setText("" + rs.score4);
                ((EditText)findViewById(R.id.editTextNewScorePlayer5)).setText("" + rs.score5);

                ((EditText)findViewById(R.id.editTextNewScorePlayer1)).setEnabled(false);
                ((EditText)findViewById(R.id.editTextNewScorePlayer2)).setEnabled(false);
                ((EditText)findViewById(R.id.editTextNewScorePlayer3)).setEnabled(false);
                ((EditText)findViewById(R.id.editTextNewScorePlayer4)).setEnabled(false);
                ((EditText)findViewById(R.id.editTextNewScorePlayer5)).setEnabled(false);

            }else{
                findViewById(R.id.buttonNextRound).setEnabled(true);
                ((EditText)findViewById(R.id.editTextNewScorePlayer1)).setText("");
                ((EditText)findViewById(R.id.editTextNewScorePlayer2)).setText("");
                ((EditText)findViewById(R.id.editTextNewScorePlayer3)).setText("");
                ((EditText)findViewById(R.id.editTextNewScorePlayer4)).setText("");
                ((EditText)findViewById(R.id.editTextNewScorePlayer5)).setText("");

                ((EditText)findViewById(R.id.editTextNewScorePlayer1)).setEnabled(true);
                ((EditText)findViewById(R.id.editTextNewScorePlayer2)).setEnabled(true);
                ((EditText)findViewById(R.id.editTextNewScorePlayer3)).setEnabled(true);
                ((EditText)findViewById(R.id.editTextNewScorePlayer4)).setEnabled(true);
                ((EditText)findViewById(R.id.editTextNewScorePlayer5)).setEnabled(true);
            }
        }
    }

    private Boolean exit = false;
    @Override
    public void onBackPressed() {
        if (exit) {
            finish(); // finish activity
        } else {
            Toast.makeText(this, "Press Back again to Exit.",
                    Toast.LENGTH_SHORT).show();
            exit = true;
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    exit = false;
                }
            }, 1 * 1000);

        }

    }

}

0 个答案:

没有答案