Question

This is my list adapter:

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<List<item>> {
    private final Context context;
    private final List<item> markers;
    private final static String[] a={"s"};

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

    @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);

        title.setText("      "+markers.get(position).getTitle());
        views.setText(markers.get(position).getView()+"");
        likes.setText(markers.get(position).getLike()+"");
        upvote.setText(markers.get(position).getUpvote()+"");
        downvote.setText(markers.get(position).getDownvote()+"");
        desc.setText(markers.get(position).getDesc());
        pub.setText(markers.get(position).getPub()+"");
        idnum.setText(markers.get(position).getId()+"");

        return rowView;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return (this.markers != null) ? this.markers.size() : 0;

    }


}

The app crashes when I click on one of the listView's items..

LogCat:

10-12 18:20:49.791: W/dalvikvm(3450): threadid=1: thread exiting with uncaught exception (group=0x4107b930)
10-12 18:20:49.801: E/AndroidRuntime(3450): FATAL EXCEPTION: main
10-12 18:20:49.801: E/AndroidRuntime(3450): java.lang.IndexOutOfBoundsException: Invalid index 2, size is 0
10-12 18:20:49.801: E/AndroidRuntime(3450):     at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
10-12 18:20:49.801: E/AndroidRuntime(3450):     at java.util.ArrayList.get(ArrayList.java:304)
10-12 18:20:49.801: E/AndroidRuntime(3450):     at android.widget.ArrayAdapter.getItem(ArrayAdapter.java:337)
10-12 18:20:49.801: E/AndroidRuntime(3450):     at com.example.free.ListActivity.onListItemClick(ListActivity.java:94)
10-12 18:20:49.801: E/AndroidRuntime(3450):     at android.support.v4.app.ListFragment$2.onItemClick(ListFragment.java:58)
10-12 18:20:49.801: E/AndroidRuntime(3450):     at android.widget.AdapterView.performItemClick(AdapterView.java:298)
10-12 18:20:49.801: E/AndroidRuntime(3450):     at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
10-12 18:20:49.801: E/AndroidRuntime(3450):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
10-12 18:20:49.801: E/AndroidRuntime(3450):     at android.widget.AbsListView$1.run(AbsListView.java:3423)
10-12 18:20:49.801: E/AndroidRuntime(3450):     at android.os.Handler.handleCallback(Handler.java:725)
10-12 18:20:49.801: E/AndroidRuntime(3450):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-12 18:20:49.801: E/AndroidRuntime(3450):     at android.os.Looper.loop(Looper.java:137)
10-12 18:20:49.801: E/AndroidRuntime(3450):     at android.app.ActivityThread.main(ActivityThread.java:5227)
10-12 18:20:49.801: E/AndroidRuntime(3450):     at java.lang.reflect.Method.invokeNative(Native Method)
10-12 18:20:49.801: E/AndroidRuntime(3450):     at java.lang.reflect.Method.invoke(Method.java:511)
10-12 18:20:49.801: E/AndroidRuntime(3450):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
10-12 18:20:49.801: E/AndroidRuntime(3450):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
10-12 18:20:49.801: E/AndroidRuntime(3450):     at dalvik.system.NativeStart.main(Native Method)

On line 94:

String selectedValue = (String) getListAdapter().getItem(position);

The whole method:

@Override
public void onListItemClick(ListView l, View v, int position, long id) {

    // get selected items
    String selectedValue = (String) getListAdapter().getItem(position);
    Toast.makeText(getActivity(), selectedValue, Toast.LENGTH_SHORT).show();

}

What's the problem here? ty

Was it helpful?

Solution

Simply change to

//String selectedValue = (String) getListAdapter().getItem(position);
String selectedValue = markers.get(position).getTitle();

or try overriding getItem() and return the item there.

Edit

I am not sure of your data but you can return your data object in getItem() something like this

@Override
    public Object getItem(int position) {
        return "123";
    }

this is only a sample.. if you will call getListAdapter().getItem(position) now, it will return 123 string , you can modify it to your requirement of data.

OTHER TIPS

You can do this:

@Override
public void onListItemClick(ListView l, View v, int position, long id) {

    TextView title = (TextView) v.findViewById(R.id.title);
    ProgressBar loader = (ProgressBar) v.findViewById(R.id.loader);
    ImageView image = (ImageView) v.findViewById(R.id.itemimgae);
    TextView views = (TextView) v.findViewById(R.id.views);
    TextView likes=(TextView) v.findViewById(R.id.likes);
    TextView upvote=(TextView) v.findViewById(R.id.upvote);
    TextView downvote=(TextView) v.findViewById(R.id.downvote);
    TextView desc=(TextView) v.findViewById(R.id.desc);
    TextView pub =(TextView) v.findViewById(R.id.pub);
    TextView idnum =(TextView) v.findViewById(R.id.idnum);

//than use the values
    String stLikes = likes.getText().toString();
/.....
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top