Java 添加异步任务功能?
我有一个活动,我将从API调用中获取信息。我知道android的主线程,并且知道我不应该让它过载。我尝试使用线程,但我认为对于应用程序的复杂性,Asynctask将是最好的方式 我目前正在使用Java 添加异步任务功能?,java,android,multithreading,android-asynctask,Java,Android,Multithreading,Android Asynctask,我有一个活动,我将从API调用中获取信息。我知道android的主线程,并且知道我不应该让它过载。我尝试使用线程,但我认为对于应用程序的复杂性,Asynctask将是最好的方式 我目前正在使用Thread().run(),但它仍然返回“跳过x帧,主线程中的工作太多”。我想知道如何将Asynctask类添加到我的应用程序中以获得更好的性能。我的活动使用ExpandableListView Home.java: public class Home extends Activity { @Overr
Thread().run()
,但它仍然返回“跳过x帧,主线程中的工作太多”。我想知道如何将Asynctask类添加到我的应用程序中以获得更好的性能。我的活动使用ExpandableListView
Home.java:
public class Home extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_act);
init();
}
void init() {
findViews();
changeFont();
clickListeners();
assignConditions("category", "all", "1");
categoryAllApiCall();
}
void categoryAllApiCall() {
RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(base_url).setLogLevel(RestAdapter.LogLevel.FULL).build();
final Category_All category_all = restAdapter.create(Category_All.class);
category_all.getFeed(file, operation_condition, all_condition, max_depth_condition, new Callback<CategoryPojo>() {
@Override
public void success(CategoryPojo categoryPojo, Response response) {
progressBar.setVisibility(View.GONE);
final CategoryPojo category = categoryPojo;
new Thread(new Runnable() {
@Override
public void run() {
category_id = Arrays.copyOf(category.getCategoryId(), category.getCategoryId().length);
category_name = Arrays.copyOf(category.getCategoryName(), category.getCategoryName().length);
parent_id = Arrays.copyOf(category.getParentId(), category.getParentId().length);
}
}).run();
prepareListData();
setAdapter();
}
@Override
public void failure(RetrofitError error) {
tv_title_header.setText(error.getMessage());
progressBar.setVisibility(View.GONE);
}
});
}
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
int count = -1;
for (int i = 0; i < category_id.length; i++) {
List<String> child = new ArrayList<String>();
if (parent_id[i].equals("0")) {
count++;
listDataHeader.add(category_name[i]);
for (int j = 0; j < category_id.length; j++) {
if (parent_id[j].equals(category_id[i])) {
child.add(category_name[j]);
}
}
listDataChild.put(listDataHeader.get(count), child);
}
}
}
void setAdapter() {
elv_home_body_lay.setAdapter(new HomeExpandableListAdapter(this, listDataHeader, listDataChild));
elv_home_body_lay.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
int previousGroup = -1;
@Override
public void onGroupExpand(int groupPosition) {
if (groupPosition != previousGroup) {
elv_home_body_lay.collapseGroup(previousGroup);
}
previousGroup = groupPosition;
}
});
elv_home_body_lay.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
Intent intent = new Intent(Home.this, ProductListing.class);
Bundle bundle = new Bundle();
bundle.putString("operation_condition", "productlisting");
bundle.putString("catids_condition", category_id[childPosition]);
bundle.putString("catname", category_name[childPosition]);
bundle.putString("start_row_condition", "0");
bundle.putString("limit_condition", "10");
intent.putExtras(bundle);
startActivity(intent);
return false;
}
});
}
}
公共类主页扩展活动{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.home_act);
init();
}
void init(){
FindView();
changeFont();
单击侦听器();
转让条件(“类别”、“全部”、“1”);
categoryAllApiCall();
}
void categoryAllApiCall(){
RestAdapter RestAdapter=new RestAdapter.Builder().setEndpoint(base_url).setLogLevel(RestAdapter.LogLevel.FULL).build();
最终Category\u All Category\u All=restAdapter.create(Category\u All.class);
category_all.getFeed(文件、操作条件、所有条件、最大深度条件、新回调(){
@凌驾
公共作废成功(CategoryPojo CategoryPojo,响应){
progressBar.setVisibility(View.GONE);
最终类别项目类别=类别项目;
新线程(newrunnable()){
@凌驾
公开募捐{
category_id=Arrays.copyOf(category.getCategoryId(),category.getCategoryId().length);
category_name=Arrays.copyOf(category.getCategoryName(),category.getCategoryName().length);
parent_id=Arrays.copyOf(category.getParentId(),category.getParentId().length);
}
}).run();
prepareListData();
setAdapter();
}
@凌驾
公共无效失败(错误){
tv_title_header.setText(error.getMessage());
progressBar.setVisibility(View.GONE);
}
});
}
私有void prepareListData(){
listDataHeader=新的ArrayList();
listDataChild=newHashMap();
整数计数=-1;
对于(int i=0;i
HomeExpandableListAdapter.java
public class HomeExpandableListAdapter extends BaseExpandableListAdapter
{
private Context context;
private List<String> listDataHeader;
private HashMap<String, List<String>> listDataChild;
public HomeExpandableListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<String>> listChildData) {
this.context = context;
this.listDataHeader = listDataHeader;
this.listDataChild = listChildData;
}
@Override
public Object getChild(int groupPosition, int childPosititon)
{
return this.listDataChild.get(this.listDataHeader.get(groupPosition)).get(childPosititon);
}
@Override
public long getChildId(int groupPosition, int childPosition)
{
return childPosition;
}
@Override
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
{
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.home_child_items_lay, null);
}
TextView txtListChild = (TextView) convertView.findViewById(R.id.lblListItem);
txtListChild.setTypeface(EasyFonts.robotoLight(context));
txtListChild.setText(childText);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition)
{
return this.listDataChild.get(this.listDataHeader.get(groupPosition)).
size();
}
@Override
public Object getGroup(int groupPosition)
{
return this.listDataHeader.get(groupPosition);
}
@Override
public int getGroupCount()
{
return this.listDataHeader.size();
}
@Override
public long getGroupId(int groupPosition)
{
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
{
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null)
{
LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.home_group_items_lay, null);
}
TextView lblListHeader = (TextView) convertView.findViewById(R.id.lblListHeader);
ImageView img=(ImageView)convertView.findViewById(R.id.imageView1);
lblListHeader.setTypeface(EasyFonts.robotoBold(context));
lblListHeader.setText(headerTitle);
if(isExpanded)
{
img.setImageResource(R.drawable.ic_remove_grey_36pt_2x);
}
if(!isExpanded)
{
img.setImageResource(R.drawable.ic_add_grey_36pt_2x);
}
return convertView;
}
@Override
public boolean hasStableIds()
{
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition)
{
return true;
}
}
公共类HomeExpandableListAdapter扩展了BaseExpandableListAdapter
{
私人语境;
私有列表listDataHeader;
私有HashMap listDataChild;
公共HomeExpandableListAdapter(上下文上下文、列表listDataHeader、HashMap listChildData){
this.context=上下文;
this.listDataHeader=listDataHeader;
this.listDataChild=listChildData;
}
@凌驾
公共对象getChild(int-groupPosition、int-ChildPosition)
{
返回this.listDataChild.get(this.listDataHeader.get(groupPosition)).get(childpositionon);
}
@凌驾
公共长getChildId(int-groupPosition,int-childPosition)
{
返回子位置;
}
@凌驾
公共视图getChildView(int-groupPosition,final int-childPosition,boolean isLastChild,View-convertView,ViewGroup-parent)
{
最终字符串childText=(字符串)getChild(groupPosition,childPosition);
if(convertView==null){
LayoutInflater INFLATER=(LayoutInflater)this.context.getSystemService(context.LAYOUT\u INFLATER\u SERVICE);
convertView=充气机充气(R.layout.home\u child\u items\u lay,空);
}
TextView txtListChild=(TextView)convertView.findViewById(R.id.lblListItem);
setTypeface(EasyFonts.robotoLight(上下文));
setText(childText);
返回视图;
}
@凌驾
公共整数getChildrenCount(整数组位置)
{
返回this.listDataChild.get(this.listDataHeader.get(groupPosition))。
大小();
}
@凌驾
公共对象getGroup(int-groupPosition)
{
返回此.listDataHeader.get(groupPosition);
}
@凌驾
public int getGroupCount()
{
返回此.listDataHeader.size();
}
@奥夫
new Thread(new Runnable() {
@Override
public void run() {
category_id = Arrays.copyOf(category.getCategoryId(), category.getCategoryId().length);
category_name = Arrays.copyOf(category.getCategoryName(), category.getCategoryName().length);
parent_id = Arrays.copyOf(category.getParentId(), category.getParentId().length);
}
}).run();
new Thread(new Runnable() {
@Override
public void run() {
category_id = Arrays.copyOf(category.getCategoryId(), category.getCategoryId().length);
category_name = Arrays.copyOf(category.getCategoryName(), category.getCategoryName().length);
parent_id = Arrays.copyOf(category.getParentId(), category.getParentId().length);
}
}).start();
public class YourLongTask extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(String... urls) {
//run some long operation here on background
return "return some value to post execute function. change type based on your needs";
}
@Override
protected void onPostExecute(String result) {
// update your view here after on main thread after you have done long task
}
}
YourLongTask task = new YourLongTask().execute();
android.os.AsyncTask<Params, Progress, Result>