必须使用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;
}
}