idk
idk

Reputation: 35

Custom ListView Adapter isn't working with List<List<String>> instead of String[]

I have a listView inside a fragment.

Im getting some json from the net and passing it into a List<List<String>>.

For some reason the list doesnt work.. there's no crash and there's nothing suspicious in the logcat..

it just doesnt show anything in the ListView.

Adapter's call (From the fragment):

setListAdapter(new MobileArrayAdapter(getActivity(), items));

This is my adapter's code:

package com.example.free; import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class MobileArrayAdapter extends ArrayAdapter<String> {
    private final Context context;
    public List<List<String>> items;
    //private final static String[] a={"s"};

    public MobileArrayAdapter(Context context, List<List<String>> items) {
        super(context, R.layout.list_item);
        this.context = context;
        this.items = items;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = inflater.inflate(R.layout.list_item, parent, false);
        TextView title = (TextView) rowView.findViewById(R.id.title);
        ProgressBar loader = (ProgressBar) rowView.findViewById(R.id.loader);
        ImageView image = (ImageView) rowView.findViewById(R.id.itemimgae);
        TextView views = (TextView) rowView.findViewById(R.id.views);
        TextView likes=(TextView) rowView.findViewById(R.id.likes);
        TextView upvote=(TextView) rowView.findViewById(R.id.upvote);
        TextView downvote=(TextView) rowView.findViewById(R.id.downvote);
        TextView desc=(TextView) rowView.findViewById(R.id.desc);
        TextView pub =(TextView) rowView.findViewById(R.id.pub);
        TextView idnum =(TextView) rowView.findViewById(R.id.idnum);

        Log.e("title",items.get(1).get(1));

        title.setText("      "+items.get(1).get(position));
        views.setText(items.get(6).get(position));
        likes.setText(items.get(7).get(position));
        upvote.setText(items.get(8).get(position));
        downvote.setText(items.get(9).get(position));
        desc.setText(items.get(4).get(position));
        pub.setText(items.get(5).get(position));
        idnum.setText(items.get(0).get(position));


        return rowView;
    }
}

items comes full.. I checked it, but for some reason the data doesnt get to the adapter..

Whats wrong here?

EDIT:

