2014-10-20 66 views
0

我的應用程序中有一個autocompletetextview,一個textview和一個按鈕。當按鈕被點擊時,它將搜索數據庫中的單詞,等於textview上的單詞。在TEXTVIEW中添加分數

FLOW:從autocompletetextview •字(輸入通過點擊imagebuttons)>搜索字(使用按鈕) 條件: •如果單詞在DATABSE,TextView的將顯示相應的得分(也以dBASE ),那麼自動完成的textview應該清除文本(做出另一個詞)。

問題: 1.我如何添加第一個分數,這已經在textview和下一個單詞的分數? 2.如何禁用AutoCompleteTextview中的「EditText」效果?我需要這個給予「優先級」,只通過imagebuttons進行輸入(我的意思是每當你有一個autocompletetextview它就像你有一個編輯文本)。

任何建議將不勝感激。 我不知道該怎麼做,所以我問這個問題。我是Android編程的新手。我希望你能理解我的問題,我的語法:)

繼承人的代碼我使用: MainActivity.java

DBAdapter dbHelper; 
TextView score; 
protected static final String TAG = null; 
String generatedString = " "; 
AutoCompleteTextView text; 
TextView timer; 
ImageButton searchWord; 
ImageButton image1, image2, image3, image4, image5, image6, image7, image8, 
image9, image10, image11, image12, image13, image14, image15, image16; 
private CountDownTimer countDownTimer; 
private boolean timeHasStarted = false; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 

dbHelper =new DBAdapter(this); 
searchWord = (ImageButton) findViewById(R.id.searchWord); 
image1 = (ImageButton) findViewById(R.id.Button1); 

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

final int[] myPics = { images here }; 


int rando = (int)(Math.random()* 5); 
image1.setImageResource(myPics[rando]); 
image1.setId(myPics[rando]); 


OnClickListener myCommoClickListner = new OnClickListener(){ 

@Override 
public void onClick(View arg0) { 
// start timer when 1 of 16 imagebutton is click (display letters = image in imagebutton 
}; 



image1.setOnClickListener(myCommoClickListner); 
} 

@TargetApi(Build.VERSION_CODES.GINGERBREAD) @SuppressLint("NewApi") public class MyCountDownTimer extends CountDownTimer{ 
public MyCountDownTimer(long millisInFuture, long countDownInterval){ 
super(millisInFuture, countDownInterval); 
} 

@TargetApi(Build.VERSION_CODES.GINGERBREAD) @SuppressLint("NewApi") @Override 
public void onTick(long millisUntilFinished) { 
long millis = millisUntilFinished; 
String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis), 
TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)), 
TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))); 
System.out.println(hms); 
timer.setText(hms); 

} 

@Override 
public void onFinish() { 
timer.setText("Time's Up!"); 
} 
} 

//Search 
public void viewWord(View view) 
{ 
score = (TextView)findViewById(R.id.yourScore); 
String s1= search.getText().toString(); 
String s2= dbHelper.getData(s1); 
score.setText(s2); 
} 
} 

這是我將對DBAdapter代碼中搜索代碼: 公共字符串的getData(字符串字) { SQLiteDatabase db = helper.getWritableDatabase(); String [] columns = {DBHelper.WORD,DBHelper.SCORE};遊標cursor = db.query(DBHelper.TABLE_NAME,columns,DBHelper.WORD +「='」+ word +「'」,null,null,null,null); StringBuffer buffer = new StringBuffer(); (cursor.moveToNext()) int index1 = cursor.getColumnIndex(DBHelper.WORD); int index2 = cursor.getColumnIndex(DBHelper.SCORE); String words = cursor.getString(index1); String score = cursor.getString(index2); buffer.append(score +「\ n」); } return buffer.toString(); }

回答

0

你的問題是有點不清楚,但我會盡力回答基於我如何解釋這一點:

  1. 我如何添加率先得分,這已經是textview和下一個單詞的分數?

我假設你要總結第一得分和下一個分數?

在您的DBAdapter類中創建一個類來處理您的搜索和求和。讓光標移動到您的搜索值的第一個實例並將其存儲爲您的第一個值。接下來,有一個新的查詢移動到數據庫中的下一行。將此值存儲爲第二個值。關閉你的光標。總結第一個值和第二個值並將其返回。

也許你可以嘗試一些與此類似:

public String getData(String word) 
    { 
    SQLiteDatabase db = helper.getWritableDatabase(); 
    String[] columns={DBHelper.WORD, DBHelper.SCORE}; 
    Cursor cursor=db.query(DBHelper.TABLE_NAME, columns, DBHelper.WORD+ "= '"+word+"'", null, null, null, null); 
    StringBuffer buffer = new StringBuffer(); 
    cursor.moveToFirst(); 
    // Given that you are only finding one instance, while is not necessary and in fact may not do what you want. It will return the LAST instance of your query value 
     int index1 =cursor.getColumnIndex(DBHelper.WORD); 
     int index2 =cursor.getColumnIndex(DBHelper.SCORE); 
     String words =cursor.getString(index1); 
     String score =cursor.getString(index2); 

    cursor=db.query(DBHelper.TABLE_NAME, columns, "*", null, null, null, null); // "*" should be the wildcard for SQL 

     cursor.moveToNext(); 
     int indexSecond1 =cursor.getColumnIndex(DBHelper.WORD); 
     int indexSecond2 =cursor.getColumnIndex(DBHelper.SCORE); 
     // Do something with the data 


     buffer.append(score +"\n"); 

    return buffer.toString(); 
} 
  • 我怎樣才能禁止在AutoCompleteTextview了 「的EditText」 的效果?我需要這個給予「優先級」,只通過imagebuttons進行輸入(我的意思是每當你有一個autocompletetextview它就像你有一個編輯文本)。
  • 您可以嘗試在你的XML

    或編程設置inputTypenoneyourEditText.setKeyListener(null);

    +0

    是的,你是正確的。但是我仍然不知道如何去做第一個。請幫幫我。 – 2014-10-20 18:59:56

    +0

    修改您的getData以獲得附加查詢。 – erad 2014-10-20 20:51:42