Question

I am new to android and am trying to create two fragments and have it so you can slide between the two, one is a list and the otehr a grid. I had the list working when it when I was using an ArrayAdapter and had my EventListFragment extending ListFragment (if code is helpful let me know and ill post that part)

I am now trying to create a custom list view with multiline list items (let me know if there is an easier way and if i have simply overcomplicated the whole thing)

Here is my code:

Event List Fragment:

public class EventListFragment extends Fragment {

    ArrayList<EventObject> eventObjects;

    @Override
    public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle        savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        eventObjects = ((EventsActivity)getActivity()).getEventObjects();

        View view = inflater.inflate(R.layout.eventgrid ,container,false);

        Listview listView = (ListView) view.findViewById(R.id.listView);
        if (listView == null) {
             System.out.println("asas");
        }
        listView.setAdapter(new MyCustomBaseAdapter(getActivity().getBaseContext(), eventObjects));
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> a, View v, int position, long id) {
                Object o = listView.getItemAtPosition(position);
                EventObject fullObject = (EventObject)o;
                System.out.println("asd");
            }
        });
       return view;
    }

}

The corresponding xml:

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

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

<ListView
    android:id="@+id/listView"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"/>

</RelativeLayout>

The xml for the customgridrow:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent">
<TextView android:id="@+id/name"
          android:textSize="14sp"
          android:textStyle="bold"
          android:textColor="#FFFF00"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"/>
<TextView android:id="@+id/cityState"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"/>
<TextView android:id="@+id/phone"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"/>
</LinearLayout>

EDIT

The getView() from MyCustomBaseAdapter :

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.customgridrow, null);
        holder = new ViewHolder();
        holder.txtName = (TextView) convertView.findViewById(R.id.name);
        holder.txtCityState = (TextView) convertView.findViewById(R.id.cityState);
        holder.txtPhone = (TextView) convertView.findViewById(R.id.phone);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.txtName.setText(events.get(position).getName());
    holder.txtCityState.setText(events.get(position).getDate());
    holder.txtPhone.setText(events.get(position).getVenue());

    return convertView;
}
Was it helpful?

Solution

From your comments

You are inflating the wrong layout

You should inflate the one that has listview and initialize the same.

Change

View view = inflater.inflate(R.layout.eventgrid ,container,false);

to

 View view = inflater.inflate(R.layout.eventList ,container,false);

OTHER TIPS

Every time you want to add an object to a list, you have to provide a view for the object. Android asks your list adapter, be it custom or system-defined, it asks the adapter what each list item is supposed to look like. This is where getView() comes into play.

public abstract View getView (int position, View convertView, ViewGroup parent):
Get a View that displays the data at the specified position in the data set.

If convertView is null, you will get an NPE.

Tutorials here: http://www.javacodegeeks.com/2013/06/android-listview-tutorial-and-basic-example.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top