Android ListView嵌套ListView,正确触发子ListView中的被点击项


必须使用parent获取被点击的子ListView中的项



package cn.example.doublelistviewdemo;

import java.util.List;
 
 

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class Fadapter extends BaseAdapter { 
	protected static final String TAG = "Fadapter"; 
	
	private List<LvModel> lvList;
	private Context context;
	private Cadapter cadapter;

	public Fadapter(List<LvModel> lvList, Context context) {
		super();
		this.lvList = lvList;
		this.context = context;
	}

	@Override
	public int getCount() {
		return lvList.size();
	}

	@Override
	public LvModel getItem(int position) {
		return lvList.get(position);
	}

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

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View itemView = View.inflate(context, R.layout.fitem, null);
		TextView tv_name=(TextView) itemView.findViewById(R.id.tv_fname);
		ListView lv_cList=(ListView) itemView.findViewById(R.id.lv_cList);//子ListView
		
		tv_name.setText(getItem(position).getName()); 
		
		
		lv_cList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
				//错误的方式,无论点击哪个子ListView中的项,只能触发当前getView中的项
				//Log.i(TAG, "data-----------------------------------------" + cadapter.getItem(position));
				
				//必须使用parent获取被点击的子ListView中的项
				Log.i(TAG, "data-----------------------------------------" + parent.getItemAtPosition(position)); 
				
			}
		}); 
		
		cadapter=new Cadapter(getItem(position).getcName(), context);
		lv_cList.setAdapter(cadapter);
		ListVewUtils.setListViewHeightBasedOnChildren(lv_cList);
		 
		return itemView;
	}
 

}


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值