ここ数日、カスタムHorizo ntalListView 内のいくつかの UI ビューを更新しようとしています。私は、非同期タスクの使用、ハンドラーを介したメッセージの投稿など、数え切れないほどの方法を試してきました。それらを更新する方法がわかりませんでした。その結果、それらを 1 つずつ更新するキュー システムを構築しようとしました。ここに私のコードがありますが、まだ更新されていません。
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<String, WeakReference<EPGChannel>> _cachedChannels;
private Queue<EPGChannel> _queue;
private EPGThread _thread;
private static Context mContext;
private boolean _isBusy;
public EPGLoader() {
_cachedChannels = new HashMap<String, WeakReference<EPGChannel>>();
_queue = new LinkedList<EPGChannel>();
_isBusy = false;
}
public void load(ArrayList<TextView> textViews, CustomChannel channel) {
Iterator<EPGChannel> 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<EPGChannel> it = _queue.iterator();
if (!_isBusy && 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<EGPInfo> 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<EPGLoader.EPGChannel>(EPGChannel));
} catch (Exception e) {
e.printStackTrace();
} finally {
threadHandler.post(threadCallBack);
}
}
}
private class EPGChannel {
private ArrayList<TextView> textViews;
private CustomChannel channel;
public ArrayList<TextView> getTextViews() {
return textViews;
}
public void setTextViews(ArrayList<TextView> textViews) {
this.textViews = textViews;
}
public CustomChannel getChannel() {
return channel;
}
public void setChannel(CustomChannel channel) {
this.channel = channel;
}
}
}
ArrayAdapter の getView イベントが発生したときに、メイン スレッドから呼び出します。
@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<TextView> textViews = new ArrayList<TextView>();
textViews.add(nowTitle);
textViews.add(nowPlayTime);
textViews.add(nextTitle);
textViews.add(nextPlayTime);
EPGLoader.getInstance(mContext).load(textViews, currentChannel);
return retval;
}
では、私のコードの何が問題なのですか? 前もって感謝します。
編集: HorizontalListView を通常の Android の ListView に置き換えて動作しますが、プロジェクトで HorizontalListView を使用する必要があります。何か提案はありますか?
編集 2: 戻る (HorizontalListView) に報酬を与え、いくつかの UI コントローラーの背景色を設定しようとしましたが、何だと思いますか? 更新しました。テキストプロパティを更新しません!!! どうして????