Android: Loading Fragment is Slow to Get String Values From SQLite - android

I try to use SQLite database to get String values and set these values into TextViews in Fragment.The String values are gotten when Boolean variables are returned true, and when loading the fragment, it takes little bit long time to load.
I have tried to use multiple threads to solve the problem but it does not work apparently. Do you have any idea to be faster load the fragment?
This is a class that be called in the fragment
public class Schedule_Config_Monday{
private Context mContext;
public Schedule_Config_Monday(Context context){
this.mContext = context;
}
public void Monday_Schedule_Settings_0(final TextView textView_1,final TextView textView_2,final TextView textView_3,final TextView textView_4,final TextView textView_5,
final TextView textView_6,final TextView textView_7,final TextView textView_8,final TextView textView_9,final TextView textView_10,
final TextView textView_11,final TextView textView_12,final TextView textView_13,final TextView textView_14,final TextView textView_15,
final TextView textView_16,final TextView textView_17,final TextView textView_18,final TextView textView_19,final TextView textView_20,
final TextView textView_21,final TextView textView_22,final TextView textView_23,final TextView textView_24){
final DatabaseTimetable databaseTimetable = new DatabaseTimetable(mContext);
final boolean[] monday_b = new boolean[]{
databaseTimetable.Monday_Start_End_Time(0,1),
databaseTimetable.Monday_Start_End_Time(0,2),
databaseTimetable.Monday_Start_End_Time(0,3),
databaseTimetable.Monday_Start_End_Time(0,4),
databaseTimetable.Monday_Start_End_Time(0,5),
databaseTimetable.Monday_Start_End_Time(0,6),
databaseTimetable.Monday_Start_End_Time(0,7),
databaseTimetable.Monday_Start_End_Time(0,8),
databaseTimetable.Monday_Start_End_Time(0,9),
databaseTimetable.Monday_Start_End_Time(0,10),
databaseTimetable.Monday_Start_End_Time(0,11),
databaseTimetable.Monday_Start_End_Time(0,12),
databaseTimetable.Monday_Start_End_Time(0,13),
databaseTimetable.Monday_Start_End_Time(0,14),
databaseTimetable.Monday_Start_End_Time(0,15),
databaseTimetable.Monday_Start_End_Time(0,16),
databaseTimetable.Monday_Start_End_Time(0,17),
databaseTimetable.Monday_Start_End_Time(0,18),
databaseTimetable.Monday_Start_End_Time(0,19),
databaseTimetable.Monday_Start_End_Time(0,20),
databaseTimetable.Monday_Start_End_Time(0,21),
databaseTimetable.Monday_Start_End_Time(0,22),
databaseTimetable.Monday_Start_End_Time(0,23),
databaseTimetable.Monday_Start_End_Time(0,0)
};
final TextView[] Mon_Text = new TextView[]{
textView_1,textView_2,textView_3,textView_4,textView_5,
textView_6,textView_7,textView_8,textView_9,textView_10,
textView_11,textView_12,textView_13,textView_14,textView_15,
textView_16,textView_17,textView_18,textView_19,textView_20,
textView_21,textView_22,textView_23,textView_24};
final int start = 0;
if (monday_b[0]){
Mon_Text[0].post(new Runnable() {
#Override
public void run() {
int end = 1;
Mon_Text[0].setText(databaseTimetable.Monday_Title(start,end));
Mon_Text[0].setTextColor(Color.parseColor(databaseTimetable.Monday_Text_Color(start,end)));
Mon_Text[0].setBackgroundColor(Color.parseColor(databaseTimetable.Monday_Text_BG_Color(start,end)));
}
});
}
else if (monday_b[1]){
Mon_Text[0].post(new Runnable() {
#Override
public void run() {
int end = 2;
Mon_Text[0].setText(databaseTimetable.Monday_Title(start,end));
Mon_Text[0].setTextColor(Color.parseColor(databaseTimetable.Monday_Text_Color(start,end)));
for (int i = 0; i < end; i++){
Mon_Text[i].setBackgroundColor(Color.parseColor(databaseTimetable.Monday_Text_BG_Color(start,end)));
}
}
});
}
else if (monday_b[2]){
Mon_Text[0].post(new Runnable() {
#Override
public void run() {
int end = 3;
Mon_Text[0].setText(databaseTimetable.Monday_Title(start,end));
Mon_Text[0].setTextColor(Color.parseColor(databaseTimetable.Monday_Text_Color(start,end)));
for (int i = 0; i < end; i++){
Mon_Text[i].setBackgroundColor(Color.parseColor(databaseTimetable.Monday_Text_BG_Color(start,end)));
}
}
});
}
else if (monday_b[3]){
Mon_Text[0].post(new Runnable() {
#Override
public void run() {
int end = 4;
Mon_Text[0].setText(databaseTimetable.Monday_Title(start,end));
Mon_Text[0].setTextColor(Color.parseColor(databaseTimetable.Monday_Text_Color(start,end)));
for (int i = 0; i < end; i++){
Mon_Text[i].setBackgroundColor(Color.parseColor(databaseTimetable.Monday_Text_BG_Color(start,end)));
}
}
});
}
else if (monday_b[4]){
Mon_Text[0].post(new Runnable() {
#Override
public void run() {
int end = 5;
Mon_Text[0].setText(databaseTimetable.Monday_Title(start,end));
Mon_Text[0].setTextColor(Color.parseColor(databaseTimetable.Monday_Text_Color(start,end)));
for (int i = 0; i < end; i++){
Mon_Text[i].setBackgroundColor(Color.parseColor(databaseTimetable.Monday_Text_BG_Color(start,end)));
}
}
});
}
else if (monday_b[5]){
Mon_Text[0].post(new Runnable() {
#Override
public void run() {
int end = 6;
Mon_Text[0].setText(databaseTimetable.Monday_Title(start,end));
Mon_Text[0].setTextColor(Color.parseColor(databaseTimetable.Monday_Text_Color(start,end)));
for (int i = 0; i < end; i++){
Mon_Text[i].setBackgroundColor(Color.parseColor(databaseTimetable.Monday_Text_BG_Color(start,end)));
}
}
});
}
else if (monday_b[6]){
Mon_Text[0].post(new Runnable() {
#Override
public void run() {
int end = 7;
Mon_Text[0].setText(databaseTimetable.Monday_Title(start,end));
Mon_Text[0].setTextColor(Color.parseColor(databaseTimetable.Monday_Text_Color(start,end)));
for (int i = 0; i < end; i++){
Mon_Text[i].setBackgroundColor(Color.parseColor(databaseTimetable.Monday_Text_BG_Color(start,end)));
}
}
});
}
else if (monday_b[7]){
Mon_Text[0].post(new Runnable() {
#Override
public void run() {
int end = 8;
Mon_Text[0].setText(databaseTimetable.Monday_Title(start,end));
Mon_Text[0].setTextColor(Color.parseColor(databaseTimetable.Monday_Text_Color(start,end)));
for (int i = 0; i < end; i++){
Mon_Text[i].setBackgroundColor(Color.parseColor(databaseTimetable.Monday_Text_BG_Color(start,end)));
}
}
});
}
else if (monday_b[8]){
Mon_Text[0].post(new Runnable() {
#Override
public void run() {
int end = 9;
Mon_Text[0].setText(databaseTimetable.Monday_Title(start,end));
Mon_Text[0].setTextColor(Color.parseColor(databaseTimetable.Monday_Text_Color(start,end)));
for (int i = 0; i < end; i++){
Mon_Text[i].setBackgroundColor(Color.parseColor(databaseTimetable.Monday_Text_BG_Color(start,end)));
}
}
});
}
else if (monday_b[9]){
Mon_Text[0].post(new Runnable() {
#Override
public void run() {
int end = 10;
Mon_Text[0].setText(databaseTimetable.Monday_Title(start,end));
Mon_Text[0].setTextColor(Color.parseColor(databaseTimetable.Monday_Text_Color(start,end)));
for (int i = 0; i < end; i++){
Mon_Text[i].setBackgroundColor(Color.parseColor(databaseTimetable.Monday_Text_BG_Color(start,end)));
}
}
});
}
else if (monday_b[10]){
Mon_Text[0].post(new Runnable() {
#Override
public void run() {
int end = 11;
Mon_Text[0].setText(databaseTimetable.Monday_Title(start,end));
Mon_Text[0].setTextColor(Color.parseColor(databaseTimetable.Monday_Text_Color(start,end)));
for (int i = 0; i < end; i++){
Mon_Text[i].setBackgroundColor(Color.parseColor(databaseTimetable.Monday_Text_BG_Color(start,end)));
}
}
});
}
else if (monday_b[11]){
Mon_Text[0].post(new Runnable() {
#Override
public void run() {
int end = 12;
Mon_Text[0].setText(databaseTimetable.Monday_Title(start,end));
Mon_Text[0].setTextColor(Color.parseColor(databaseTimetable.Monday_Text_Color(start,end)));
for (int i = 0; i < end; i++){
Mon_Text[i].setBackgroundColor(Color.parseColor(databaseTimetable.Monday_Text_BG_Color(start,end)));
}
}
});
}
else if (monday_b[12]){
Mon_Text[0].post(new Runnable() {
#Override
public void run() {
int end = 13;
Mon_Text[0].setText(databaseTimetable.Monday_Title(start,end));
Mon_Text[0].setTextColor(Color.parseColor(databaseTimetable.Monday_Text_Color(start,end)));
for (int i = 0; i < end; i++){
Mon_Text[i].setBackgroundColor(Color.parseColor(databaseTimetable.Monday_Text_BG_Color(start,end)));
}
}
});
}
else if (monday_b[13]){
Mon_Text[0].post(new Runnable() {
#Override
public void run() {
int end = 14;
Mon_Text[0].setText(databaseTimetable.Monday_Title(start,end));
Mon_Text[0].setTextColor(Color.parseColor(databaseTimetable.Monday_Text_Color(start,end)));
for (int i = 0; i < end; i++){
Mon_Text[i].setBackgroundColor(Color.parseColor(databaseTimetable.Monday_Text_BG_Color(start,end)));
}
}
});
}
else if (monday_b[14]){
Mon_Text[0].post(new Runnable() {
#Override
public void run() {
int end = 15;
Mon_Text[0].setText(databaseTimetable.Monday_Title(start,end));
Mon_Text[0].setTextColor(Color.parseColor(databaseTimetable.Monday_Text_Color(start,end)));
for (int i = 0; i < end; i++){
Mon_Text[i].setBackgroundColor(Color.parseColor(databaseTimetable.Monday_Text_BG_Color(start,end)));
}
}
});
}
else if (monday_b[15]){
Mon_Text[0].post(new Runnable() {
#Override
public void run() {
int end = 16;
Mon_Text[0].setText(databaseTimetable.Monday_Title(start,end));
Mon_Text[0].setTextColor(Color.parseColor(databaseTimetable.Monday_Text_Color(start,end)));
for (int i = 0; i < end; i++){
Mon_Text[i].setBackgroundColor(Color.parseColor(databaseTimetable.Monday_Text_BG_Color(start,end)));
}
}
});
}
else if (monday_b[16]){
Mon_Text[0].post(new Runnable() {
#Override
public void run() {
int end = 17;
Mon_Text[0].setText(databaseTimetable.Monday_Title(start,end));
Mon_Text[0].setTextColor(Color.parseColor(databaseTimetable.Monday_Text_Color(start,end)));
for (int i = 0; i < end; i++){
Mon_Text[i].setBackgroundColor(Color.parseColor(databaseTimetable.Monday_Text_BG_Color(start,end)));
}
}
});
}
else if (monday_b[17]){
Mon_Text[0].post(new Runnable() {
#Override
public void run() {
int end = 18;
Mon_Text[0].setText(databaseTimetable.Monday_Title(start,end));
Mon_Text[0].setTextColor(Color.parseColor(databaseTimetable.Monday_Text_Color(start,end)));
for (int i = 0; i < end; i++){
Mon_Text[i].setBackgroundColor(Color.parseColor(databaseTimetable.Monday_Text_BG_Color(start,end)));
}
}
});
}
else if (monday_b[18]){
Mon_Text[0].post(new Runnable() {
#Override
public void run() {
int end = 19;
Mon_Text[0].setText(databaseTimetable.Monday_Title(start,end));
Mon_Text[0].setTextColor(Color.parseColor(databaseTimetable.Monday_Text_Color(start,end)));
for (int i = 0; i < end; i++){
Mon_Text[i].setBackgroundColor(Color.parseColor(databaseTimetable.Monday_Text_BG_Color(start,end)));
}
}
});
}
else if (monday_b[19]){
Mon_Text[0].post(new Runnable() {
#Override
public void run() {
int end = 20;
Mon_Text[0].setText(databaseTimetable.Monday_Title(start,end));
Mon_Text[0].setTextColor(Color.parseColor(databaseTimetable.Monday_Text_Color(start,end)));
for (int i = 0; i < end; i++){
Mon_Text[i].setBackgroundColor(Color.parseColor(databaseTimetable.Monday_Text_BG_Color(start,end)));
}
}
});
}
else if (monday_b[20]){
Mon_Text[0].post(new Runnable() {
#Override
public void run() {
int end = 21;
Mon_Text[0].setText(databaseTimetable.Monday_Title(start,end));
Mon_Text[0].setTextColor(Color.parseColor(databaseTimetable.Monday_Text_Color(start,end)));
for (int i = 0; i < end; i++){
Mon_Text[i].setBackgroundColor(Color.parseColor(databaseTimetable.Monday_Text_BG_Color(start,end)));
}
}
});
}
else if (monday_b[21]){
Mon_Text[0].post(new Runnable() {
#Override
public void run() {
int end = 22;
Mon_Text[0].setText(databaseTimetable.Monday_Title(start,end));
Mon_Text[0].setTextColor(Color.parseColor(databaseTimetable.Monday_Text_Color(start,end)));
for (int i = 0; i < end; i++){
Mon_Text[i].setBackgroundColor(Color.parseColor(databaseTimetable.Monday_Text_BG_Color(start,end)));
}
}
});
}
else if (monday_b[22]){
Mon_Text[0].post(new Runnable() {
#Override
public void run() {
int end = 23;
Mon_Text[0].setText(databaseTimetable.Monday_Title(start,end));
Mon_Text[0].setTextColor(Color.parseColor(databaseTimetable.Monday_Text_Color(start,end)));
for (int i = 0; i < end; i++){
Mon_Text[i].setBackgroundColor(Color.parseColor(databaseTimetable.Monday_Text_BG_Color(start,end)));
}
}
});
}
else if (monday_b[23]){
Mon_Text[0].post(new Runnable() {
#Override
public void run() {
int end = 0;
Mon_Text[0].setText(databaseTimetable.Monday_Title(start,end));
Mon_Text[0].setTextColor(Color.parseColor(databaseTimetable.Monday_Text_Color(start,end)));
for (int i = 0; i < 24; i++){
Mon_Text[i].setBackgroundColor(Color.parseColor(databaseTimetable.Monday_Text_BG_Color(start,end)));
}
}
});
}
}
}
These are methods inside of a class extends SQLiteOpenHelper to get Title, Text Color and Background Color
public String Monday_Title(int start, int end){
sqLiteDatabase = this.getReadableDatabase();
String[] columns = new String[]{COLUMN_TITLE,COLUMN_SUBTITLE,
COLUMN_MON,COLUMN_START_TIME,COLUMN_END_TIME};
#SuppressLint("Recycle")
Cursor cursor = sqLiteDatabase.query(TABLE_TIMETABLE,columns,COLUMN_MON + "=" + 1 + " AND " + COLUMN_START_TIME + " = " + start + " AND " + COLUMN_END_TIME + " = " + end,null,null,null,null);
int iTitle = cursor.getColumnIndex(COLUMN_TITLE);
int iSubtitle = cursor.getColumnIndex(COLUMN_SUBTITLE);
StringBuilder result = new StringBuilder();
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){
result.append(cursor.getString(iTitle)).append("\n").append("\n").append(cursor.getString(iSubtitle)).append("\n\n");
}
sqLiteDatabase.close();
return result.toString();
}
//Monday Get TextColor
public String Monday_Text_Color(int start, int end){
sqLiteDatabase = this.getReadableDatabase();
String[] columns = new String[]{COLUMN_TITLE,COLUMN_SUBTITLE, COLUMN_COLOR_TEXT,
COLUMN_MON,COLUMN_START_TIME,COLUMN_END_TIME};
#SuppressLint("Recycle")
Cursor cursor = sqLiteDatabase.query(TABLE_TIMETABLE,columns,COLUMN_MON + "=" + 1 + " AND " + COLUMN_START_TIME + " = " + start + " AND " + COLUMN_END_TIME + " = " + end,null,null,null,null);
int iTextColor = cursor.getColumnIndex(COLUMN_COLOR_TEXT);
StringBuilder result = new StringBuilder();
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){
result.append("#").append(cursor.getString(iTextColor));
}
sqLiteDatabase.close();
return result.toString();
}
//Monday Get Text Background Color
public String Monday_Text_BG_Color(int start, int end){
sqLiteDatabase = this.getReadableDatabase();
String[] columns = new String[]{COLUMN_TITLE,COLUMN_SUBTITLE, COLUMN_COLOR_TEXT_BG,
COLUMN_MON,COLUMN_START_TIME,COLUMN_END_TIME};
#SuppressLint("Recycle")
Cursor cursor = sqLiteDatabase.query(TABLE_TIMETABLE,columns,COLUMN_MON + "=" + 1 + " AND " + COLUMN_START_TIME + " = " + start + " AND " + COLUMN_END_TIME + " = " + end,null,null,null,null);
int iTextColor_BG = cursor.getColumnIndex(COLUMN_COLOR_TEXT_BG);
StringBuilder result = new StringBuilder();
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){
result.append("#").append(cursor.getString(iTextColor_BG));
}
sqLiteDatabase.close();
return result.toString();
}
This is a method that returns true if defined start and end are in the database
public Boolean Monday_Start_End_Time(int start, int end){
sqLiteDatabase = this.getReadableDatabase();
String[] columns = new String[]{COLUMN_MON,COLUMN_START_TIME,COLUMN_END_TIME};
#SuppressLint("Recycle")
Cursor cursor = sqLiteDatabase.query(TABLE_TIMETABLE,columns,COLUMN_MON + "=" + 1 + " AND " + COLUMN_START_TIME + " = " + start + " AND " + COLUMN_END_TIME + " = " + end,null,null,null,null);
boolean result = cursor.moveToFirst();
cursor.close();
sqLiteDatabase.close();
return result;
}
I am currently thinking what makes slow loading fragment is this Boolean check, so that I tried to create a function that returns integer value when the boolean variables are true, but it also slow to load the fragment.
And I am sure the problem is method of public void Monday_Schedule_Settings_0 because when I remove the code, the fragment loads definitely faster.
I would love to hear you advise.

