本文介绍了runOnUiThread(新的Runnable(){})是不确定的该类型新的Thread(){}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
纵观部分:
runOnUiThread(新的Runnable(){
我有问题:
的方法runOnUiThread(新的Runnable(){})是不确定的该类型新的Thread(){}
所以,我明白,runOnUiThread必须来自一个活动。但林不知道这将如何实施?它从一个preference对话框/设置查看
这其中ChannelsDialog preference自带形式: pref_channels.xml
< preferenceScreen的xmlns:机器人=http://schemas.android.com/apk/res/android>
< com.example.tvrplayer.ChannelsDialog preference
机器人:标题=@字符串/ pref_channels_dialog_title
机器人:dialogMessage =@字符串/ pref_channels_dialog_message
机器人:negativeButtonText =测试/>
< / preferenceScreen>
ChannelsDialog preference:
包com.example.tvrplayer;
进口的java.util.ArrayList;
进口的java.util.HashMap;
进口的java.util.List;
进口org.json.JSONArray;
进口org.json.JSONException;
进口org.json.JSONObject;
进口android.app.Activity;
进口android.content.Context;
进口android.content.DialogInterface;
进口的Android preference.Dialog preference。
进口android.util.AttributeSet;
进口android.util.Log;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.widget.AdapterView;
进口android.widget.ListView;
公共类ChannelsDialog preference扩展对话框preference {
公共静态上下文CTX;
//活动的活动;
公共ChannelsDialog preference(上下文的背景下,ATTRS的AttributeSet){
超(背景下,ATTRS);
this.ctx =背景;
}
@覆盖
受保护的视图onCreateDialogView(){
//设置加载微调
//开始寻找渠道
//完成后,节目频道
LayoutInflater充气=((SettingsActivity)CTX).getLayoutInflater();
最后查看VW = inflater.inflate(R.layout.channel_content_view,NULL);
//在一个新的线程执行此操作
线程线程=新的Thread()
{
@覆盖
公共无效的run(){
尝试 {
最后JSONArray渠道;
字符串username = NULL;
字符串的linkID = NULL;
最后弦乐apiurl =http://192.168.2.136:8080;
最后弦乐的channelID = NULL;
最终的数据库处理器DB =新的数据库处理器(CTX);
清单<使用者>用户= db.getAllUsers();
对于(用户CN:用户){
的linkID = cn.getLinkID();
用户名= cn.getUserName();
}
db.close();
最后的ArrayList< HashMap的<字符串,字符串> > channelList =新的ArrayList< HashMap的<字符串,字符串> > ();
尝试 {
如果(的channelID!= NULL){
通道= Json.getJson(apiurl +/休息/渠道/+的linkID +/+用户名+/+的channelID,GET);
Log.i(通道,channels.toString());
的for(int i = 0; I< channels.length();我++){
的JSONObject json_data = channels.getJSONObject(ⅰ);
字符串名称= json_data.getString(姓名);
字符串的channelID = json_data.getString(的channelID);
HashMap的<字符串,字符串> channelObject =新的HashMap<字符串,字符串>();
//如果(json_data.getString(ParentPath)==){
channelObject.put(ID,的channelID);
channelObject.put(姓名,名);
channelList.add(channelObject);
//}
}
} 其他 {
通道= Json.getJson(apiurl +/休息/渠道/+的linkID +/+用户名,GET);
Log.i(通道,channels.toString());
的for(int i = 0; I< channels.length();我++){
的JSONObject json_data = channels.getJSONObject(ⅰ);
字符串名称= json_data.getString(姓名);
字符串的channelID = json_data.getString(的channelID);
HashMap的<字符串,字符串> channelObject =新的HashMap<字符串,字符串>();
Log.i(CHANNEL LEN,+ json_data.getString(PARENTID)长度());
如果(json_data.getString(PARENTID)长度()< 32){
channelObject.put(ID,的channelID);
channelObject.put(姓名,名);
channelList.add(channelObject);
}
}
}
runOnUiThread(新的Runnable(){
公共无效的run(){
HiddenChannelsListAdapter适配器=新HiddenChannelsListAdapter(CTX,channelList);
ListView的LV =(ListView控件)vw.findViewById(R.id.list);
lv.setAdapter(适配器);
lv.isClickable();
lv.setOnItemClickListener(新android.widget.AdapterView.OnItemClickListener()
{
@覆盖
公共无效onItemClick(适配器视图<>为arg0,查看ARG1,INT ARG2,长ARG3)
{
Log.i(点击LV项目,YEA);
}
});
}
});
}赶上(JSONException E){
// TODO自动生成的catch块
e.printStackTrace();
}
}赶上(例外五){
e.printStackTrace();
}
}
};
thread.start();
//新的ChannelHandler()执行(apiurl,用户名,的linkID,大众,CTX,的channelID)。
回归大众;
}
@覆盖
保护无效onDialogClosed(布尔positiveResult){
super.onDialogClosed(positiveResult);
persistBoolean(positiveResult);
}
@覆盖
公共无效的onClick(DialogInterface对话,诠释其)
{
super.onClick(对话框,其中);
//
Log.v(其中,Integer.toString(它));
//
如果(这== -1){//清除所有
//新的ChannelHandler()执行(apiurl,用户名,的linkID,大众,CTX,的channelID)。
}
}
}
解决方案
这样写 -
((活动)范围内).runOnUiThread(新的Runnable(){
@覆盖
公共无效的run(){
// code为UiThread
}
});
这可能会帮助你。
Looking at the part:
runOnUiThread(new Runnable() {
I have the problem:
The method runOnUiThread(new Runnable(){}) is undefined for the type new Thread(){}
So I understand that runOnUiThread must come from an activity. But Im not sure how this will be implemented? Its a dialog from within a Preference / settings view
This where the ChannelsDialogPreference comes form: pref_channels.xml
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<com.example.tvrplayer.ChannelsDialogPreference
android:title="@string/pref_channels_dialog_title"
android:dialogMessage="@string/pref_channels_dialog_message"
android:negativeButtonText="test"/>
</PreferenceScreen>
ChannelsDialogPreference:
package com.example.tvrplayer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
public class ChannelsDialogPreference extends DialogPreference {
public static Context ctx;
// Activity activity;
public ChannelsDialogPreference(Context context, AttributeSet attrs) {
super(context, attrs);
this.ctx = context;
}
@Override
protected View onCreateDialogView() {
// Set loading spinner
// Start looking for channels
// When complete, show channels
LayoutInflater inflater = ((SettingsActivity) ctx).getLayoutInflater();
final View vw = inflater.inflate(R.layout.channel_content_view, null);
// Do this in a new Thread
Thread thread = new Thread()
{
@Override
public void run() {
try {
final JSONArray channels;
String username = null;
String linkid = null;
final String apiurl = "http://192.168.2.136:8080";
final String channelID = null;
final DatabaseHandler db = new DatabaseHandler(ctx);
List<User> users = db.getAllUsers();
for (User cn : users) {
linkid = cn.getLinkID();
username = cn.getUserName();
}
db.close();
final ArrayList< HashMap < String, String > > channelList = new ArrayList < HashMap < String, String > > ();
try {
if ( channelID != null ) {
channels = Json.getJson(apiurl + "/rest/channel/"+ linkid +"/"+ username + "/" + channelID, "GET");
Log.i("CHANNELS", channels.toString());
for (int i=0; i < channels.length(); i++) {
JSONObject json_data = channels.getJSONObject(i);
String name = json_data.getString("Name");
String channelid = json_data.getString("ChannelID");
HashMap<String, String> channelObject = new HashMap<String, String>();
// if ( json_data.getString("ParentPath") == "" ) {
channelObject.put("id", channelid);
channelObject.put("name", name);
channelList.add(channelObject);
// }
}
} else {
channels = Json.getJson(apiurl + "/rest/channel/"+ linkid +"/"+ username, "GET");
Log.i("CHANNELS", channels.toString());
for (int i=0; i < channels.length(); i++) {
JSONObject json_data = channels.getJSONObject(i);
String name = json_data.getString("Name");
String channelid = json_data.getString("ChannelID");
HashMap<String, String> channelObject = new HashMap<String, String>();
Log.i("CHANNEL LEN", ""+ json_data.getString("ParentID").length());
if ( json_data.getString("ParentID").length() < 32 ) {
channelObject.put("id", channelid);
channelObject.put("name", name);
channelList.add(channelObject);
}
}
}
runOnUiThread(new Runnable() {
public void run() {
HiddenChannelsListAdapter adapter = new HiddenChannelsListAdapter(ctx, channelList);
ListView lv = (ListView) vw.findViewById(R.id.list);
lv.setAdapter(adapter);
lv.isClickable();
lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
Log.i("CLICKED ON LV ITEM", "YEA");
}
});
}
});
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
thread.start();
// new ChannelHandler().execute(apiurl, username, linkid, vw, ctx, channelID);
return vw;
}
@Override
protected void onDialogClosed(boolean positiveResult) {
super.onDialogClosed(positiveResult);
persistBoolean(positiveResult);
}
@Override
public void onClick (DialogInterface dialog, int which)
{
super.onClick(dialog, which);
//
Log.v("which", Integer.toString(which));
//
if(which == -1) { //Clear all
// new ChannelHandler().execute(apiurl, username, linkid, vw, ctx, channelID);
}
}
}
解决方案
Write like this-
((Activity) context).runOnUiThread(new Runnable() {
@Override
public void run() {
//Code for the UiThread
}
});
This may help you.
这篇关于runOnUiThread(新的Runnable(){})是不确定的该类型新的Thread(){}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!