Android 中 AsyncTask 的参数

Parameters of AsyncTask in Android

我正在尝试了解 Android 中的 AsyncTask。我不明白我们如何传递参数。在这段代码中:

 protected class AsyncLogin extends AsyncTask<String, JSONObject, Boolean> {
        String userName = null;

        @Override
        protected Boolean doInBackground(String... params)
        {
            RestAPI api = new RestAPI();
            boolean userAuth = false;
            try
            {
                JSONObject jsonObj = api.UserAuthentication(params[0], params[1]);
                JSONParser parser = new JSONParser();
                userAuth = parser.parseUserAuth(jsonObj);
                userName = params[0];

            }
            catch (Exception e)
            {
                Log.d("AsyncLogin", e.getMessage());
            }

            return  userAuth;
        }

        @Override
        protected void onPreExecute()
        {
            super.onPreExecute();
            Toast.makeText(context, "Please wait...", Toast.LENGTH_SHORT).show();
        }

        @Override
        protected  void onPostExecute(Boolean result)
        {
            if(result) {
                Intent i = new Intent(LoginActivity.this, UserDetailsActivity.class);
                i.putExtra("username", userName);
                startActivity(i);
            }
            else
            {
                Toast.makeText(context, "Not valid username/password", Toast.LENGTH_SHORT).show();
            }
        }
    }

我不明白为什么我们在

中使用 <String, JSONObject, Boolean>
protected class AsyncLogin extends AsyncTask<String, JSONObject, Boolean>

String、JSONObject、Boolean指的是什么?你能给我解释一下吗?谢谢

异步任务实现允许您将一种类型的参数作为参数。但是您可以通过向它声明一个参数化构造函数来向它传递更多类型参数。 例如

class YourAsynchTask extends AsyncTask<ArgumentObject, ProgressObject, ResultObject> {

......

ObjectType1 argument1;
ObjectType2 argument2;
ObjectType3 argument3;

YourAsynchTask(ObjectType1 arg1, ObjectType2 arg2, ObjectType3 arg3) {
argument1 = arg1;
argument2 = arg2;
argument3 = arg3;
    } 


   // rest of the method of your asynch task like doInBackground, etc.
}

你可以这样调用这种类型的异步任务:

new YourAsynchTask(arg1, arg2, arg3).execute(argumentObjet);

