0

我正在编写我的第一个 Android 应用程序并面临一个问题:( 我正在使用 Eclipse,该应用程序旨在从 Android 4.0.3 运行。

我在视图顶部有一个选项卡操作栏,由 4 个选项卡组成。其中三个具有相同的结构:一个带有两个单选按钮的单选组和一个列表视图。这些选项卡基于相同的数据,将用于过滤数据。radioButton 的文本取决于选项卡,并将进一步过滤数据。

最后一个选项卡具有完全不同的布局和功能。

我制作了两个片段和两个布局(一个用于前三个,一个用于最后一个选项卡)。第一个片段是管理无线电组和列表视图。我还有一个适配器来设置数据模型,具体取决于选择的选项卡和按钮。但不知何故,我就是无法让它正常工作。单选按钮的 不会刷新,并且列表仅在第一次加载活动时显示。不会抛出异常。

我的方法错了吗?

这是我的主要活动:

    @Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_main);

    appContext = getApplicationContext();


    dataholder = (P1DataHolder) getIntent().getSerializableExtra(
            "data"); //Hold all the data

    final ActionBar mainBar = getActionBar();
    mainBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    mainBar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);

    // create the fragments with the name of parent tab
    Fragment aFragment = new ListFragment(
            MyApplication.TAB_A);
    Fragment bFragment = new ListFragment(
            MyApplication.TAB_B);
    Fragment cFragment = new ListFragment(
            MyApplication.TAB_C);
    Fragment settingFragment = new P1SettingFragment();

    // Fragment A
    ActionBar.Tab tabA = mainBar.newTab().setText(
            MyApplication.TAB_A);
    tabA.setTabListener(new TabsListener(aFragment));

    // Fragment B
    ActionBar.Tab tabB = mainBar.newTab().setText(
            MyApplication.TAB_B);
    tabB.setTabListener(new TabsListener(bFragment));

    // Fragment C
    ActionBar.Tab tabC = mainBar.newTab().setText(
            MyApplication.TAB_C);
    tabC.setTabListener(new TabsListener(cFragment));

    // Settings
    ActionBar.Tab tabSetting = mainBar.newTab().setText(
            MyApplication.TAB_SETTING);
    tabSetting.setTabListener(new TabsListener(settingFragment));

    // Adding Tabs to actionbar
    mainBar.addTab(tabA);
    mainBar.addTab(tabB);
    mainBar.addTab(tabC);
    mainBar.addTab(tabSetting);

}

编辑:框架代码

public class ListFragment extends Fragment {

private ArrayList<Recipe> listAll;
private ArrayList<Recipe> listNotCookedYet;
private CustomListView ls;
private SectionComposerAdapter adapter;
private Activity parent;
private String tabTag; // Tab state from Parent
private View view;
private SegmentedRadioGroup segmentText;
private RadioButton rbAll;
private RadioButton rbNotCookedYet;


public ListFragment(String tabTag, ArrayList<Recipe> listAll, ArrayList<Recipe> listNTY) {
    this.tabTag = tabTag;
    this.listAll = listAll;
    this.listNotCookedYet = listNTY;
}

public String getTabTag(){
    return this.tabTag;
}

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

    // Inflate the layout for this fragment
    view = inflater.inflate(R.layout.list_with_section, container,
            false);
    parent = getActivity();

    // get Elements
    segmentText = (SegmentedRadioGroup) view
            .findViewById(R.id.segment_text);
    rbAll = (RadioButton) view.findViewById(R.id.button_list_all);
    rbNotCookedYet = (RadioButton) view
            .findViewById(R.id.button_list_notcookedyet);

    // Set Content
    if (parent != null && parent instanceof ListMainActivity) {

        ListDataHolder holder = ((ListMainActivity) parent)
                .getDataHolder();

        adapter = new SectionComposerAdapter(parent, holder);
        adapter.getCount();
        initialize(adapter);

        System.out.println("ANZAHL ALL: " + listAll.size());
        System.out.println("ANZAHL UNCOOKED: " + listNotCookedYet.size());

        //The default text is "all (*)" and "not cooked yet (*)"
        rbAll.setText(rbAll.getText().toString()
                .replace("*", listAll.size() + ""));
        rbNotCookedYet.setText(rbNotCookedYet.getText().toString()
                .replace("*", listNotCookedYet.size() + ""));

        segmentText
                .setOnCheckedChangeListener(new OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(RadioGroup group,
                            int checkedId) {

                        if (rbAll.getId() == checkedId) {
                            adapter.setDataModel(tabTag, true);
                            System.out.println("CHECKED: All");

                        }

                        else if (rbNotCookedYet.getId() == checkedId) {
                            adapter.setDataModel(tabTag, false);
                            System.out.println("CHECKED: Not Yet");
                        }


                    }
                });

    }
    return view;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

}

private void initialize(SectionComposerAdapter adapter) {
    ls = (CustomListView) view
            .findViewById(R.id.list_services);
    ls.setPinnedHeaderView(LayoutInflater.from(parent).inflate(
            R.layout.item_composer_header, ls, false));
    ls.setAdapter(adapter);
    ls.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            Intent intent = new Intent(view.getContext(),
                    RecipeActivity.class);
            startActivityForResult(intent, 0);

        }
    });
}

