作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
这是我的问题:
我为我的 ListView 创建了一个自定义适配器,我从我提供给该适配器的列表中获取信息,以便他完成我的 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 的 fragment :
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) {
}
}
最佳答案
在 onItemClick
中,您可以使用 AdapterView
参数,
NextRdv item = (NextRdv) parent.getItemAtPosition(position)
关于android - 如何从 ListView 中的自定义适配器获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24034754/
我是一名优秀的程序员,十分优秀!