I think your performance problem is that you're closing the database and reopening it with every DB query. You should be closing the cursors, but not closing the database itself. IE you should remove every instance of:
sqliteDatabase.close();

Related

Recursive(Tower Hanoi) UI for android

`[I want to change the UI through the recursive function.
I want to implement UI through recursive function. When you run it, you should see the process of changing the UI according to the value, but if you insert hanoi(), the process does not come out and the value is printed at once. I think I need to change the move, but it's the same if I put sleep() in the move, and I tried Thread, Handler, AsyncTask, but the results are the same. I'd appreciate your help.
public class Disk3 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
class BackgroundThread extends Thread {
boolean running = false;
#Override
public void run() {
running = true;
while (running) {
runOnUiThread(new Runnable() {
#Override
public void run() {
move(3, 1, 3);
}
});
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
btn_move.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
BackgroundThread thread = new BackgroundThread();
thread.start();
}
});
public void move(int N, int start, int to) {
if (start == 1) {
for (int i = 0; i <= 2; i++) {
if (list_tv_diskA[i].getVisibility() == View.VISIBLE) {
startTv = list_tv_diskA[i];
startTv.setVisibility(View.INVISIBLE);
Log.d("TAG", "list_tv_disk A :: " + i);
break;
}
}
}
if (start == 2) {
for (int j = 0; j <= 2; j++) {
if (list_tv_diskB[j].getVisibility() == View.VISIBLE) {
startTv = list_tv_diskB[j];
startTv.setVisibility(View.INVISIBLE);
Log.d("TAG", "list_tv_disk B :: " + j);
break;
}
}
}
if (start == 3) {
for (int k = 0; k <= 2; k++) {
if (list_tv_diskC[k].getVisibility() == View.VISIBLE) {
startTv.setText(toTv.getText());
startTv = list_tv_diskC[k];
startTv.setVisibility(View.INVISIBLE);
Log.d("TAG", "list_tv_disk C :: " + k);
break;
}
}
}
if (to == 1) {
for (int i = 2; i >= 0; i--) {
if (list_tv_diskA[i].getVisibility() == View.INVISIBLE) {
toTv = list_tv_diskA[i];
toTv.setText(startTv.getText());
toTv.setVisibility(View.VISIBLE);
Log.d("TAG", "list_tv_disk B :: " + i);
break;
}
}
}
if (to == 2) {
for (int j = 2; j >= 0; j--) {
if (list_tv_diskB[j].getVisibility() == View.INVISIBLE) {
toTv = list_tv_diskB[j];
toTv.setText(startTv.getText());
toTv.setVisibility(View.VISIBLE);
Log.d("TAG", "list_tv_disk Bda :: " + j);
break;
}
}
}
if (to == 3) {
for (int k = 2; k >= 0; k--) {
if (list_tv_diskC[k].getVisibility() == View.INVISIBLE) {
toTv = list_tv_diskC[k];
toTv.setText(startTv.getText());
toTv.setVisibility(View.VISIBLE);
Log.d("TAG", "list_tv_disk C :: " + k);
break;
}
}
}
public void Hanoi(int N, int start, int to, int via) {
if (N == 1) {
move(1, start, to);
return;
} else {
Hanoi(N - 1, start, via, to);
move(N, start, to);
Hanoi(N - 1, via, to, start);
}
}

I got length of array error in Android but array isn't null

I got length of array error in Android but the array isn't null.
The Codes :
public class xxx extends AppCompatActivity {
TableLayout oyuntl;
TextView oyuntv;
String trtags, tvtags, kelimeurl, sorukelimes, cevapkelimes;
int trtagi, tvtagi, kelimekoy, satiri, sirai, yoni, bittii, kelimei;
ArrayList<String> kelimearray, yerarray;
Random rastgele;
String[] kelimebol;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_oyun);
ArrayList<String> alfabearray = new ArrayList<String>();
alfabearray.add("A");
alfabearray.add("B");
alfabearray.add("C");
//***
alfabearray.add("V");
alfabearray.add("Y");
alfabearray.add("Z");
kelimeurl = "http://xxx.c1.biz/xxx.php";
StringRequest kelimeal = new StringRequest(Request.Method.POST, kelimeurl, new Response.Listener<String> () {
#Override
public void onResponse (String response){
String[] kelimess = response.split("-");
sorukelimes = kelimess[0];
cevapkelimes = kelimess[1];
sorukelimes = sorukelimes + " kelimenin eş anlamlısını bulmacada bulun";
oyuntv.setText(sorukelimes);
kelimei = cevapkelimes.length();
rastgele = new Random();
kelimekoy = 0;
yerarray = new ArrayList<String>();
kelimearray = new ArrayList<String>();
bittii = 0;
Runnable calistir = new Runnable(){
public void run(){
while(kelimekoy == 0) {
satiri = rastgele.nextInt(11 - 1) + 1;
sirai = rastgele.nextInt(11 - 1) + 1;
yoni = rastgele.nextInt(5 - 1) + 1;
kelimebol = cevapkelimes.split("(?!^)");
if (yoni == 1) {
if (satiri >= kelimei) {
kelimekoy = 1;
for(int i = 0; i < kelimei; i++){
kelimearray.add(kelimebol[i]);
yerarray.add(String.valueOf(satiri) + "-" + String.valueOf(sirai));
satiri = satiri - 1;
}
bittii = 1;
}else{
kelimekoy = 0;
}
} else if (yoni == 2) {
if((11 - satiri) >= kelimei) {
kelimekoy = 1;
for(int ii = 0; ii < kelimei; ii++){
kelimearray.add(kelimebol[ii]);
yerarray.add(String.valueOf(satiri) + "-" + String.valueOf(sirai));
satiri = satiri + 1;
}
bittii = 1;
}else{
kelimekoy = 0;
}
} else if (yoni == 3) {
if((11 - sirai) >= kelimei){
kelimekoy = 1;
for(int iii = 0; iii < kelimei; iii++){
kelimearray.add(kelimebol[iii]);
yerarray.add(String.valueOf(satiri) + "-" + String.valueOf(sirai));
sirai = sirai + 1;
}
bittii = 1;
}else{
kelimekoy = 0;
}
} else if(yoni == 4){
if(sirai >= kelimei){
kelimekoy = 1;
for(int iiii = 0; iiii < kelimei; iiii++){
kelimearray.add(kelimebol[iiii]);
yerarray.add(String.valueOf(satiri) + "-" + String.valueOf(sirai));
sirai = sirai - 1;
}
bittii = 1;
}else{
kelimekoy = 0;
}
}
}
}
};
Thread thread = new Thread(calistir);
thread.start();
}
}, new Response.ErrorListener () {
public void onErrorResponse (VolleyError kelimeale){
Toast.makeText(getApplicationContext(), kelimeale.toString(), Toast.LENGTH_LONG);
}
});
kelimeal.setRetryPolicy( new DefaultRetryPolicy(70000, 5, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
RequestQueue kelimequeue = Volley.newRequestQueue(oyun.this);
kelimequeue.add(kelimeal);
TableRow.LayoutParams trp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams tvp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
for(int i = 1; i < 11; i++){
trtagi = i;
trtags = "tr" + String.valueOf(trtagi);
TableRow tabler = new TableRow(oyun.this);
tabler.setTag(trtags);
tabler.setLayoutParams(trp);
for(int ii = 1; ii < 11; ii++){
tvtagi = ii;
tvtags = String.valueOf(trtagi) + String.valueOf(tvtagi);
TextView tabletv = new TextView(oyun.this);
tabletv.setTag(tvtags);
tabletv.setLayoutParams(tvp);
Collections.shuffle(alfabearray);
tabletv.setText(alfabearray.get(0));
for(int yeri = 0; yeri < kelimebol.length; yeri++) {
String[] yerbol = yerarray.get(yeri).split("-");
if ((String.valueOf(trtagi) + "-" + String.valueOf(tvtagi)) == yerarray.get(yeri)) {
tabletv.setText(kelimearray.get(0));
kelimearray.remove(0);
} else {
Collections.shuffle(alfabearray);
tabletv.setText(alfabearray.get(0));
}
}
bittii = 2;
tabler.addView(tabletv);
}
oyuntl.addView(tabler);
}
}
}
I got error in tis line :
for(int yeri = 0; yeri < kelimebol.length; yeri++) {
But I added element in "kelimebol" array in this code :
kelimebol = cevapkelimes.split("(?!^)");
How I can resolve this problem - error?
I wait your help and answers.
Note : I doesn't had a good English. I hope you can understand me. Pardon me.

type missmatch : cannot convert from string to int

public void clickresult(View v){
String temp = "";
int cards[] = {etnumber1.getText().toString(),etnumber2.getText().toString(),
etnumber3.getText().toString(),etnumber4.getText().toString()
,etnumber4.getText().toString()};
int noOfCard = cards.length;
for (int marker = 1; marker < noOfCard; marker++) {
for (int j = 0; j < marker; j++) {
if (cards[marker] < cards[j]) {
int tmp = cards[marker];
cards[marker] = cards[j];
cards[j] = tmp;
}
}
}
for (int i = 0; i < noOfCard; i++) {
temp += cards[i] + " ";
}
Toast.makeText(this, temp, Toast.LENGTH_LONG).show();
}
it still have one error -> type missmatch : cannot convert from string to int
how to fix that
int cards[] = {etnumber1.getText().toString(),etnumber2.getText().toString(),
etnumber3.getText().toString(),etnumber4.getText().toString()
,etnumber4.getText().toString()};
#Override
protected void onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.your_fragment_layout, container, false);
etnumber1 = (EditText)findViewById(R.id.etnumber1);
etnumber2 = (EditText)findViewById(R.id.etnumber2);
etnumber3 = (EditText)findViewById(R.id.etnumber3);
etnumber4 = (EditText)findViewById(R.id.etnumber4);
etnumber5 = (EditText)findViewById(R.id.etnumber5);
}
public void onClickResult(){
String cards[] = {etnumber1.getText().toString,
etnumber2.getText().toString(),
etnumber3.getText().toString(),
etnumber4.getText().toString(),
etnumber5.getText().toString()
};
int noOfCard = cards.length;
Arrays.sort(cards);
StringBuilder builder = new StringBuilder();
for (String s : cards)
builder.append(s).append(" ");
Toast.makeText(context, builder.toString(), Toast.LENGTH_SHORT).show();
}
This will sort your array and display it in a Toast in this format: "2 3 5 6 7" (according to the numbers you provided). If the EditTexts are going to have only numbers, consider using NumberPicker instead.
LENGTH_SHORT can also be LENGTH_LONG.
public class MainActivity extends Activity {
EditText number1,number2,number3,number4,number5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
number1 = (EditText)findViewById(R.id.etnumber1);
number2 = (EditText)findViewById(R.id.etnumber2);
number3 = (EditText)findViewById(R.id.etnumber3);
number4 = (EditText)findViewById(R.id.etnumber4);
number5 = (EditText)findViewById(R.id.etnumber5);
}
public void clickresult(){
int cards[] = {6,7,3,2,5};
int noOfCard = cards.length;
for (int marker = 1; marker < noOfCard; marker++) {
for (int j = 0; j < marker; j++) {
if (cards[marker] < cards[j]) {
int tmp = cards[marker];
cards[marker] = cards[j];
cards[j] = tmp;
}
}
}
for (int i = 0; i < noOfCard; i++) {
System.out.print(cards[i] + " ");
}
System.out.println();
}

Check all the view's background in an if statement in Android?

I have many buttons(81) from the left and right part of my layout. All in all, I have 162 buttons. I put those buttons to Button[] and I handle it properly.
private int[] right_lung = { R.id.btn_right_106, R.id.btn_right_113,
R.id.btn_right_114, R.id.btn_right_115, R.id.btn_right_116, R.id.btn_right_121,
R.id.btn_right_122, R.id.btn_right_123, R.id.btn_right_124, R.id.btn_right_125,
R.id.btn_right_129, R.id.btn_right_130, R.id.btn_right_131, R.id.btn_right_132,
R.id.btn_right_133, R.id.btn_right_134, R.id.btn_right_137, R.id.btn_right_138,
R.id.btn_right_139, R.id.btn_right_140, R.id.btn_right_141, R.id.btn_right_142,
R.id.btn_right_143, R.id.btn_right_145, R.id.btn_right_146, R.id.btn_right_147,
R.id.btn_right_148, R.id.btn_right_149, R.id.btn_right_150, R.id.btn_right_151,
R.id.btn_right_152, R.id.btn_right_153, R.id.btn_right_154, R.id.btn_right_155,
R.id.btn_right_156, R.id.btn_right_157, R.id.btn_right_158, R.id.btn_right_159,
R.id.btn_right_160, R.id.btn_right_161, R.id.btn_right_162, R.id.btn_right_163,
R.id.btn_right_164, R.id.btn_right_165, R.id.btn_right_166, R.id.btn_right_167,
R.id.btn_right_168, R.id.btn_right_169, R.id.btn_right_170, R.id.btn_right_171,
R.id.btn_right_172, R.id.btn_right_173, R.id.btn_right_174, R.id.btn_right_175,
R.id.btn_right_176, R.id.btn_right_177, R.id.btn_right_178, R.id.btn_right_179,
R.id.btn_right_180, R.id.btn_right_181, R.id.btn_right_182, R.id.btn_right_183,
R.id.btn_right_184, R.id.btn_right_185, R.id.btn_right_186, R.id.btn_right_187,
R.id.btn_right_188, R.id.btn_right_189, R.id.btn_right_190, R.id.btn_right_191,
R.id.btn_right_192, R.id.btn_right_194, R.id.btn_right_195, R.id.btn_right_196,
R.id.btn_right_197, R.id.btn_right_198, R.id.btn_right_199, R.id.btn_right_200,
R.id.btn_right_205, R.id.btn_right_206, R.id.btn_right_207 };
private Button[] btn_right = new Button[right_lung.length];
private int[] left_lung = { R.id.btn_left_7, R.id.btn_left_13, R.id.btn_left_14,
R.id.btn_left_15, R.id.btn_left_16, R.id.btn_left_20, R.id.btn_left_21,
R.id.btn_left_22, R.id.btn_left_23, R.id.btn_left_24, R.id.btn_left_27,
R.id.btn_left_28, R.id.btn_left_29, R.id.btn_left_30, R.id.btn_left_31,
R.id.btn_left_32, R.id.btn_left_34, R.id.btn_left_35, R.id.btn_left_36,
R.id.btn_left_37, R.id.btn_left_38, R.id.btn_left_39, R.id.btn_left_40,
R.id.btn_left_41, R.id.btn_left_42, R.id.btn_left_43, R.id.btn_left_44,
R.id.btn_left_45, R.id.btn_left_46, R.id.btn_left_47, R.id.btn_left_48,
R.id.btn_left_49, R.id.btn_left_50, R.id.btn_left_51, R.id.btn_left_52,
R.id.btn_left_53, R.id.btn_left_54, R.id.btn_left_55, R.id.btn_left_56,
R.id.btn_left_57, R.id.btn_left_58, R.id.btn_left_59, R.id.btn_left_60,
R.id.btn_left_61, R.id.btn_left_62, R.id.btn_left_63, R.id.btn_left_64,
R.id.btn_left_65, R.id.btn_left_66, R.id.btn_left_67, R.id.btn_left_68,
R.id.btn_left_69, R.id.btn_left_70, R.id.btn_left_71, R.id.btn_left_72,
R.id.btn_left_73, R.id.btn_left_74, R.id.btn_left_75, R.id.btn_left_76,
R.id.btn_left_77, R.id.btn_left_78, R.id.btn_left_79, R.id.btn_left_80,
R.id.btn_left_81, R.id.btn_left_82, R.id.btn_left_83, R.id.btn_left_84,
R.id.btn_left_85, R.id.btn_left_86, R.id.btn_left_87, R.id.btn_left_88,
R.id.btn_left_89, R.id.btn_left_90, R.id.btn_left_91, R.id.btn_left_92,
R.id.btn_left_93, R.id.btn_left_94, R.id.btn_left_95, R.id.btn_left_98,
R.id.btn_left_99, R.id.btn_left_100, };
private Button[] btn_left = new Button[left_lung.length];
Whenever I click on the button, just like in mine sweeper game, many buttons are randomly opened. And when it is opened, I am changing its background into R.drawable.affected. The goal of the game is to open all the buttons in left and right. My question is this, how can I check if all of the buttons are set in R.drawable.affected? Because after that, I will execute a method that will congratulate the user. Thanks in advance.
EDIT:
for (int i = 0; i < btn_right.length; i++) {
final int a = i;
int counter_total_affected = 0;
btn_right[i].setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (counter == 1) {
right_lung.add(a);
btn_right[a].setBackgroundResource(R.drawable.affected);
counter_total_affected++;
} else if (counter == 2) {
for (int i = 0; i < 2; i++) {
int n = i;
right_lung.add(a + n);
}
try {
for (int i = 0; i < 2; i++) {
int n = i;
btn_right[a + n].setBackgroundResource(R.drawable.affected);
counter_total_affected++;
}
} catch (Exception e) {
e.printStackTrace();
}
} else if (counter == 3) {
right_lung.add(a);
btn_right[a].setBackgroundResource(R.drawable.affected);
counter_total_affected++;
} else if (counter == 4) {
for (int i = 0; i < 25; i++) {
int n = i;
right_lung.add(a + n);
}
try {
for (int i = 0; i < 25; i++) {
int n = i;
btn_right[a + n].setBackgroundResource(R.drawable.affected);
counter_total_affected++;
}
} catch (Exception e) {
e.printStackTrace();
}
} else if (counter == ...) {
//statements...
} else if (counter_total_affected == (btn_left.length + btn_right.length)) {
//CONGRATULATORY METHOD
}
counter++;
}
}
Increment the counter each time you change background of button and compare its value with length of button array. if both are same that means, all button backgrounds are set.
Try this :
for (int i = 0; i < btn_right.length; i++) {
final int b = i;
btn_right[i].setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if btn_right[i].getDrawable().getConstantState().equals
(getResources().getDrawable(R.drawable.affected).getConstantState()))
{
if(counter == btn_right.length){
//Congratulate user...
}
}else{
btn_right[b].setBackgroundResource(R.drawable.affected);
counter++;
}
}
}
U can set Tag:
for (int i = 0; i < btn_right.length; i++) {
final int b = i;
btn_right[i].setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (counter == 1) {
right_affected.add(b);
btn_right[b].setBackgroundResource(R.drawable.affected);
btn_right[b].setTag('1');
} else {
//some stuff here...
}
}
}
after congratulate the user set its Tag to 0.
EDIT:
ArrayList<Integer> arrayofId =new ArrayList<Integer>();
#Override
public void onClick(View v) {
if (counter == 1) {
right_affected.add(b);
btn_right[b].setBackgroundResource(R.drawable.affected);
arrayofId.add(b);//need to convert int to Integer.
} else {
//some stuff here...
}
}
To congratulate:
for(int i=0;i<arrayofId.size();i++)
{
// you can get here id of effected buttons
}

only the original thread that created a view hierarchy can touch its views. android

public class master extends Activity {
ProgressDialog progressDialog;
EditText tahmini_kelime;
EditText girilen_sayi ;
EditText toplam_harf_sayisi ;
Button tamamdir;
TextView jTextArea1;
Vector vector_all,vect_end,vect,recent_search;
BufferedReader read;
String recent_tahmin_kelime;
boolean bayrak,bayrak2 ;
int column_number ;
InputStreamReader inputreader ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.master);
column_number=0;
bayrak=true;
toplam_harf_sayisi=(EditText)findViewById(R.id.toplam_harf);
tahmini_kelime=(EditText)findViewById(R.id.tahmini_kelime);
girilen_sayi=(EditText)findViewById(R.id.sayi_gir);
tamamdir=(Button)findViewById(R.id.tamamdirrrr);
jTextArea1=(TextView)findViewById(R.id.jte);
bayrak2=true;
recent_search = new Vector();
InputStream inputStream = getResources().openRawResource(R.raw.sozluk);
try {
inputreader = new InputStreamReader(inputStream,"utf-8");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
};
read = new BufferedReader(inputreader);
int k = 0;
String result = "";
try {
vector_all = new Vector();
while (read.ready()) {
result = read.readLine();
vector_all.add(result);
jTextArea1.append(result + "\n");
k = k + 1;
}
String size = "" + k;
} catch (IOException ex) {
}
tamamdir.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if( bayrak2 )
{
if(Integer.parseInt(toplam_harf_sayisi.getText().toString())>8 || Integer.parseInt(toplam_harf_sayisi.getText().toString())<=1)
{
toplam_harf_sayisi.setText("");
Dialog dl=new Dialog(master.this);
dl.setTitle("hatalı giriş");
dl.setCanceledOnTouchOutside(true);
dl.show();
return;
}
int findwordlength = Integer.parseInt(toplam_harf_sayisi.getText().toString());
int k = 0;
String result = "";
jTextArea1.setText("");
InputStream inputStream = getResources().openRawResource(R.raw.sozluk);
try {
inputreader = new InputStreamReader(inputStream,"utf-8");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
};
read = new BufferedReader(inputreader);
String resultword = "";
try {
vect = new Vector();
while (read.ready()) {
result = read.readLine();
if (result.length() == findwordlength) {
vect.addElement(result);
resultword = resultword + result + "\n";
k = k + 1;
}
jTextArea1.setText("");
}
jTextArea1.append(resultword + "\n");
RandomKelime(vector_all,0 );
} catch (IOException ex) {
}
toplam_harf_sayisi.setEnabled(false);
girilen_sayi.setEnabled(true);
bayrak2=false;
}
else
{
progressDialog = ProgressDialog.show(master.this, "Bir Düşüneyim :D", "lütfen bekleyiniz...");
Thread thread = new Thread(new Runnable() {
public void run() {
mainGuessWord(column_number);
handler.sendEmptyMessage(0);
}
});
thread.start();
girilen_sayi.setText("");
}
}
});
}
private void mainGuessWord(int look) {
int result_int = 0;
String randomword = "";
int randomword2 = 0;
randomword = tahmini_kelime.getText().toString();
result_int = Integer.parseInt(girilen_sayi.getText().toString());
if (result_int == 0) {
mevcut_degil(vect, randomword);
} else {
elemeAgaci(vect, randomword, result_int);
}
}
public void elemeAgaci(Vector vect, String elem, int length) {
String word = elem.toString();
Vector cmp_vect;
cmp_vect = new Vector();
vect_end = new Vector();
int count = 0;
int countword = 0; // toplam word sayısı
int each_word_total = 0; // her kelimede bulunan harf sayısı
jTextArea1.setText("");
String compare = "";
for (int i = 0; i < vect.size(); i++) {
each_word_total = 0;
compare = "";
for (int j = 0; j < word.length(); j++) {
if(!compare.contains(""+word.charAt(j)))
{
for (int k = 0; k < vect.get(i).toString().length(); k++) {
if (vect.get(i).toString().charAt(k) == word.charAt(j)) {
each_word_total++;
}
}
compare=""+compare+word.charAt(j);
}
}
System.out.println("" + vect.get(i) + " => " + each_word_total);
if (length == each_word_total) {
cmp_vect.add(vect.get(i));
jTextArea1.append(vect.get(i) + "\n");
countword++;
}
}
vect.clear();
for (int l = 0; l < cmp_vect.size(); l++) {
vect.add(cmp_vect.get(l));
}
if (countword == 1) {
Dialog dl=new Dialog(master.this);
dl.setTitle("The Word id : "+jTextArea1.getText().toString());
dl.setCanceledOnTouchOutside(true);
dl.show();
} else {
column_number = column_number + 1;
if(vect.size()<10){
RandomKelime_Table(vect);
}else{
RandomKelime(vector_all, column_number);
}
}
}
public void mevcut_degil(Vector vect, String m) {
char control[];
control = m.toCharArray();
boolean flag = false;
int countword = 0;
Vector detect;
detect = new Vector();
jTextArea1.setText("");
for (int k = 0; k < vect.size(); k++) {
flag = false;
for (int s = 0; s < control.length; s++) {
if (vect.get(k).toString().contains("" + control[s])) {
flag = true;
}
}
if (!flag) {
detect.addElement(vect.get(k));
countword = countword + 1;
}
}
vect.clear();
for (int s = 0; s < detect.size(); s++) {
vect.addElement(detect.get(s));
}
for (int a = 0; a < countword; a++) {
jTextArea1.append(vect.get(a).toString() + "\n");
}
if (countword == 1) {
Dialog dl=new Dialog(master.this);
dl.setTitle("The Word id : "+jTextArea1.getText().toString());
dl.setCanceledOnTouchOutside(true);
dl.show();
}
else {
column_number = column_number + 1;
RandomKelime(vect, column_number);
}
}
public void RandomKelime(Vector vector, int k)
{
String sesli[]={"a","e","ı","i","o","ö","u","ü"};
Random a = new Random();
if (k == 0) {
String passedword = "";
passedword = vector_all.get((int) (Math.random() * vector_all.size())).toString();
while (passedword.length() < 8) {
passedword = vector_all.get((int) (Math.random() * vector_all.size())).toString();
}
tahmini_kelime.setText(passedword);
recent_tahmin_kelime=passedword;
// jTable1.setValueAt(vector_all.get((int) (Math.random() * vector_all.size())), k, 0);
} else {
recent_search.addElement(recent_tahmin_kelime );
int say = 0;
String design = "";
String guess_words = "";
String as="";
int f=0;
int count=0;
int calculate_all=0;
for (int u = 0; u < recent_search.size(); u++) {
design = recent_search.get(u).toString();
bayrak = false;
as="";
count=0;
for(int s=0;s<sesli.length;s++)
{
if(design.contains(""+sesli[s]) && count==0){
as+=""+sesli[s];
count++;
}
}
guess_words = vector_all.get((int) a.nextInt(vector_all.size())).toString();
while (guess_words.length() < 8) {
guess_words = vector_all.get((int) (Math.random() * vector_all.size())).toString();
}
while (say < design.length()) {
calculate_all=0;
while (guess_words.contains("" + as) && !design.equals(guess_words)) {
say = 0;
calculate_all++;
guess_words = vector_all.get( a.nextInt(vector_all.size())).toString();
while (guess_words.length() < 8) {
guess_words = vector_all.get((int) (Math.random() * vector_all.size())).toString();
}
f=f+1;
System.out.println("Tahmın: " + guess_words + " => " + design);
if(calculate_all>vect.size())
{
break;
}
}
say++;
System.out.println("coutn: " + say);
}
}
if (true) {
tahmini_kelime.setText(guess_words);
}
}
}
public void RandomKelime_Table(Vector vector ) {
String passedword = "";
Random a = new Random();
try {
passedword = vect.get(a.nextInt(vect.size())).toString();
} catch (Exception e) {
Dialog dl=new Dialog(master.this);
dl.setTitle("Hatalı Giriş.Yeniden Başlayın.");
dl.setCanceledOnTouchOutside(true);
dl.show();
yeniden_basla();
}
tahmini_kelime.setText(passedword );
}
public void yeniden_basla()
{
bayrak2=true;
girilen_sayi.setEnabled(false);
toplam_harf_sayisi.setEnabled(true);
toplam_harf_sayisi.setText("");
vect.clear();
vector_all.clear();
vect_end.clear();
recent_search.clear();
jTextArea1.setText("");
recent_tahmin_kelime="";
column_number=0;
bayrak=true;
InputStream inputStream = getResources().openRawResource(R.raw.sozluk);
try {
inputreader = new InputStreamReader(inputStream,"utf-8");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
};
read = new BufferedReader(inputreader);
int k = 0;
String result = "";
try {
vector_all = new Vector();
while (read.ready()) {
result = read.readLine();
vector_all.add(result);
jTextArea1.append(result + "\n");
k = k + 1;
}
String size = "" + k;
} catch (IOException ex) {
}
}
private Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
progressDialog.dismiss();
}
};
}
this all of my code.
You don't show where you create your handler (onCreate ? onStart ? somewhere else ?). Is it started from the main thread ? If so, you need to be provide a more complete stack trace so we can understand.
If you're starting it from another thread then that's the issue because it's attempting to change progressDialog and that must be done from the main thread.
PS: if you used an AsyncTask you wouldn't have to scratch your head around this as it's designed to do exactly what you want and be thread safe.
Post comment : use an AsyncThread : add the progress bar in onPreExecute(), change run() to doInBackground() and move the dismiss() to onPostExecute(

Categories

Resources