Android can';不能在线程中定义适配器

Android can';不能在线程中定义适配器,android,multithreading,simpleadapter,Android,Multithreading,Simpleadapter,我没有复制所有代码,因为它太长了,但为了简洁: 我有一个功能(recup_list_internet),其中有一个线程,它从internet检索数据(XML),对其进行解码,并将每个“节点”分配给适配器中的一个元素 在线程外执行解码时,一切正常。 因此,我将其修改为在线程内部使用,在其中创建一个void run()函数,显示我的progressDialog,解码,数据被很好地检索,并很好地分配给我的映射(=新HashMap();) 这就是问题所在 private void recup_list_

我没有复制所有代码,因为它太长了,但为了简洁:

我有一个功能(recup_list_internet),其中有一个线程,它从internet检索数据(XML),对其进行解码,并将每个“节点”分配给适配器中的一个元素

在线程外执行解码时,一切正常。 因此,我将其修改为在线程内部使用,在其中创建一个void run()函数,显示我的progressDialog,解码,数据被很好地检索,并很好地分配给我的映射(=新HashMap();) 这就是问题所在

private void recup_list_internet()
{
final ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();
final Context mContext=this.getBaseContext();


Thread t = new Thread()
{
    public void run()
    {/* the code here works fine, not displaying it to be more concise*/

    progressDialog.dismiss(); //works fine
    SimpleAdapter mSchedule = new SimpleAdapter (mContext, listItem, R.layout.affiche_boutique,new String[] {"img", "titre", "description","Prix","uniqueID"}, new int[] {R.id.img,R.id.titre, R.id.description,R.id.prix,R.id.uniqueID}); //works fine
    maListViewPerso.setAdapter(mSchedule); //doesn't work
    }
};
t.start();
}
似乎我在我的线程中无法“访问”maListViewPero。。。 (maListViewPerso之前在我的onCreate代码中定义:

public class DisplayInternet  extends Activity{
private ListView maListViewPerso;
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ceinture_lv);
    maListViewPerso = (ListView) findViewById(R.id.listviewperso);
    recup_list_internet();
}   
那么,我可以把这条线放在哪里,让它发挥作用呢? “maListViewPerso.setAdapter(msSchedule);”


因为我已经尝试在我的线程外(最终)声明mSchedule,但在我的线程内,我无法访问它(因此,我无法在线程内的“t.start()”行后使用它,请使用:

这基本上是说“嘿,UI线程,为我执行这个东西”-并将所有必须在UI线程中执行的代码放入可运行状态-当您有一个线程从网络检索数据(不能在UI线程上运行)但必须在UI上发布结果时,这尤其有用(必须从UI线程执行)

例如:

view.post(new Runnable(){ 
    public void run(){
        //put all the code you want to be execute on the UI thread here
    }
});

在线程内,使用:

这基本上是说“嘿,UI线程,为我执行这个东西”-并将所有必须在UI线程中执行的代码放入可运行状态-当您有一个线程从网络检索数据(不能在UI线程上运行)但必须在UI上发布结果时,这尤其有用(必须从UI线程执行)

例如:

view.post(new Runnable(){ 
    public void run(){
        //put all the code you want to be execute on the UI thread here
    }
});

尝试使用runOnUi函数从其他线程中“触摸”主线程中的视图。

尝试使用runOnUi函数从其他线程中“触摸”主线程中的视图。

我上面的代码与处理程序配合使用效果很好……但我会尝试您的代码,谢谢。我可以问您一个“可运行”的示例吗?new Runnable(){public void run(){//将所有代码放在这里};-提交vith view.post,括号内的所有代码将被发送到UI线程执行我上面的代码与处理程序一起工作很好…但是我会尝试你的代码谢谢我可以问你一个什么是“可运行”的示例吗?new Runnable(){public void run(){//将所有代码放在这里};-提交vith view.post,括号内的所有代码都将发送到UI线程执行