Android 如何从 ListView 中的自定义适配器获取数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24034754/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get data from custom adapter in ListView?
提问by Kevin Gilles
here is my problem :
这是我的问题:
I created a custom adapter for my ListView and I get information out of a List that I give to that adapter in order to let him fulfill my ListView. He does everything correctly. Now I'd like to implement an OnItemClickListenerand to get the data from it. But in that list I have 4 TextViews ...
我为我的 ListView 创建了一个自定义适配器,并从我提供给该适配器的 List 中获取信息,以便让他完成我的 ListView。他做的一切都是正确的。现在我想实现一个OnItemClickListener并从中获取数据。但是在那个列表中我有 4 个 TextViews ...
How can I retrieve the data?
如何检索数据?
I already know how to get the id and position since I display them in a Toast.
我已经知道如何获取 ID 和位置,因为我将它们显示在 Toast 中。
Thanks a lot for your help !
非常感谢你的帮助 !
Here is the custom Adapter :
这是自定义适配器:
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;
}
}
Here is my Fragment that posess the ListView :
这是我的构成 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) {
}
}
回答by Blackbelt
inside onItemClick
you can use the AdapterView
argument,
在里面onItemClick
你可以使用AdapterView
参数,
NextRdv item = (NextRdv) parent.getItemAtPosition(position)
回答by Neha Shukla
You can use below code
您可以使用以下代码
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();
}
});
using this u can get the data of any TextView inside custom adapter for selected row.
使用它,您可以获取所选行的自定义适配器内的任何 TextView 的数据。
Hope will help you...
希望能帮到你...