【问题标题】:Using one adapter for multiple arrays对多个阵列使用一个适配器
【发布时间】:2016-07-25 15:41:20
【问题描述】:

我有一种情况需要一些建议。我有一个具有可扩展列表视图的应用程序。每个子点击都会将用户发送到下一个活动,这是一个带有列表视图的选项卡布局。选项卡布局有 3 个选项卡。我试图弄清楚当在可扩展列表视图上单击孩子时如何将数据发送到 3 个选项卡列表视图。

原本我打算在 setOnChildClickListener 中这样设置:

expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
            if (groupPosition == 0) {
                if (childPosition == 0) {
                    mCustomListViewAdapter.addAdapterItem(new CustomObject("Squats", "60%", "6", "150", false));
                    listViewFri.setAdapter(mCustomListViewAdapter);

编辑:这是我设置 3 个选项卡的选项卡布局活动。我不确定在哪里可以访问捆绑的附加功能。

public class WorkoutDaysActivity extends BaseActivity {

ListView listViewFri = (ListView) findViewById(R.id.listViewFri);
ListView listViewMon = (ListView) findViewById(R.id.listViewMon);
ListView listViewWed = (ListView) findViewById(R.id.listViewWed);

/**
 * The {@link android.support.v4.view.PagerAdapter} that will provide
 * fragments for each of the sections. We use a
 * {@link FragmentPagerAdapter} derivative, which will keep every
 * loaded fragment in memory. If this becomes too memory intensive, it
 * may be best to switch to a
 * {@link android.support.v4.app.FragmentStatePagerAdapter}.
 */
private SectionsPagerAdapter mSectionsPagerAdapter
/**
 * The {@link ViewPager} that will host the section contents.
 */



private ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.workout_days);
    Bundle extras = getIntent().getBundleExtra("args");



    mToolBar = activateToolbarWithHomeEnabled();
    setUpNavigationDrawer();

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);



}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_workout_days, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";

    public PlaceholderFragment() {
    }

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {



        switch (getArguments().getInt(ARG_SECTION_NUMBER)) {
            case 1:
                View rootView = inflater.inflate(R.layout.fragment_workout_days, container, false);

                Bundle extras = getArguments();
                CustomObject objects = (CustomObject) extras.getSerializable("w29w1");




                return rootView;
            case 2: View rootView2 = inflater.inflate(R.layout.fragment_sub_page1, container, false);
                TextView textView2 = (TextView) rootView2.findViewById(R.id.textView2);
                textView2.setText("Workout 29 Week 1");
                return rootView2;
            case 3:
                View rootView3 = inflater.inflate(R.layout.fragment_sub_page2, container, false);
                TextView textView3 = (TextView) rootView3.findViewById(R.id.txtFrag3);
                textView3.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
                return rootView3;
            default: View rootView4 = inflater.inflate(R.layout.fragment_workout_days, container, false);
                TextView textView4 = (TextView) rootView4.findViewById(R.id.txtFrag1);
                textView4.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
                return rootView4;
        }

    }
}

/**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
 * one of the sections/tabs/pages.
 */
public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class below).
        return PlaceholderFragment.newInstance(position + 1);
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return "Monday";
            case 1:
                return "Wednesday";
            case 2:
                return "Friday";
        }
        return null;
    }
}
}