ListDataHolder 已按字母顺序对数据进行分段,并将它们作为单独的 Arraylist 保存为每个选项卡和每个单选按钮选择(因此有 6 个数组列表)。

这是我的适配器:

public class SectionComposerAdapter extends CustomListAdapter {

private Activity activity;
private ListDataHolder dataholder;
private static LayoutInflater inflater = null;
private ImageLoader imageLoader;
private List<Pair<String, ArrayList<Recipe>>> data;

public SectionComposerAdapter(Activity a, ListDataHolder holder) {
    activity = a;
    this.dataholder = holder;
    inflater = (LayoutInflater) activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader = new ImageLoader(activity.getApplicationContext());
    data = dataholder.getAllData(dataholder.getAZRecipeAll());
}

public void setDataModel(String tab, boolean all) {
    if (MyApplication.TAB_A.equals(tab)) {
        if (all) {
            data = dataholder.getAllData(dataholder.getAZRecipeAll());
        } else {
            data = dataholder.getAllData(dataholder
                    .getAZRecipeNotCookedYet());
        }
    } else if (MyApplication.TAB_B.equals(tab)) {
        if (all) {
            data = dataholder.getAllData(dataholder.getMeatRecipeAll());
        } else {
            data = dataholder.getAllData(dataholder
                    .getMeatRecipeNotCookedYet());
        }
    } else if (MyApplication.TAB_C.equals(tab)) {
        if (all) {
            data = dataholder
                    .getAllData(dataholder.getVegetarianRecipeAll());
        } else {
            data = dataholder.getAllData(dataholder
                    .getVegetarianRecipeNotCookedYet());
        }
    }


}

@Override
public int getCount() {
    int res = 0;
    for (int i = 0; i < data.size(); i++) {
        res += data.get(i).second.size();
    }
    return res;
}

@Override
public Recipe getItem(int position) {
    int c = 0;
    for (int i = 0; i < data.size(); i++) {
        if (position >= c && position < c + data.get(i).second.size()) {
            return data.get(i).second.get(position - c);
        }
        c += data.get(i).second.size();
    }
    return null;
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
protected void onNextPageRequested(int page) {
}

@Override
protected void bindSectionHeader(View view, int position,
        boolean displaySectionHeader) {
    if (displaySectionHeader) {
        view.findViewById(R.id.list_section_header).setVisibility(
                View.VISIBLE);
        TextView lSectionTitle = (TextView) view
                .findViewById(R.id.list_section_header);
        lSectionTitle
                .setText(getSections()[getSectionForPosition(position)]);
    } else {
        TextView lSectionTitle = (TextView) view
                .findViewById(R.id.list_section_header);
        lSectionTitle
                .setText(getSections()[getSectionForPosition(position)]);
        view.findViewById(R.id.list_section_header)
                .setVisibility(View.GONE);
    }
}

@Override
public View getAmazingView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    if (vi == null)
        vi = inflater.inflate(R.layout.list_row, null);

    TextView recipename = (TextView) vi.findViewById(R.id.recipe_name);
    TextView recipevip = (TextView) vi.findViewById(R.id.recipe_fav_mark);
    ImageView thumb_image = (ImageView) vi.findViewById(R.id.list_image);

    Recipe recipe = new Recipe();
    recipe = getItem(position);

    // Setting all values in listview
    recipename.setText(recipe.getTitle);
    recipevip.setText(recipe.isFav() ? "FAV" : "");
    imageLoader.DisplayImage(recipe.getImage_thumbnail(), thumb_image);

    return vi;
}

@Override
public void configurePinnedHeader(View header, int position, int alpha) {
    TextView lSectionHeader = (TextView) header;
    lSectionHeader.setText(getSections()[getSectionForPosition(position)]);
    // lSectionHeader.setBackgroundColor(alpha << 24 | (0xbbffbb));
    // lSectionHeader.setTextColor(alpha << 24 | (0x000000));
}

@Override
public int getPositionForSection(int section) {
    if (section < 0)
        section = 0;
    if (section >= data.size())
        section = data.size() - 1;
    int c = 0;
    for (int i = 0; i < data.size(); i++) {
        if (section == i) {
            return c;
        }
        c += data.get(i).second.size();
    }
    return 0;
}

@Override
public int getSectionForPosition(int position) {
    int c = 0;
    for (int i = 0; i < data.size(); i++) {
        if (position >= c && position < c + data.get(i).second.size()) {
            return i;
        }
        c += data.get(i).second.size();
    }
    return -1;
}

@Override
public String[] getSections() {
    String[] res = new String[data.size()];
    for (int i = 0; i < data.size(); i++) {
        res[i] = data.get(i).first;
    }
    return res;
}

该列表在没有选项卡和单选按钮的情况下工作正常。监听器也工作正常,我在日志中看到选项卡和单选按钮的变化。我只是无法让他们正确地一起工作。

提前谢谢你,我希望得到一些提示;)

戴安娜

4

1 回答 1

0

呃...多么尴尬,我花了两天时间在代码中寻找问题,而答案却如此简单。我有从其他选项卡的 url 返回的空数据。

对不起!

于 2012-07-20T17:04:47.570 回答