这是我的问题:
我为ListView创建了一个自定义适配器,我从List中获取了一些信息,以便让他实现我的ListView。他做的一切都很正确。现在,我想实现 OnItemClickListener 并从中获取数据。但是在那个列表中我有4个TextViews ......
如何检索数据 ?
我已经知道如何获取id和位置,因为我在Toast中显示它们。
非常感谢您的帮助!
这是自定义适配器:
public class NextRdvAdapter extends ArrayAdapter<NextRdv> {
private List<NextRdv> listNextRdv = new ArrayList<NextRdv>();
private Context context;
public NextRdvAdapter(List<NextRdv> nextRdvList, Context ctx) {
super(ctx, R.layout.list_next_rdv, nextRdvList);
this.listNextRdv = nextRdvList;
this.context = ctx;
}
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) { // This a new view we inflate the new layout
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_next_rdv, parent, false);
} // Now we can fill the layout with the right values
TextView tvTimeStart = (TextView) convertView.findViewById(R.id.next_time_start);
TextView tvTimeEnd = (TextView) convertView.findViewById(R.id.next_time_end);
TextView tvEnterprise = (TextView) convertView.findViewById(R.id.next_enterprise_name);
TextView tvAddress = (TextView) convertView.findViewById(R.id.next_address);
NextRdv rdv = listNextRdv.get(position);
tvTimeStart.setText(rdv.getStartTime());
tvTimeEnd.setText(rdv.getEndTime());
tvEnterprise.setText(rdv.getEnterprise());
tvAddress.setText(rdv.getAddress());
return convertView;
}
}
这是我的片段,它具有ListView:
public class HomeFragment extends Fragment implements OnItemClickListener{
float density;
//Everything you need about this rdv
private ListView lvThisRdv;
private List<NextRdv> listThisRdv = new ArrayList<NextRdv>();
private NextRdvAdapter ThisRdvAdapter;
private NextRdvListBouchon thisBouchon;
//Everything you need about next rdv
private ListView lvNextRdv;
private List<NextRdv> listNextRdv = new ArrayList<NextRdv>();
private NextRdvAdapter nextRdvAdapter;
private NextRdvListBouchon bouchon;
public HomeFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
lvThisRdv = (ListView)rootView.findViewById(R.id.listView_this_RDV);
thisBouchon = new NextRdvListBouchon();
listThisRdv = thisBouchon.getBouchonInc();
ThisRdvAdapter = new NextRdvAdapter(listThisRdv, getActivity());
lvThisRdv.setAdapter(ThisRdvAdapter);
//Displaying the list of the next meetings
lvNextRdv = (ListView)rootView.findViewById(R.id.listView_next_RDV);
//Get test data
bouchon = new NextRdvListBouchon();
listNextRdv = bouchon.getBouchon();
nextRdvAdapter = new NextRdvAdapter(listNextRdv, getActivity());
lvNextRdv.setAdapter(nextRdvAdapter);
lvNextRdv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//
Toast toast = Toast.makeText(getActivity(), "Pos : " +position+ " / id : " + id, Toast.LENGTH_SHORT);
toast.show();
}
});
return rootView;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
}
}
答案 0 :(得分:12)
在onItemClick
内你可以使用AdapterView
参数
NextRdv item = (NextRdv) parent.getItemAtPosition(position)
答案 1 :(得分:7)
您可以使用以下代码
lvNextRdv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// selected item
String selected =((TextView)view.findViewById(R.id.your_textView_item_id)).getText().toString();
Toast toast=Toast.makeText(getApplicationContext(), selected, Toast.LENGTH_SHORT);
toast.show();
}
});
使用此功能,您可以获取所选行内自定义适配器内任何TextView的数据。
希望能帮到你......