runOnUiThread(new Runnable(){})未定义类型new Thread(){}

时间:2013-03-01 07:02:51

标签: android

看看这部分:

runOnUiThread(new Runnable() {

我遇到了问题:

The method runOnUiThread(new Runnable(){}) is undefined for the type new Thread(){}

所以我理解runOnUiThread必须来自一个活动。但我不确定这将如何实施?它是在Preference / settings视图中的对话框

这是ChannelsDialogPreference形成的地方: 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);
        }
    }


}

3 个答案:

答案 0 :(得分:48)

像这样写 -

((Activity) context).runOnUiThread(new Runnable() {
    @Override
    public void run() {
        //Code for the UiThread
    }
});

For Fragment:

getActivity().runOnUiThread(new Runnable() {
    @Override
    public void run() {
        //Code for the UiThread
    }
});

这可能会对你有帮助。

答案 1 :(得分:3)

如果您使用的是Fragments,请添加getActivity()

这里替换:
runOnUiThread(new Runnable() {
getActivity().runOnUiThread(new Runnable() {

答案 2 :(得分:0)

您必须setOnClickListener未在代码中设置

Button confirm = (Button) dialog.findViewById(R.id.btn_confirm);
confirm.setOnClickListener(new OnClickListener() 
{

    @Override
    public void onClick(View v)
    {
        your call to method...
    }
});