我认为这部分会像这样:

  switch (getArguments().getInt(ARG_SECTION_NUMBER)) {
            case 1:
                View rootView = inflater.inflate(R.layout.fragment_workout_days, container, false);

                Bundle extras = getArguments();
                CustomObject objects = (CustomObject) extras.getSerializable("w29w1");
                CustomListViewAdapter customListViewAdapter = new CustomListViewAdapter(this, objects);

但我在 (this, objects) 下遇到错误,提示“无法应用于 .placeholderfragment .custom 对象”“PlaceholderFragment 无法转换为上下文”

这是我放在 onChildClick 中的:

Bundle extras = new Bundle();
                    final ArrayList<CustomObject> objects = new ArrayList<CustomObject>();
                    objects.add(new CustomObject("Squat", "65%", "6", "150", false));
                    extras.putSerializable("w29w1", objects);

                    Intent intent = new Intent(getApplicationContext(), WorkoutDaysActivity.class);
                    intent.putExtra("args", extras);
                    startActivity(intent);

感谢您的帮助!如果您需要查看我的任何代码以更好地了解我正在尝试做什么,请告诉我!

【问题讨论】:

    标签: android arraylist android-arrayadapter custom-adapter


    【解决方案1】:

    我很难完全理解这个问题,但这是我目前的想法。

    您是否尝试在每个列表上设置相同的列表适配器实例?如果您想要相同的一般行为,但要将不同的数据绑定到一个列表,请确保为每个列表实例化新的适配器实例。

    myList1.setAdapter(new MyAdapter());
    myList2.setAdapter(new MyAdapter());
    

    而不是

    MyAdapter myAdapter = new MyAdapter();
    myList1.setAdapter(myAdapter);
    myList2.setAdapter(myAdapter);
    

    此外,在与另一个活动通信时,通过意图移动数据是明智的。在您的初始活动中,当一个操作提示启动下一个活动时,在调用startActivity() 之前将附加内容放入意图中。然后,在下一个活动中,您将调用getIntent().getXXXExtra("..."),其中XXX 与您正在提取的额外内容类型匹配,... 是您在上一个活动中放置在额外内容上的正确键。

    如果您正在使用包含在活动中的片段,请使用Fragment.setArguments()(在活动2 中)和Fragment.getArguments()(在片段2 中)将额外内容从片段1 传递到活动2 到片段2。

    传递额外的东西 当您单击一个项目并想要启动一个新活动(要显示的新视图)时,一个好的方法是捆绑所需的信息,将其传递给新活动,解开所有这些信息,然后将其应用于视图你想要的。

    例如...

    @Override
    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
    if (groupPosition == 0) {
        if (childPosition == 0) {
            Bundle extras = new Bundle();
            // Put all the extras you need. This can be primitives like int, boolean, double, or complex objects using the Serializable interface
            // For example, say your CustomObject implements Serializable
            CustomObject customObject = new CustomObject("Squats", "60%", "6", "150", false);
            extras.putSerializable("object_key_here", customObject);
    
            Intent intent = new Intent(getContext(), NextActivityHere.class);
            intent.putExtra("args", extras);
            startActivity(intent);
        }
    }
    

    在包含包含列表的片段的活动中:

    // Likely in onCreate()
    Bundle extras = getIntent.getBundleExtra("args");
    MyFragment myFragment = new MyFragment();
    myFragment.setArguments(extras);
    // Use fragment manager to add the fragment to your view
    

    在包含列表的片段(myFragment)中:

    // Likely in onCreate()
    Bundle extras = getArguments();
    CustomObject customObject = (CustomObject) extras.getSerializable("object_key_here");
    
    // Now you can use the object to set up the adapter, etc...
    

    希望这至少有助于展示如何将数据从原始活动获取到包含列表的片段。如果我要填充一个列表,通常我会看到自己传递一个 ArrayList 数据。如果不了解您的场景,很难更具体。

    【讨论】:

    • 感谢您的回复。是的,我想要相同的一般行为,只是不同的数据集。因此,如果我要像您的示例 myList2.setAdapter(new MyAdapter) 那样做,我可以根据需要对尽可能多的数组列表执行此操作吗?我如何告诉应用程序使用哪个列表?另外,我不完全确定如何使用 fragment.setArguements。我的 tablayout 是 3 个不同的片段。每个都包含一个列表视图,我想用提到的数组列表填充它。我也不确定我会在哪里设置适配器来填充列表
    • 如果我输入类似intent.putExtra 的东西只是为了验证点击了哪个按钮,这会起作用吗?我实际上不需要传递字符串或任何东西,但是如果我从可扩展列表视图中的 childOnClick 发送一个字符串,然后放置一个 if 语句来查看意图是否在 tablayout 类中包含该字符串,所以我知道什么要输入的数据..这行得通吗?抱歉很难解释..在我的脑海里更有意义哈哈
    • 所以我得到的印象是每个片段都有一个 ListView (listView)。在每个片段中,您可能会调用 listView.setAdapter(myAdapter),可能在 onStart()onResume() 或片段生命周期早期 (developer.android.com/guide/components/fragments.html#Creating) 中。
    • 我将编辑我之前的答案,以提及更多关于将附加内容作为参数传递的信息。 listViewFriday listView 是下一个活动中的列表吗?
    • 是的,没错,每个片段都有一个列表视图。片段一是listViewMon,二是listViewWed,三是listViewFri。感谢您的帮助
    【解决方案2】:

    带有标签的布局

    您可能想阅读一下TabLayoutsViewPagers。这些是用于创建带有标签的分页布局的模型。

    实现这些视图应该会大大降低 Activity 的复杂性。在 Activity 的 onCreate() 方法中,您可以设置 TabLayout 和 ViewPager 来托管周一、周三和周五的片段。

    周一、周三和周五的每个片段都是同一个片段类的实例化,其中包含您希望填充的列表(如果我得到应用布局的正确视图)。

    我的假设列表

    • 假设 XML 文件名为 activity_tab_host.xml
      • 假设 TabLayout 有一个 id="tab_layout"
      • 假设 ViewPager 有一个 id="view_pager"
    • 假设活动文件名为 TabHostActivity.java
    • 假设带有列表的片段名为 WorkoutDetailFragment.java
      • 假设您的片段通过其构造函数接收数据。您可以执行此操作,也可以按照您所做的那样通过参数传递它。我只是试图在传达总体思路的同时尽量减少代码。片段只需要获取数据! 尽量不要沉迷于此处显示的特定方法和代码;您可能会发现通过构造函数等传递数据更容易。
      • 假设数据列表在CustomObjects的ArrayLists中已经准备好,简称为mondayData、wednesdayData、fridayData
    • 假设您已经创建了名为 SectionsPagerAdapter 的自定义适配器

    指导代码(不是 100% 的需要)

    从 XML 视图源文件中获取 TabLayout 和 ViewPager 视图:

    // TabHostActivity.java::onCreate()
    setContentView(R.layout.activity_tab_host);
    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
    ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
    

    设置您的 ViewPager:

    // TabHostActivity.java::onCreate()
    SectionsPagerAdapter adapter = new SectionsPagerAdapter(getFragmentManager());
    
    // Create fragment to populate the tabs
    WorkoutDetailFragment mondayFragment = new WorkoutDetailFragment(mondayData);
    WorkoutDetailFragment wednesdayFragment = new WorkoutDetailFragment(wednesdayData);
    WorkoutDetailFragment fridayFragment = new WorkoutDetailFragment(fridayData);
    
    // Add pages (fragments) to the adapter, passing the fragment and tab title
    adapter.addFragment(mondayFragment, "Monday");
    adapter.addFragment(wednesdayFragment, "Wednesday");
    adapter.addFragment(fridayFragment, "Friday");
    
    // Tie the adapter to your ViewPager
    viewPager.setAdapter(adapter);
    

    将 ViewPager 绑定到 TabView:

    // TabHostActivity.java::onCreate()
    tabLayout.setupWithViewPager(viewPager);
    

    关于在哪里实例化新片段的主要内容是...... ViewPager 是用于保存片段的设备,允许用户在单个活动中在这些片段之间进行更改。您应该在 TabHostActivity.java 活动中实例化片段并在 ViewPager 中设置片段。

    我上面的示例通过 WorkoutDetailFragment 构造函数传递列表数据来简化情况。这是将数据传递给片段的两种简单方法之一。

    通过片段构造函数传递数据:

    public class WorkoutDetailFragment extends Fragment {
    
        private ArrayList<Object> listData;
    
        public WorkoutDetailFragment(ArrayList<Object> listData) {
            this.listData = listData;
        }
    
        // Rest of the code for the fragment...
    
    }
    

    通过片段参数传递数据

    // TabHostActivity.java
    Bundle args = new Bundle(); // May have received this bundle from extras already. If so, use Bundle args = getIntent.getExtra(...
    args.putExtra("list_data", listData); // Where listData is an ArrayList<CustomObject> in your case (Monday/Wednesday/Friday data)
    
    WorkoutDetailFragment fragment = new WorkoutDetailFragment();
    fragment.setArguments(args);
    // Add the fragment to the view pager...
    // Inside WorkoutDetailFragment.java retrieve list data from args using getArguments().getSerializable("list_data")
    

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2019-04-06
      • 2011-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-30
      相关资源
      最近更新 更多