Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy main thread can't update UI elements inside a horizontallistview?
    primarykey
    data
    text
    <p>Last few days I'm trying to update some UI Views inside a custom <a href="https://github.com/TomIsYourName/Android-HorizontalListView" rel="nofollow noreferrer">HorizontalListView</a>. I've been trying countless way like using Async Task, posting message via handler etc. I didn't figure out to update them. As a result, I tried to build a queue system to update them one by one. Here is my code and it's still not updating them.</p> <pre><code>import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; import java.util.Queue; import roboguice.RoboGuice; import CustomChannel; import EGPInfo; import android.content.Context; import android.os.Handler; import android.util.Log; import android.widget.TextView; public class EPGLoader { static private EPGLoader _instance; public static EPGLoader getInstance(Context context) { if (_instance == null) { _instance = new EPGLoader(); } mContext = context; return _instance; } private HashMap&lt;String, WeakReference&lt;EPGChannel&gt;&gt; _cachedChannels; private Queue&lt;EPGChannel&gt; _queue; private EPGThread _thread; private static Context mContext; private boolean _isBusy; public EPGLoader() { _cachedChannels = new HashMap&lt;String, WeakReference&lt;EPGChannel&gt;&gt;(); _queue = new LinkedList&lt;EPGChannel&gt;(); _isBusy = false; } public void load(ArrayList&lt;TextView&gt; textViews, CustomChannel channel) { Iterator&lt;EPGChannel&gt; it = _queue.iterator(); EPGChannel epgChannel = new EPGChannel(); epgChannel.setChannel(channel); epgChannel.setTextViews(textViews); while (it.hasNext()) { if (it.next().equals(epgChannel)) { it.remove(); break; } } _queue.add(epgChannel); loadNext(); } private void loadNext() { Iterator&lt;EPGChannel&gt; it = _queue.iterator(); if (!_isBusy &amp;&amp; it.hasNext()) { _isBusy = true; EPGChannel epgChannel = it.next(); it.remove(); EPGChannel epgCache = get(epgChannel.getChannel().getChannelId()); if (epgCache != null) { Log.i("A", "Cache"); epgChannel.textViews.get(0).setText("1"); epgChannel.textViews.get(1).setText("2"); epgChannel.textViews.get(2).setText("3"); epgChannel.textViews.get(3).setText("4"); _isBusy = false; loadNext(); } else { _thread = new EPGThread(epgChannel); _thread.start(); Log.i("A", "Live"); } } } private EPGChannel get(String channelId) { if (_cachedChannels.containsKey(channelId)) { return _cachedChannels.get(channelId).get(); } else { return null; } } private class EPGThread extends Thread { private EPGChannel EPGChannel; final Handler threadHandler = new Handler(); final Runnable threadCallBack = new Runnable() { @Override public void run() { onLoad(); } }; public EPGThread(EPGChannel epgChannel) { this.EPGChannel = epgChannel; } private void onLoad() { if (_thread != null) { EPGChannel epgChannel = _thread.EPGChannel; // epgChannel.getTextViews().get(1).setText(epgChannel.getChannel().getName()); epgChannel.getTextViews().get(0).setText("1"); epgChannel.getTextViews().get(1).setText("2"); epgChannel.getTextViews().get(2).setText("3"); epgChannel.getTextViews().get(3).setText("4"); } _thread = null; _isBusy = false; loadNext(); } @Override public void run() { try { _isBusy = true; ChannelManager channelManager = RoboGuice.getInjector(mContext).getInstance(ChannelManager.class); ArrayList&lt;EGPInfo&gt; epgInfo = channelManager.getChannelEPG(EPGChannel.getChannel().getChannelId()); if (epgInfo.size() == 2) { EPGChannel.getChannel().updateEPGInformations(epgInfo.get(0), epgInfo.get(1)); } if (!_cachedChannels.containsKey(EPGChannel.getChannel().getChannelId())) _cachedChannels.put(EPGChannel.getChannel().getChannelId(), new WeakReference&lt;EPGLoader.EPGChannel&gt;(EPGChannel)); } catch (Exception e) { e.printStackTrace(); } finally { threadHandler.post(threadCallBack); } } } private class EPGChannel { private ArrayList&lt;TextView&gt; textViews; private CustomChannel channel; public ArrayList&lt;TextView&gt; getTextViews() { return textViews; } public void setTextViews(ArrayList&lt;TextView&gt; textViews) { this.textViews = textViews; } public CustomChannel getChannel() { return channel; } public void setChannel(CustomChannel channel) { this.channel = channel; } } } </code></pre> <p>I call it from main thread when getView event of ArrayAdapter fires. </p> <pre><code>@Override public View getView(int position, View retval, ViewGroup parent) { if (retval == null) { retval = LayoutInflater.from(parent.getContext()).inflate(R.layout.channel_item, null); } final CustomChannel currentChannel = getItem(position); final TextView nowTitle = (TextView) retval.findViewById(R.id.channel_item_current_show_title); final TextView nowPlayTime = (TextView) retval.findViewById(R.id.channel_item_current_show_play_time); final TextView nextTitle = (TextView) retval.findViewById(R.id.channel_item_next_show_title); final TextView nextPlayTime = (TextView) retval.findViewById(R.id.channel_item_next_show_play_time); ArrayList&lt;TextView&gt; textViews = new ArrayList&lt;TextView&gt;(); textViews.add(nowTitle); textViews.add(nowPlayTime); textViews.add(nextTitle); textViews.add(nextPlayTime); EPGLoader.getInstance(mContext).load(textViews, currentChannel); return retval; } </code></pre> <p>So, what is wrong with my codes? Thanks in advance.</p> <p>Edit: I replace the HorizontalListView with normal Android's ListView and it works but I have to use HorizontalListView in the project. Any suggesstions? </p> <p>Edit 2: I reward to back (HorizontalListView) and tried to set Background color of some UI controllers, guess what? It updated. It doesn't update text property!!! Why????<img src="https://i.stack.imgur.com/lKSda.png" alt="enter image description here"></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.
    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