Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6739491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:29:44+00:00 2026-05-26T11:29:44+00:00

I have an Activity and a Thread. The thread handles the data (later that

  • 0

I have an Activity and a Thread. The thread handles the data (later that data will be grabbed from Internet activity, for now, it just automatically adds a new row each 10 seconds). The thing is, after a new row being add, I can’t touch the items anymore, to regain focus, I must press the up or down arrow on my hardware keyboard, or the menu button.
Of course I first thought to re-set the .setFocusableOnTouchMode to true, but this didn’t seem to solve my problem. Or at least, I’m not setting it on the right place. Anyway this is my code:

The Activity:

    $
    package com.ejemplolisbox;



import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class EL extends Activity {
    /**
     * @see android.app.Activity#onCreate(Bundle)
     */





    public Refresh actualizar;
        // The thread
    public static ListView g;
    public static EfficientAdapter instance ;
        // The adapter (is a custom made adapter, I didn't do it myself, just grabbed it from the Internet)

        public static String[] abbreviations = {  "Item0",
                "Item1", "Item2"};







            /** Called when the activity is first created. */

            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                g = (ListView) findViewById(R.id.lv_country);
                g.setFocusable(true);
                g.setFocusableInTouchMode(true);
                instance = new EfficientAdapter(this);
                                // The adapter is now set to this instance
                g.setAdapter(instance);
                actualizar = new Refresh();
                actualizar.start();
                                //I start the thread

                g.setOnFocusChangeListener(new OnFocusChangeListener() {

                    public void onFocusChange(View arg0, boolean arg1) {
                        // TODO Auto-generated method stub
                        // Code should be here (??)
                    }

                });
                g.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                    public void onItemClick(AdapterView a, View v, int position,
                long id) {

                //use position to get clicked position
                //your code goes here

                }
                });
            }
            public static class EfficientAdapter extends BaseAdapter {
                private LayoutInflater mInflater;

                public EfficientAdapter(Context context) {
                    mInflater = LayoutInflater.from(context);


                }

                public int getCount() {
                    return abbreviations.length;
                }

                public Object getItem(int position) {
                    return position;
                }

                public long getItemId(int position) {
                    return position;
                }

                public View getView(int position, View convertView, ViewGroup parent) {

                    ViewHolder holder;
                    if (convertView == null) {
                        convertView = mInflater.inflate(R.layout.rowlayout, null);
                        holder = new ViewHolder();
                        holder.text1 = (TextView) convertView
                                .findViewById(R.id.TextView01);


                        convertView.setTag(holder);
                    } else {
                        holder = (ViewHolder) convertView.getTag();
                    }

                    holder.text1.setText(abbreviations[position]);



                    return convertView;

                }

                static class ViewHolder {
                    TextView text1;

                }
            }
        }

OK now the Thread.

 package com.ejemplolisbox;
     public class Refresh extends Thread{
     public void run(){
    while(true){
    String[] ex=EL.abbreviations;
    String[] ne=new String[ex.length+1];
    for(int i=0;i<ex.length;i++){
        ne[i]=ex[i];
    }
    ne[ex.length]="newItem"+ex.length;
    EL.abbreviations=ne;
    try{
    EL.instance.notifyDataSetChanged();
    EL.g.setFocusableInTouchMode(true);


    }
    catch(Exception e){
        int i = 0;
        EL.g.setFocusableInTouchMode(true);
    }
    EL.g.setFocusableInTouchMode(true);
    try {
        Thread.sleep(10000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

        }
        }
        }
        }

Well, thanks on advance, any solution will be appretiated

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T11:29:44+00:00Added an answer on May 26, 2026 at 11:29 am

    You can’t directly modify UI elements from a thread that isn’t the UI thread. If you want to modify a UI element from a different thread, you have to call the function runOnUiThread. That means you’ll have to pass a refernce to the activity to the Refresh class in it’s constructor so you can do the following call on your thread:

    activity.runOnUiThread(new Runnable(){
      public void run(){
        EL.g.setFocusableInTouchMode(true);
      }
    });
    

    That said, if I were you I’d scrap this and just use an AsyncTaskLoader instead. It’s much easier and the class was specifically created for asynchronously loading up elements in things like a listview. The way you have the code written right now is not going to work very well and is prone to all kinds of error. In android, a general rule of thumb is to not use the raw Thread class unless you abosultely have to. Use either the AsyncTaskLoader, AsyncTask, or IntentService.

    Final note, if you’re developing for API levels less than 10, you can still access the AsyncTaskLoader class via the android Support Package.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Thread that downloads data from internet public class Bp implements Runnable
I have a thread reading data from a bluetooth stream that sends the data
That's the cuestion , i have: Activity A Activity B From A i want
I have a thread using handler and messages to send data to the activity.
I have an activity that has a TabHost containing a set of TabSpecs each
I have an activity that contains several user editable items (an EditText field, RatingBar,
I have an activity that launches another activity with startActivityForResult method. I would like
I have a semi-complicated problem and hoping that someone here will be able to
I have an Android activity which in turn starts a thread. In the thread
I have a class that fetches data in response to button presses in the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.