Android 如何在我的自定义 Listview 中实现 AsyncTask
Android How to implement AsyncTask in my custom Listview
当我 运行 我的代码时,我总是得到一个关于 Skipped 303 frames! The application may be doing too much work on its main thread.
如何在下面的代码中实现 AsyncTask,以及如何显示进度条的日志,同时从我的 Parse.com的数据库。如果有任何帮助,我将不胜感激。
这是我的 Activity 的代码:
@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;
}
我看到的一个问题是每次调用 getItemView()
时您都在调用 findViewById()
; findViewById()
是一项昂贵的操作。你应该实现类似 ViewHolder
模式的东西来避免这种情况。
根据 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
}
});
当我 运行 我的代码时,我总是得到一个关于 Skipped 303 frames! The application may be doing too much work on its main thread.
如何在下面的代码中实现 AsyncTask,以及如何显示进度条的日志,同时从我的 Parse.com的数据库。如果有任何帮助,我将不胜感激。
这是我的 Activity 的代码:
@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;
}
我看到的一个问题是每次调用 getItemView()
时您都在调用 findViewById()
; findViewById()
是一项昂贵的操作。你应该实现类似 ViewHolder
模式的东西来避免这种情况。
根据 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
}
});