0

I need that my ListView support different layouts depending on some condition, but it's not working and layout is the same in every cell. Here is a part of my code that unfortunately isn't working:

public class CustomAdapter extends ArrayAdapter<String> {

    private final Activity context;
    private final String[] sSecondStartTimeArray;
    private final String[] sSecondEndTimeArray;
    private final String[] sSecondTitleArray;
    private final String AllData2;
    public CustomAdapter(Activity context,
                         String[] sSecondStartTimeArray, String[] sSecondEndTimeArray, String[] sSecondTitleArray, String AllData2) {
        super(context, R.layout.relcell2main, sSecondStartTimeArray);
        this.context = context;
        this.sSecondStartTimeArray = sSecondStartTimeArray;
        this.sSecondEndTimeArray = sSecondEndTimeArray;
        this.sSecondTitleArray = sSecondTitleArray;
        this.AllData2 = AllData2;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            LayoutInflater inflater = context.getLayoutInflater();
            int resource;
            switch (HTTPRequestSecond.GetSecondType(AllData2, position)) {
                case "rest":
                    resource = R.layout.relcell2second;
                    break;
                default:
                    resource = R.layout.relcell2main;
                    break;
            }
            convertView = inflater.inflate(resource, parent, false);
        }
        //some code (it's now working properly even without this part)
        return convertView;
    }

"rest" result exists, I checked. But not working anyway.

galath
  • 5,717
  • 10
  • 29
  • 41
Justin McGuire
  • 365
  • 1
  • 5
  • 18
  • Any errors? It may be related to your `inflater.inflate` method, it throws a `InflateException` if theres an error. – Erick Filho Jul 06 '15 at 20:45
  • @ErickFilho no errors, just the same layout everywhere :( – Justin McGuire Jul 06 '15 at 20:53
  • Only `relcell2main` is shown? – Erick Filho Jul 06 '15 at 20:56
  • @ErickFilho Yup. But for now it's alright, getItemViewType() and getViewTypeCount() really helped. But I wonder why inflaters don't inflate XMLs in their original ways? Actually, cells sometimes don't look like corresponding XMLs (e.g., they finally may be de facto "wrap_content" in width despite the original was "match_parent", and inflaters ignore some xml tags (e.g., margin)). Do you know why it happens? – Justin McGuire Jul 06 '15 at 23:49
  • Never experienced such a thing, my cells are just like the xml I design. – Erick Filho Jul 07 '15 at 12:11
  • @ErickFilho what type of layout do you use in cells? "margin"-tags work too? – Justin McGuire Jul 07 '15 at 13:08
  • Yea, `layout_margin` works. As well as, `padding`. – Erick Filho Jul 07 '15 at 13:19
  • @ErickFilho and what type of Layout is the best way to use as mark-up for cells? – Justin McGuire Jul 07 '15 at 13:26
  • I have cells using Frame, Relative and Linear. It really depends, however you should always keep it simple. So if you can use Linear, use it. – Erick Filho Jul 07 '15 at 13:46
  • @ErickFilho and is it possible to make different cell height in one ListView? I parse data from Internet so the height of each cell should be dynamic, but everywhere I have the same height as the first cell. – Justin McGuire Jul 07 '15 at 14:30
  • Yes, I think it is possible. Try something like setting parent layout's height to `wrap_content` and then having a TextView with your parsed data. Take a look at TextView docs to wrap the text. – Erick Filho Jul 07 '15 at 17:06

1 Answers1

0

Try using getItemViewType(). Explanation here and tutorial here.

Community
  • 1
  • 1
Damian Kozlak
  • 7,065
  • 10
  • 45
  • 51