[英]Score not working
這是我的密碼
//declare a boolean variable for score private int optionTxtView = 0 ; //Search word //display score public void viewWord(View view) { score = (TextView)findViewById(R.id.yourScore); tv2 = (TextView)findViewById(R.id.tv2); tv3 = (TextView)findViewById(R.id.tv3); searchWord = (ImageView) findViewById(R.id.searchWord); wordList = (ListView) findViewById(R.id.wordList); text = (AutoCompleteTextView) findViewById(R.id.textHere); wordList = (ListView) findViewById(R.id.wordList); String s1= search.getText().toString(); String s2= dbHelper.getData(s1); if(optionTxtView == 0){ //display the score on textview1 tv2.setText(s2); optionTxtView = 1; } else{ //display the score on textview2 tv3.setText(s2); optionTxtView = 0; } calculate(); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new ArrayList<String>()); wordList.setAdapter(adapter); adapter.add(text.getText().toString()); adapter.notifyDataSetChanged(); text.setText(null); } private void calculate(){ x = Integer.parseInt(tv2.getText().toString()); y = Integer.parseInt(tv3.getText().toString()); z = x + y; score.setText(Integer.toString(z)); }
所以基本上我決定擁有3個textview,score,tv2和tv3。 因此,第一個單詞的第一個分數應該在tv2中,第二個分數應該在tv3中。 並且這兩個textview的總和應為得分。 將文本放在textview上后,它將通過單擊imagebutton從數據庫的textview中搜索單詞。 如果單詞在數據庫中,它將自動將單詞放入列表視圖中,同時應在兩個文本視圖中顯示分數。
我真的需要幫助。 我盡了一切努力,但是當從屬性中設置textview tv2和tv3中的值/數字時,它永遠不會添加EXCEPT。 但是,只要數字/值來自數據庫(因為它應該是這樣),它就永遠不會添加!
任何幫助將不勝感激。 請幫助我提供我的代碼。
onCreate方法
//TAKE NOTE: 16 IMAGEBUTTONS protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); dbHelper =new DBAdapter(this); search = (EditText) findViewById(R.id.textHere); words = (EditText) findViewById(R.id.edWord); scores = (EditText) findViewById(R.id.edScore); show_addWord = (ImageView) findViewById(R.id.show_addWord); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new ArrayList<String>()); wordList.setAdapter(adapter); score = (TextView)findViewById(R.id.yourScore); tv2 = (TextView)findViewById(R.id.tv2); tv3 = (TextView)findViewById(R.id.tv3); wordList = (ListView) findViewById(R.id.wordList); text = (AutoCompleteTextView) findViewById(R.id.textHere); wordList = (ListView) findViewById(R.id.wordList); blizzard = (ImageView) findViewById(R.id.blizzard); bomb = (ImageView) findViewById(R.id.bomb); searchWord = (ImageView) findViewById(R.id.searchWord); wordList = (ListView) findViewById(R.id.wordList); //this is til 16 image1 = (ImageButton) findViewById(R.id.Button1); image16 = (ImageButton) findViewById(R.id.Button16); timer = (TextView) this.findViewById(R.id.timer); timer.setText("00:00:30"); countDownTimer = new MyCountDownTimer(30000,1000); final AutoCompleteTextView text = (AutoCompleteTextView) findViewById(R.id.textHere); final ImageButton image1 = (ImageButton) findViewById(R.id.Button1); //this is til 16 final ImageButton image16 = (ImageButton) findViewById(R.id.Button16); final int[] myPics = { R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.e }; //this is til z //this is til 16 int rando = (int)(Math.random()* 5); image1.setImageResource(myPics[rando]); image1.setId(myPics[rando]); //this is from a -z OnClickListener myCommoClickListner = new OnClickListener(){ @Override public void onClick(View arg0) { Log.i(TAG,"arg0.getId() = " + arg0.getId()); if(arg0.getId()==R.drawable.a){ Log.i(TAG,"arg0.getId()="+arg0.getId()); generatedString=generatedString+"a"; //[PLACEE RESPACTIVE CHARACTEER HERE] text.setText(generatedString); ((ImageButton) arg0).setImageResource(R.drawable.changea); if (!timeHasStarted) { countDownTimer.start(); timeHasStarted = true; } else{ if(arg0.getId()==R.drawable.z){ Log.i(TAG,"arg0.getId() = " + arg0.getId()); generatedString = generatedString+"z"; text.setText(generatedString); ((ImageButton) arg0).setImageResource(R.drawable.changez); if (!timeHasStarted) { countDownTimer.start(); timeHasStarted = true; } } } }; image1.setOnClickListener(myCommoClickListner); image2.setOnClickListener(myCommoClickListner); image3.setOnClickListener(myCommoClickListner); image4.setOnClickListener(myCommoClickListner); image5.setOnClickListener(myCommoClickListner); image6.setOnClickListener(myCommoClickListner); image7.setOnClickListener(myCommoClickListner); image8.setOnClickListener(myCommoClickListner); image9.setOnClickListener(myCommoClickListner); image10.setOnClickListener(myCommoClickListner); image11.setOnClickListener(myCommoClickListner); image12.setOnClickListener(myCommoClickListner); image13.setOnClickListener(myCommoClickListner); image14.setOnClickListener(myCommoClickListner); image15.setOnClickListener(myCommoClickListner); image16.setOnClickListener(myCommoClickListner); }
logcat的
從屬性中設置文本視圖tv2和tv3中的值/數字時,它永遠不會添加EXCEPT
您將需要將代碼放入onClick
按鈕或任何偵聽用戶操作的內容。 之后,您可以獲取textview's
值並對其進行處理。
從代碼的外觀來看,我建議您將所有初始化都移到 onCreate
:
//move this code to onCreate
score = (TextView)findViewById(R.id.yourScore);
tv2 = (TextView)findViewById(R.id.tv2);
tv3 = (TextView)findViewById(R.id.tv3);
searchWord = (ImageView) findViewById(R.id.searchWord);
wordList = (ListView) findViewById(R.id.wordList);
text = (AutoCompleteTextView) findViewById(R.id.textHere);
wordList = (ListView) findViewById(R.id.wordList);
//also move this
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new ArrayList<String>());
wordList.setAdapter(adapter);
之后,在onClick
(或您喜歡的任何東西viewWord()
中調用viewWord()
),例如:
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
viewWord(v); //v is your button
}
});
UPDATE
在logcat
查看異常后,似乎出現了錯誤,因為整數具有一些whitespaces
:“ 2” <-2之后的空格
嘗試:
x = Integer.parseInt(tv2.getText().toString().replaceAll("\\s",""));
y = Integer.parseInt(tv3.getText().toString().replaceAll("\\s",""));
這個想法是在將所有空白解析為Integer
之前將其刪除 。
如果您有任何疑問或想念我,請隨時發表評論:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.