片段mysql数据库连接

时间:2014-03-17 11:25:10

标签: android mysql

这是我的片段,我有数据库适配器,但列表视图没有填充。

这是我的代码:

public class HomeFragment扩展了Fragment {

public HomeFragment(){}

private TextView txt;
private String n = "";
public String s="";
public ListView lst;
public Button btn;
private HomeAdapter adapter;
private List<homeinfo> items;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState)
{

    View rootView = inflater.inflate(R.layout.fragment_home, container, false);


   lst=(ListView) rootView.findViewById(R.id.listView);
   btn=(Button) rootView.findViewById(R.id.button1);

   btn.setOnClickListener(new OnClickListener() 
   {

    @Override
    public void onClick(View arg0) 
    {
        // TODO Auto-generated method stub
        Intent intentDemo=new Intent(getActivity(),Testing.class);
        startActivity(intentDemo);
    }
});


   items=new ArrayList<homeinfo>();
   adapter=new HomeAdapter(getActivity().getApplicationContext(),items);
   lst.setAdapter(adapter);




   Thread thread=new Thread(doInBackground);
    thread.start();


    return rootView;
}

Runnable doInBackground = new Runnable()      {

    @Override
    public void run() 
    {
        items = homeservice.getAllRadio("http://********.com/");
        runOnUiThread(runOnUi);
    }

 };

 Runnable runOnUi=new Runnable()
 {

    @Override
    public void run() 
    {
        for(homeinfo radioitems:items)
        {
            adapter.add(radioitems);
        }

        adapter.notifyDataSetChanged();
    }

 };

protected void runOnUiThread(Runnable runOnUi2){         // TODO自动生成的方法存根

}

}

1 个答案:

答案 0 :(得分:0)

方法runOnUiThread()是Activity类的一个元素。要从Fragment访问它,请尝试以下操作:

getActivity().runOnUiThread(runOnUi);