1

我正在使用 LazyAdapter 来显示列表视图,并且我想在单击项目时将值传递给另一个活动。但是我无法检索该值。请帮帮我。MainActivity.java

        list=(ListView)findViewById(R.id.list);
        //transparent so when scrolling listview won't turn black.
        list.setCacheColorHint(Color.TRANSPARENT); 
         // Getting adapter by passing JSON data ArrayList
         adapter=new LazyAdapter(ReviewShowActivity.this, reviewlist);
         list.setAdapter(adapter);

         list.setOnItemClickListener(new OnItemClickListener() {

            @SuppressWarnings("unchecked")
            @Override
             public void onItemClick(AdapterView<?> parent, View view,
                     int position, long id) {

                //Having Trouble with this line, how to retrieve value???
                HashMap<String, String> map2 = (HashMap<String, String>) list.getAdapter().getItem(position);

                Intent in = new Intent(getApplicationContext(), SingleReviewActivity.class);
                in.putExtra(TAG_NAME, map2.get(TAG_NAME));
                in.putExtra(TAG_USERREVIEW, map2.get(TAG_USERREVIEW));
                in.putExtra(TAG_DATE, map2.get(TAG_DATE));
                in.putExtra(TAG_THUMBURL, map2.get(TAG_THUMBURL));
                startActivity(in);
             }
         });

LazyAdapter.java

 public class LazyAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader; 

public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
    activity = a;
    data=d;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader=new ImageLoader(activity.getApplicationContext());
}

public int getCount() {
    return data.size();
}

public Object getItem(int position) {
    return position;
}

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

public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.reviewshow, null);

    TextView username = (TextView)vi.findViewById(R.id.username); // title
    TextView userreview = (TextView)vi.findViewById(R.id.userreview); // artist name
    TextView numlikes = (TextView)vi.findViewById(R.id.numlikes); // duration
    HashMap<String, String> map = new HashMap<String, String>();
    map = data.get(position);

     //Setting all values in listview
     username.setText(map.get(ReviewShowActivity.TAG_NAME));
     userreview.setText(map.get(ReviewShowActivity.TAG_USERREVIEW));
     numlikes.setText(map.get(ReviewShowActivity.TAG_NUMLIKES));
    return vi;
}

日志猫

04-13 04:14:36.135: E/AndroidRuntime(23909): FATAL EXCEPTION: main
04-13 04:14:36.135: E/AndroidRuntime(23909): java.lang.ClassCastException:     java.lang.Integer
04-13 04:14:36.135: E/AndroidRuntime(23909):    at org.example.veniew.ReviewShowActivity$ReviewTask$1.onItemClick(ReviewShowActivity.java:221)
04-13 04:14:36.135: E/AndroidRuntime(23909):    at  android.widget.AdapterView.performItemClick(AdapterView.java:284)
04-13 04:14:36.135: E/AndroidRuntime(23909):    at android.widget.ListView.performItemClick(ListView.java:3561)
04-13 04:14:36.135: E/AndroidRuntime(23909):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
04-13 04:14:36.135: E/AndroidRuntime(23909):    at android.os.Handler.handleCallback(Handler.java:587)
04-13 04:14:36.135: E/AndroidRuntime(23909):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-13 04:14:36.135: E/AndroidRuntime(23909):    at android.os.Looper.loop(Looper.java:143)
04-13 04:14:36.135: E/AndroidRuntime(23909):    at android.app.ActivityThread.main(ActivityThread.java:4277)
04-13 04:14:36.135: E/AndroidRuntime(23909):    at java.lang.reflect.Method.invokeNative(Native Method)
04-13 04:14:36.135: E/AndroidRuntime(23909):    at java.lang.reflect.Method.invoke(Method.java:507)
04-13 04:14:36.135: E/AndroidRuntime(23909):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-13 04:14:36.135: E/AndroidRuntime(23909):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-13 04:14:36.135: E/AndroidRuntime(23909):    at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

1

这是面对类似问题的人的答案。

public Object getItem(int position) { 
return data.get(position); 
}
于 2012-04-13T09:54:51.533 回答
0

也许此链接可以帮助您获取数据数组列表哈希图,将此代码放在 setonitemclicklistener

关联

于 2015-08-28T10:00:02.020 回答