0

I'm trying to put a custom listview in a tabbed fragment, the closest I got to is that I put it into the left "list" pane of my dual-pane activity.

Here's the problem. I feel like something is wrong in my custom adapter and I'm not referring to context correctly. As for the tab1fragment, I'm pretty sure I'm passing that view incorrectly. As a note, instead of populating mylistview on OnCreate I put my listView in onAttached; I've heard that that's better to avoid certain functions returning null during the create process.

The design of this application is...

Single Activity Single Pane - > Multi-Pane Activity...

Multi-Pane Activity -> Left, a small container for a fragment that will contain a listview and a few textboxes. [Let's call this listView A for now.]

Multi-Pane Activity - > Right,Detail Pane: In the detail pane is a fragment that hosts tabs, each tab is a fragment. I want to put a listView in each tab. [List view B for now?]

When trying the closest I got was sticking it in my first "listview A".

In order of brevity I chopped down I'm cutting down the code if you feel like you need more don't hesitate to ask. [Otherwise it's about 7-8 pages] I put comments specifying the code that I think is problematic. I don't fully understand my getView in my listAdapter specifically the if (convertView == null) code block.

If you see any other weird issues and would like to offer constructive criticism I'm totally dying for a code review!

Other things to note: Using the fragmentsupport manager on all my code.

Tab1DrinkFragment

    @Override
    public void onAttach(Activity activity){
        super.onAttach(activity);

        ArrayList<Item> menu = new ArrayList<Item>();

        Item item1 = new Item("Coke","Pure cane sugar!", 2.50);
        Item item2 = new Item("Dr Pepper","Pure cane sugar!", 3.00);
        Item item3 = new Item("Sprite","Pure cane sugar!", 2.50);

        menu.add(item1);
        menu.add(item2);
        menu.add(item3);

        View v = getView();
        CustomOrderListAdapter adapter = new CustomOrderListAdapter(getActivity(),menu);

        ListView listView = (ListView) v.findViewById(R.id.drinkList); //Problem code
        listView.setAdapter(adapter);
    }

Tab1Drink.xml

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

 <!-- When I got this to work I was not using @id/drinkList but @+id/android:list -->
    <ListView
        android:id="@+id/drinkList"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:dividerHeight="1dp"
        android:background="@color/dark_blue" />

</RelativeLayout>

CustomOrderListAdapter

import java.util.ArrayList;
import java.util.Arrays;

import com.landa.backend.*;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.landa.R;

public class CustomOrderListAdapter extends BaseAdapter {

        Context context;
        int layoutResourceId;
        private ArrayList<Item> listData; //Once we get json working be a REALLY good idea to actually use the generic features of this

        private LayoutInflater layoutInflater;

        public CustomOrderListAdapter(Context context, ArrayList<Item> listData) {
            this.context = context;
            this.listData = listData;
        }

        @Override
        public int getCount() {
            return listData.size();
        }

        @Override
        public Object getItem(int position) {
            return listData.get(position);
        }

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

        public View getView(int position, View convertView, ViewGroup parent) {
            OrderHolder holder;
            if (convertView == null) {

                layoutInflater = LayoutInflater.from(context); //Problem code
                convertView = layoutInflater.inflate(R.layout.customer_order_list_row, null);
                holder = new OrderHolder();
                holder.nameView = (TextView) convertView.findViewById(R.id.itemName);
                holder.priceView = (TextView) convertView.findViewById(R.id.price);
                holder.removedItemsView = (TextView) convertView.findViewById(R.id.removedItems);
                convertView.setTag(holder);
            } else {
                holder = (OrderHolder) convertView.getTag();
            }
            Item item = (Item)listData.get(position);
            holder.nameView.setText(item.getName());
            holder.priceView.setText(String.valueOf(item.getPrice()));
            //holder.removedItemsView.setText(Arrays.toString(item.getRemoved()));

            return convertView;
        }

        static class OrderHolder {
            TextView nameView;
            TextView priceView;
            TextView removedItemsView;
        }

}
4

1 回答 1

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

    ArrayList<Item> menu = new ArrayList<Item>();

    Item item1 = new Item("Coke","Pure cane sugar!", 2.50);
    Item item2 = new Item("Dr Pepper","Pure cane sugar!", 3.00);
    Item item3 = new Item("Sprite","Pure cane sugar!", 2.50);

    menu.add(item1);
    menu.add(item2);
    menu.add(item3);

    CustomOrderListAdapter adapter = new CustomOrderListAdapter(getActivity(),menu);

    View view = inflater.inflate(R.layout.Tab1Drink, null);

    ListView listView = (ListView) view.findViewById(R.id.drinkList); //Problem code
    listView.setAdapter(adapter);

    return view;
}
于 2013-11-01T21:01:18.933 回答