Android如何在我的自定义Listview中实现AsyncTask

时间:2015-04-22 16:54:55

标签: android parse-platform android-asynctask custom-lists

当我运行我的代码时,我总是得到一个关于Skipped 303 frames! The application may be doing too much work on its main thread.的日志如何在下面的代码中实现AsyncTask,以及如何在从我的Parse.com数据库中获取数据时显示进度条。我将不胜感激任何帮助。

这是我的活动代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_doctors_name_list);

    Bundle extras = getIntent().getExtras();
    final String key = extras.getString("KEY");
    ListView lvDoctorsName = (ListView) findViewById(R.id.lvDoctorsName);

    ParseQueryAdapter.QueryFactory<ParseObject> factory = new ParseQueryAdapter.QueryFactory<ParseObject>() {
        public ParseQuery create() {
            ParseQuery query = new ParseQuery("doctors");
            query.whereContains("name", key);
            return query;
        }
    };
    CustomLayout urgentAdapter = new CustomLayout(this, factory);
    lvDoctorsName.setAdapter(urgentAdapter);

    TextView empty = (TextView) findViewById(R.id.empty_list_item);
    lvDoctorsName.setEmptyView(empty);

    itemClickListener();
}

public void itemClickListener() {
    ListView lvDoctorsName = (ListView) findViewById(R.id.lvDoctorsName);
    lvDoctorsName.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            ParseObject item = (ParseObject) parent.getAdapter().getItem(position);
            String objectID = item.getObjectId().toString();
            Intent i = new Intent();
            i.setClass(getApplicationContext(), DoctorPage.class);
            //i.putExtra("new_variable_name",value);
            i.putExtra("objectID", objectID);
            startActivity(i);
        }
    });

}

这是我的自定义布局:

public class CustomLayout extends ParseQueryAdapter<ParseObject> {


public CustomLayout(Context context, QueryFactory<ParseObject> queryFactory) {
    super(context, queryFactory);
}


@Override
public View getItemView(ParseObject object, View v, ViewGroup parent) {
    if (v == null) {
        v = View.inflate(getContext(), R.layout.row, null);
    }
    super.getItemView(object, v, parent);

    TextView titleTextView = (TextView) v.findViewById(R.id.text1);
    titleTextView.setText(object.getString("name"));

    TextView titleTextView2 = (TextView) v.findViewById(R.id.text2);
    titleTextView2.setText(object.getString("city"));

    return v;
}

2 个答案:

答案 0 :(得分:1)

我看到的一个问题是,每次调用findViewById()时都会调用getItemView(); findViewById()是一项昂贵的操作。你应该实现类似ViewHolder模式的东西来避免这种情况。

答案 1 :(得分:1)

根据Parse文档的外观,他们会为您处理AsyncTask。所以这不是问题所在。

根据您的数据集的大小,Emmanuel对findViewById的回答是正确的。

要回答如何显示进度条,您可以阅读ParseQueryAdapter上的文档,该文档可让您浏览加载/完成加载触发器。

https://parse.com/docs/android/api/com/parse/ParseQueryAdapter.html

// Perhaps set a callback to be fired upon successful loading of a new set of ParseObjects.
adapter.addOnQueryLoadListener(new OnQueryLoadListener<ParseObject>() {
  public void onLoading() {
    // Trigger any "loading" UI
  }

  public void onLoaded(List<ParseObject> objects, ParseException e) {
    // Execute any post-loading logic, hide "loading" UI
  }
});