10-12 14:23:02.880: W/dalvikvm(23285): threadid=1: thread exiting with uncaught exception (group=0x4107b930)
10-12 14:23:02.900: E/AndroidRuntime(23285): FATAL EXCEPTION: main
10-12 14:23:02.900: E/AndroidRuntime(23285): java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
10-12 14:23:02.900: E/AndroidRuntime(23285):    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
10-12 14:23:02.900: E/AndroidRuntime(23285):    at java.util.ArrayList.get(ArrayList.java:304)
10-12 14:23:02.900: E/AndroidRuntime(23285):    at com.example.free.MobileArrayAdapter.getView(MobileArrayAdapter.java:49)
10-12 14:23:02.900: E/AndroidRuntime(23285):    at android.widget.AbsListView.obtainView(AbsListView.java:2143)
10-12 14:23:02.900: E/AndroidRuntime(23285):    at android.widget.ListView.measureHeightOfChildren(ListView.java:1246)
10-12 14:23:02.900: E/AndroidRuntime(23285):    at android.widget.ListView.onMeasure(ListView.java:1158)
10-12 14:23:02.900: E/AndroidRuntime(23285):    at android.view.View.measure(View.java:15525)
10-12 14:23:02.900: E/AndroidRuntime(23285):    at android.widget.RelativeLayout.measureChild(RelativeLayout.java:666)
10-12 14:23:02.900: E/AndroidRuntime(23285):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477)
10-12 14:23:02.900: E/AndroidRuntime(23285):    at android.view.View.measure(View.java:15525)
10-12 14:23:02.900: E/AndroidRuntime(23285):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
10-12 14:23:02.900: E/AndroidRuntime(23285):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-12 14:23:02.900: E/AndroidRuntime(23285):    at android.view.View.measure(View.java:15525)
10-12 14:23:02.900: E/AndroidRuntime(23285):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
10-12 14:23:02.900: E/AndroidRuntime(23285):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
10-12 14:23:02.900: E/AndroidRuntime(23285):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
10-12 14:23:02.900: E/AndroidRuntime(23285):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
10-12 14:23:02.900: E/AndroidRuntime(23285):    at android.view.View.measure(View.java:15525)
10-12 14:23:02.900: E/AndroidRuntime(23285):    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
10-12 14:23:02.900: E/AndroidRuntime(23285):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
10-12 14:23:02.900: E/AndroidRuntime(23285):    at android.view.View.measure(View.java:15525)
10-12 14:23:02.900: E/AndroidRuntime(23285):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
10-12 14:23:02.900: E/AndroidRuntime(23285):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-12 14:23:02.900: E/AndroidRuntime(23285):    at android.view.View.measure(View.java:15525)
10-12 14:23:02.900: E/AndroidRuntime(23285):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
10-12 14:23:02.900: E/AndroidRuntime(23285):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
10-12 14:23:02.900: E/AndroidRuntime(23285):    at android.view.View.measure(View.java:15525)
10-12 14:23:02.900: E/AndroidRuntime(23285):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
10-12 14:23:02.900: E/AndroidRuntime(23285):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-12 14:23:02.900: E/AndroidRuntime(23285):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2438)
10-12 14:23:02.900: E/AndroidRuntime(23285):    at android.view.View.measure(View.java:15525)
10-12 14:23:02.900: E/AndroidRuntime(23285):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
10-12 14:23:02.900: E/AndroidRuntime(23285):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
10-12 14:23:02.900: E/AndroidRuntime(23285):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
10-12 14:23:02.900: E/AndroidRuntime(23285):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
10-12 14:23:02.900: E/AndroidRuntime(23285):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
10-12 14:23:02.900: E/AndroidRuntime(23285):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
10-12 14:23:02.900: E/AndroidRuntime(23285):    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
10-12 14:23:02.900: E/AndroidRuntime(23285):    at android.view.Choreographer.doFrame(Choreographer.java:532)
10-12 14:23:02.900: E/AndroidRuntime(23285):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
10-12 14:23:02.900: E/AndroidRuntime(23285):    at android.os.Handler.handleCallback(Handler.java:725)
10-12 14:23:02.900: E/AndroidRuntime(23285):    at android.os.Handler.dispatchMessage(Handler.java:92)
10-12 14:23:02.900: E/AndroidRuntime(23285):    at android.os.Looper.loop(Looper.java:137)
10-12 14:23:02.900: E/AndroidRuntime(23285):    at android.app.ActivityThread.main(ActivityThread.java:5227)
10-12 14:23:02.900: E/AndroidRuntime(23285):    at java.lang.reflect.Method.invokeNative(Native Method)
10-12 14:23:02.900: E/AndroidRuntime(23285):    at java.lang.reflect.Method.invoke(Method.java:511)
10-12 14:23:02.900: E/AndroidRuntime(23285):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
10-12 14:23:02.900: E/AndroidRuntime(23285):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
10-12 14:23:02.900: E/AndroidRuntime(23285):    at dalvik.system.NativeStart.main(Native Method)

EDIT #2:

{List of lists:
{Titles list(Strings):bla,bla,bla},
{desc list(Strings): bla, bla,bla},
{another list(Strings): bla, bla,bla},
}

here's a pic

image

Upvotes: 1

Views: 355

Answers (2)

Vickyexpert
Vickyexpert

Reputation: 3167

Please check below code if it is working

I think Your Items list has no more data which you rae going to access

Check Below code:

    title.setText("      "+items.get(position).get(0));
    views.setText(items.get(position).get(1));
    likes.setText(items.get(position).get(2));
    upvote.setText(items.get(position).get(3));
    downvote.setText(items.get(position).get(4));
    desc.setText(items.get(position).get(5));
    pub.setText(items.get(position).get(6));
    idnum.setText(items.get(position).get(7));

May be list size is small and returns null so check this one.

Upvotes: 1

CommonsWare
CommonsWare

Reputation: 1006574

You are chaining to the wrong constructor, and your class is mis-declared.

Change:

public class MobileArrayAdapter extends ArrayAdapter<String>

to:

public class MobileArrayAdapter extends ArrayAdapter<List<String>>

And change:

super(context, R.layout.list_item);

to:

super(context, R.layout.list_item, 0, items);

You can then dump your own items data member and adjust getView() accordingly.

What is happening is that ArrayAdapter knows nothing of your List<String>, and so therefore it will return getCount() of 0.

The 0 in the super() call here is safe because you are not chaining to the superclass, electing to handle everything in getView() yourself.

However, that points out issues in your getView() implementation:

  • Please recycle your rows, by using convertView if it is not null

  • Consider using getActivity().getLayoutInflater() instead of always creating a new one, in case the activity either caches a LayoutInflater or supplies you with a specialized one

  • " "+items.get(1).get(position) is probably not a good idea, compared to designing your row layout such that this TextView is positioned where you want it to be

  • Given how many widgets are in your row, please consider using the ViewHolder pattern to cache those findViewById() calls

Upvotes: 1

Related Questions