0

这是我的列表适配器:

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;

    }


}

当我单击 listView 的一项时,应用程序崩溃..

日志猫:

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)

在第 94 行:

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

整个方法:

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

}

这里有什么问题?泰

4

2 回答 2

0

你可以这样做:

@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();
/.....
}
于 2013-10-12T15:42:39.687 回答
0

只需更改为

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

或尝试覆盖getItem()并返回该项目。

编辑

我不确定您的数据,但您可以在 getItem() 中返回您的数据对象,如下所示

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

这只是一个示例.. 如果您getListAdapter().getItem(position)现在调用,它将返回 123 string ,您可以根据您的数据要求对其进行修改。

于 2013-10-12T15:54:54.730 回答