Reputation: 151
I have a autocompletetextview, a textview and a button in my app. When the button is clicked it will search the word in the database equals to the word on the textview.
FLOW: • Word from autocompletetextview (input by clicking imagebuttons) > Search word (using the button) Conditions: • If the word is in the databse, the textview will display the corresponding score(also in the dbase) then the autocomplete textview should clear the text (to make another word).
QUESTIONS: 1. how can i add the first score, which is already in the textview and the score of the next word? 2. how can i disable the "EditText" effect in the AutoCompleteTextview? I need this to give "Priority" on only making inputs via imagebuttons (i mean whenever you have an autocompletetextview its like you have an edit text).
any suggestions would be highly appreciated. I don't know how to do it so I am asking this question. I am a newbie in android programming. I hope you can understand my question and my grammar :)
heres the code i use: 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);
}
}
this is the code in my dbadapter in searching the code: 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(); while(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(); }
Upvotes: 0
Views: 407
Reputation: 1786
Your questions are a little unclear, but I will try to answer based on how I'm interpreting this:
- how can i add the first score, which is already in the textview and the score of the next word?
I'm assuming you want to sum the first score and the next score?
Create a class in your DBAdapter class to handle your search and sum. Have the cursor move to the first instance of your search value and store that as your first value. Next, have a new query to move to the next row in the database. Store this value as your second value. Close your cursor. Sum the first value and the second value and return it.
Maybe you can try something similar to this:
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();
}
- how can i disable the "EditText" effect in the AutoCompleteTextview? I need this to give "Priority" on only making inputs via imagebuttons (i mean whenever you have an autocompletetextview its like you have an edit text).
You can try setting inputType
to none in your XML
or programmatically: yourEditText.setKeyListener(null);
Upvotes: 0