1
我正在使用LazyAdapter來顯示列表視圖,並且我想在單擊項目時將值傳遞給另一個活動。不過,我無法檢索該值。請幫助我。 MainActivity.java使用LazyAdapter和getAdapter()檢索值的問題
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;
}
logcat的
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)
什麼的listAdapter – L7ColWinters 2012-04-12 20:34:45
的方法的getItem我更新了lazyadapter完整的代碼。 – Eric 2012-04-12 20:43:39
你現在看到你的問題了嗎?大聲笑getItem返回一個整數適配器中的位置不是項目... – L7ColWinters 2012-04-12 20:45:55