是否可以使用run()创建ListView?

时间:2013-12-18 04:34:26

标签: android listview android-arrayadapter runnable

我想知道是否可以使用类似的东西在runnable中创建列表视图?有人可以举个例子来做吗?谢谢。

public void testBtnListViewClick(View v) {
    MainActivity.this.runOnUiThread(new Runnable() {

     @Override
    public void run() {
       LinearLayout ll = new LinearLayout(this);
       ListView lv = new ListView(this);
       String[] values = new String[10];
       for(int i=0;i<10;i++){
          values[i] = ""+i;
       }
       ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, values);
       lv.setAdapter(adapter);
       lv.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
                 Toast.makeText(getBaseContext(), ""+arg2,Toast.LENGTH_SHORT).show();
                 Log.d("DEBUG", ""+arg2);
            }
        });
        //ll.addView(lv);
        ll.addView(lv, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
        setContentView(ll);
   }
}

1 个答案:

答案 0 :(得分:0)

是..你可以.. runOnUiThread()在主线程中运行。在android UI中,像Views这样的更改可以被修改或只在UI线程中添加..它在UI线程中所以没有你的问题..

里面指的是runnable object..so更改这些行

LinearLayout ll = new LinearLayout(this);
ListView lv = new ListView(this);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, values);

进入

LinearLayout ll = new LinearLayout(MainActivity.this);
ListView lv = new ListView(MainActivity.this);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.list_item, values);