Note that there are some explanatory texts on larger screens.

plurals
  1. POMain Ui Thread does not update during method procedure
    primarykey
    data
    text
    <p>i am new to android sdk so pls don't be to harsh on me. atm i am on my first android app .. its almost done but i am facing a little problem right now. </p> <p>the app is a small single player cardplay game with 4 players - 3 computer enemys. </p> <p>the problem is that after i do a move .. the computer enemys are doing their moves based on ki logic .. and that is working just fine .. but only the last move of lets say player 4 is shown after the end of method. even if i delay the method procedure after each ki move .. it doesn't update the ImageViews to show the current ki move ...</p> <p>im really pretty lost .. as i can't find any logic mistake .. i appreciate any kind of help .. thx for reading - not native English speaker sry for that :-)</p> <p>Ok here goes some code .. cardDeck holds the current move</p> <pre><code>List&lt;Card&gt; cardDeck = new ArrayList&lt;Card&gt;(); List&lt;Card&gt; move = new ArrayList&lt;Card&gt;(); KI player2 = new KI(); but.setText("GO"); but.setOnClickListener(new OnClickListener() { @Override public void onClick(View vw) { if (!move.isEmpty()) { if (Rules.checkIsPlayableHand(move)) { sort(); setClickAble(); ki4Moves(vw); } </code></pre> <p>sort() looks kind of this:</p> <pre><code>public void sort(){ cardDeck.clear(); cardDeck.addAll(move); if (cardDeck.size() == 1) { dkView1 = (ImageView) findViewById(R.id.dkView1); Card card = cardDeck.get(0); dkView1.setImageBitmap(card.getBitmap()); } else { dkView1 = (ImageView) findViewById(R.id.dkView1); dkView1.setImageBitmap(blank); } } </code></pre> <p>and ki4Moves() looks kind of this:</p> <pre><code>public void ki4Moves(View vw){ player2.think(); List&lt;Card&gt; kiMove = new ArrayList&lt;Card&gt;(); kiMove = player2.getPossibleMove(cardDeck); if (!kiMove.isEmpty()) { trash.addAll(kiMove); move.clear(); move.addAll(kiMove); sort(); } // same for the other both ki s } </code></pre> <p>EDIT:</p> <p>well after some research .. reading android docs i havent found a real solution for my problem. </p> <p>WHAT I WANTED TO DO IS : FORCE THE MAIN UI THREAD TO UPDATE AFTER SORT() .. LIKE THIS:</p> <pre><code> @Override public void onClick(View vw) { if (!move.isEmpty()) { if (Rules.checkIsPlayableHand(move)) { sort(); // FORCE MAIN UI THREAD TO UPDATE .. just one line // let SystemClock.sleep(2000); setClickAble(); ki4Moves(vw); } </code></pre> <p>Now .. that i cant force the main thread to update or access it outside of the main thread .. ANDROID offers me Threading, Painless Threading, Async Task .. wow thanks for THAT!</p> <ol> <li>the procedure is not heavy loaded ... no database operations etc.</li> <li>if i use thread.join ... main ui thread is still locked </li> </ol> <p>So first of all .. i dont see any logical reason to use threads .. BUT I KINDA HAVE TO ? so okay here i go .. lets say i change my code like this:</p> <pre><code>Handler h = new Handler(); @Override public void onClick(View vw) { if (!move.isEmpty()) { if (Rules.checkIsPlayableHand(move)) { Thread t = new Thread(new Runnable){ void run(){ h.post(new Runnable(){ // do some logic here }); h.post(new Runnable(){ if (cardDeck.size() == 1) { dkView1 = (ImageView) findViewById(R.id.dkView1); Card card = cardDeck.get(0); dkView1.setImageBitmap(card.getBitmap()); } else { dkView1 = (ImageView) findViewById(R.id.dkView1); dkView1.setImageBitmap(blank); } }); Thread.sleep(2000); } }); t.start(); setClickAble(); ki4Moves(vw); } </code></pre> <p>So ye this is working fine .. BUT oh .. there is this method "ki4Moves(vw)" ... i cant let it t.join() .. cause as i said the main ui thread is gonna be locked. so i need to WRAP the 200 lines out of ki4Moves into threads aswell .. </p> <p>and ye this is working .. after every move .. the main ui is updated. </p> <p>BUUUT ki4Moves is one of the easiest methods .. what about if i pass the move .. and the cpu players go into a loop to finish it.. i cant hierachly access the main ui in this loop .. so there i am again.</p> <p>I dont get it .. it took me like 2 weeks to implement the logic .. as the rules of this game are pretty special .. and now that it is running as intended, ki is smart .. it fails cause of the UI LOCKS UP .. shame on this .. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload