============问题描述============
package com.example.one_listview; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; public class MainActivity extends Activity { private ListView lv; private List<Map<String, Object>> data; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); lv = (ListView)findViewById(R.id.lv); data = getData(); MyAdapter adapter = new MyAdapter(this); lv.setAdapter(adapter); } private List<Map<String, Object>> getData() { List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); Map<String, Object> map = new HashMap<String, Object>(); map.put("title", "校园回忆"); map.put("info", "青春是人生的实验课,错也错得很值得。from 《笑忘歌》"); map.put("img", R.drawable.one_1); list.add(map); map = new HashMap<String, Object>(); map.put("title", "迷离者"); map.put("info", "若不问来路与去程,人生就容易得多。by 陶立夏"); map.put("img", R.drawable.one_2); list.add(map); map = new HashMap<String, Object>(); map.put("title", "孤独地像一颗星球"); map.put("info", "所有的悲伤,总会留下一丝欢乐的线索。所有的遗憾,总会留下一处完美的角落。我在冰封的深海,找寻希望的缺口。却在午夜惊醒时,蓦然瞥见绝美的月光。from 《缺口》"); map.put("img", R.drawable.one_3); list.add(map); map = new HashMap<String, Object>(); map.put("title", "在我成为独行侠之前"); map.put("info", "当我沉默地面对着你,你又怎么知道我曾在心里对你说了多少话。当我一成不变地站在你面前,你又怎么知道我内心早已为你千回百转。 by 里则林"); map.put("img", R.drawable.one_4); list.add(map); return list; } static class ViewHolder { public ImageView img; public TextView title; public TextView info; } public class MyAdapter extends BaseAdapter { private LayoutInflater mInflater = null; private MyAdapter(Context context) { this.mInflater = LayoutInflater.from(context); } 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) { ViewHolder holder = null; if(convertView == null) { holder = new ViewHolder(); convertView = mInflater.inflate(R.layout.list_item, null); holder.img = (ImageView)convertView.findViewById(R.id.img); holder.title = (TextView)convertView.findViewById(R.id.tv); holder.info = (TextView)convertView.findViewById(R.id.info); convertView.setTag(holder); } else { holder = (ViewHolder)convertView.getTag(); } holder.img.setBackgroundResource((Integer)data.get(position).get("img")); holder.title.setText((String)data.get(position).get("title")); holder.info.setText((String)data.get(position).get("info")); return convertView; lv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?>parent , View view , int position , long id) { //想不明白了。如何补充。 } }); } } }
============解决方案1============
lv.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?>parent , View view , int position , long id)
{
//想不明白了。如何补充。
在这里面直接打开activity就可以了 如果想传递当前item的数据可以 使用data.get(position)来获取当前item的数据
}
});
============解决方案2============
lv.setOnItemClickListener
写在onCreate里
============解决方案3============
onItemClick这个不要写在Adapter里。。如众楼上所说,加个item的listener就行了,在监听器里override一个oncreat,在oncreat里加上intent就行了
============解决方案4============
监听里面加上Intent()直接进行跳转就可以了
时间: 2024-11-04 17:09:13