【问题标题】:How do i use and set custom listview adapter within a Thread?如何在线程中使用和设置自定义列表视图适配器?
【发布时间】:2012-09-14 22:31:26
【问题描述】:

我是 android 开发的菜鸟,我试图在 onCreate 期间填充列表视图时显示进度条。我正在尝试使用自定义适配器,但 eclipse 说我的构造函数未定义。具体来说,“构造函数 MainActivity.UserItemAdapter(new Thread(){}, int, ArrayList) 未定义”它建议我将自定义适配器上的上下文参数更改为线程。理想情况下,我想解决这个问题而不必更改我的构造函数,因为我在代码的其他地方使用它。任何帮助是极大的赞赏。这是我的代码。

创建时:

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    dialog = ProgressDialog.show(MainActivity.this, "Loading", "Loading, please wait..");
    Thread t = new Thread() {
        public void run() {
        //getting data for listview...   

    ListView listView = (ListView) findViewById(R.id.ListViewId);
    listView.setAdapter(new UserItemAdapter(this, R.layout.listitem, tweets));//<--error here constructor is undefined.

    handler.post(new Runnable() {
        public void run() {
            dialog.dismiss();                
          };              
      });        
    }           
 };
t.start();      
}

我的自定义适配器:

public class UserItemAdapter extends ArrayAdapter<Tweet> {
    private ArrayList<Tweet> tweets;

    public UserItemAdapter(Context context, int textViewResourceId, ArrayList<Tweet> tweets) {
        super(context, textViewResourceId, tweets);
        this.tweets = tweets;
    } 

【问题讨论】:

    标签: android multithreading parameters progress-bar adapter


    【解决方案1】:

    this 在这种情况下指的是 Thread 对象。用户getApplicationContext()MyActivity.this

    【讨论】:

    • 如果您知道 Activity 仅在该上下文中有效,请始终使用该上下文。如果任务仍有可能在活动之间执行并且Context 的使用与Activity 没有紧密联系,则仅使用getApplicationContext()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-18
    相关资源
    最近更新 更多