AsyncTask (Type1, Type2, Type3) 使用参数类型:

     public class Child
            {
                String child_title;

                public String getChild_title() {
                    return child_title;
                }

                public void setChild_title(String child_title) {
                    this.child_title = child_title;
                }
            }

            public class Parent
            {
                String header_title;
                ArrayList<Child>childArrayList;

                public ArrayList<Child> getChildArrayList() {
                    return childArrayList;
                }

                public void setChildArrayList(ArrayList<Child> childArrayList) {
                    this.childArrayList = childArrayList;
                }

                public String getHeader_title() {
                    return header_title;
                }

                public void setHeader_title(String header_title) {
                    this.header_title = header_title;
                }
            }


            public class ExpandAdapter extends BaseExpandableListAdapter
            {
                Context context;
                ArrayList<Parent>parentArrayList;
                LayoutInflater li;
                public ExpandAdapter(Context context, ArrayList<Parent> parentArrayList)
                {
                    this.context=context;
                    this.parentArrayList=parentArrayList;
                    li= (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
                }
                @Override
                public int getGroupCount() {
                    return parentArrayList.size();
                }

                @Override
                public int getChildrenCount(int groupPosition) {
                    return parentArrayList.get(groupPosition).getChildArrayList().size();
                }

                @Override
                public Object getGroup(int groupPosition) {
                    return parentArrayList.get(groupPosition);
                }

                @Override
                public Object getChild(int groupPosition, int childPosition) {
                    return parentArrayList.get(groupPosition).getChildArrayList().get(childPosition);
                }

                @Override
                public long getGroupId(int groupPosition) {
                    return groupPosition;
                }

                @Override
                public long getChildId(int groupPosition, int childPosition) {
                    return childPosition;
                }

                @Override
                public boolean hasStableIds() {
                    return true;
                }

                @Override
                public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

                    convertView=li.inflate(R.layout.customheader,null);
                    TextView tv= (TextView) convertView.findViewById(R.id.tv);
                    tv.setText(parentArrayList.get(groupPosition).getHeader_title());
                    return convertView;
                }

                @Override
                public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
                    convertView=li.inflate(R.layout.customchild,null);
                    TextView tv1= (TextView) convertView.findViewById(R.id.tv1);
                    tv1.setText(parentArrayList.get(groupPosition).childArrayList.get(childPosition).getChild_title());
                    return convertView;
                }
                @Override
                public boolean isChildSelectable(int groupPosition, int childPosition) {
                    return true;
                }
            }

             ExpandableListView expandlv;
                ArrayList<Parent>parentArrayList;
                ArrayList<Child>childArrayList;
                Parent parent;
                Child child;
                String header_title[]={"No","Alpha","Funcation"};
                String child_notitle[]={"1","2","3"};
                String child_alphatitle[]={"A","B","C"};
                String child_functiontitle[]={"F1","F2","F3"};
                Context context=this;
                ExpandAdapter expandadapter;


             expandlv= (ExpandableListView) findViewById(R.id.expandlv);
                    parentArrayList =new ArrayList<>();
                    for(int i=0;i<header_title.length;i++)
                    {
                        parent =new Parent();
                        parent.setHeader_title(header_title[i]);
                        childArrayList =new ArrayList<>();
                        if(i==0)
                        {
                            for(int j=0;j<child_notitle.length;j++)
                            {
                                child =new Child();
                                child.setChild_title(child_notitle[j]);
                                childArrayList.add(child);
                                parent.setChildArrayList(childArrayList);
                            }
                        }
                        if(i==1)
                        {
                            for(int j=0;j<child_alphatitle.length;j++)
                            {
                                child=new Child();
                                child.setChild_title(child_alphatitle[j]);
                                childArrayList.add(child);
                                parent.setChildArrayList(childArrayList);
                            }
                        }
                        if(i==2)
                        {
                            for(int j=0;j<child_functiontitle.length;j++)
                            {
                                child=new Child();
                                child.setChild_title(child_functiontitle[j]);
                                childArrayList.add(child);
                                parent.setChildArrayList(childArrayList);
                            }
                        }
                        parentArrayList.add(parent);
                    }
                    expandadapter=new ExpandAdapter(context,parentArrayList);
                   expandlv.setAdapter((ExpandableListAdapter) expandadapter);


        customheader

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="vertical" android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Header Title"
                android:textColor="@android:color/darker_gray"
                android:textSize="30dp"
                android:layout_centerHorizontal="true"
                android:id
            enter code here

        customchild

        <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="vertical" android:layout_width="match_parent"
            android:layout_height="match_parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv1"
            android:layout_centerHorizontal="true"
            android:text="Child Title"
            android:textColor="@android:color/holo_green_dark"
            android:textSize="20dp"/>
        </RelativeLayout>


            <ExpandableListView
                android:layout_width="wrap_content"
                android:layout_margin="20dp"
                android:layout_height="wrap_content"
                android:id="@+id/expandlv"/>


  View Pager  With Slide

  <android.support.design.widget.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary">
    </android.support.design.widget.TabLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:padding="@dimen/_10sdp" />

            TabLayout tabLayout;
    ViewPager viewPager;

  tabLayout=(TabLayout)findViewById(R.id.tabLayout);
        viewPager=(ViewPager)findViewById(R.id.viewPager);
        tabLayout.addTab(tabLayout.newTab().setText(""));
        tabLayout.addTab(tabLayout.newTab().setText(""));
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
        final MyAdapter adapter = new MyAdapter(MainActivity.this,getSupportFragmentManager(), tabLayout.getTabCount());
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
    }


public class MyAdapter extends FragmentPagerAdapter {

    private Context myContext;
    int totalTabs;

    public MyAdapter(Context context, FragmentManager fm, int totalTabs) {
        super(fm);
        myContext = context;
        this.totalTabs = totalTabs;
    }

    // this is for fragment tabs
    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                Fragment0 zero = new Fragment0();
                return zero;
            case 1:
                Fragment1 one = new Fragment1();
                return one;

            default:
                return null;
        }
    }
    // this counts total number of tabs
    @Override
    public int getCount() {
        return totalTabs;
    }
}