Java 如何在android的列表视图中获取项目值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24250280/
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 item value in a listview in android?
提问by
I have referenced to a tutorial and I have created a custom list view in fragment, but I am unable to get the position or value of any item on item click of list view. I want to show a toast containing the position of the item. I don't know how to implement that.
我参考了一个教程,并在片段中创建了一个自定义列表视图,但是我无法在单击列表视图的项目时获取任何项目的位置或值。我想显示一个包含项目位置的吐司。我不知道如何实施。
CustomListAdapter.java
自定义列表适配器.java
public class CustomListAdapter extends BaseAdapter{
private ArrayList<MyActivityAdapterItem> listData;
private Context context;
//private LayoutInflater layoutInflater;
public CustomListAdapter(Context context, ArrayList<MyActivityAdapterItem> listData) {
this.listData = listData;
this.context = context;
}
@Override
public int getCount() {
return listData.size();
}
@Override
public Object getItem(int position) {
return listData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
//ViewHolder holder;
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.list_row_layout, null);
// holder = new ViewHolder();
//convertView.setTag(holder);
}
TextView headingView = (TextView) convertView.findViewById(R.id.title);
TextView placeView = (TextView) convertView.findViewById(R.id.place);
TextView reportedDateView = (TextView) convertView.findViewById(R.id.date);
headingView.setText(listData.get(position).getHeading());
placeView.setText("At, " + listData.get(position).getPlace());
reportedDateView.setText(listData.get(position).getDate());
return convertView;
}
}
FindPeopleFragment.java
FindPeopleFragment.java
public class FindPeopleFragment extends Fragment implements OnClickListener {
AlertDialog.Builder builder ;
ListView lv1;
ImageButton delall;
CharSequence options[] = new CharSequence[] {"Delete"};
ArrayList details;
CustomListAdapter ad1;
String msg="abc";
public FindPeopleFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_find_people, container, false);
delall=(ImageButton) rootView.findViewById(R.id.deleteall);
details = getListData();
lv1 = (ListView) rootView.findViewById(R.id.activitylist);
ad1=new CustomListAdapter(getActivity(), details);
lv1.setAdapter(ad1);
builder= new AlertDialog.Builder(getActivity());
delall.setOnClickListener(this);
lv1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
//Toast.makeText(getActivity(), , Toast.LENGTH_LONG).show();
/*Bundle args = new Bundle();
args.putString(MyActivitiesFragment.MSG, msg);
MyActivitiesFragment f1=new MyActivitiesFragment();
f1.setArguments(args);
MainActivity.fm.beginTransaction().replace(R.id.frame_container,f1).commit();*/
}
});
return rootView;
}
采纳答案by G.T.
The position of the clicked item in a ListView
can be retrived easily on the onItemClick
method as you can see in the documentation:
如您在文档中所见,可以ListView
在onItemClick
方法上轻松检索a 中单击项目的位置:
public abstract void onItemClick (AdapterView parent, View view, int position, long id)
Callback method to be invoked when an item in this AdapterView has been clicked.
Implementers can call getItemAtPosition(position) if they need to access the data associated with the selected item.
Parameters parent The AdapterView where the click happened. view The view within the AdapterView that was clicked (this will be a view provided by the adapter) position The position of the view in the adapter. id The row id of the item that was clicked.
public abstract void onItemClick (AdapterView parent, View view, int position, long id)
单击此 AdapterView 中的项目时要调用的回调方法。
如果实现者需要访问与所选项目关联的数据,他们可以调用 getItemAtPosition(position)。
参数 parent 发生点击的 AdapterView。view AdapterView 中被点击的视图(这将是适配器提供的视图) position 视图在适配器中的位置。id 单击的项目的行 id。
So in your case, the position of the clicked item is the arg2value. Your code should be like this:
因此,在您的情况下,单击项目的位置是arg2值。你的代码应该是这样的:
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Toast.makeText(getActivity(), arg2+"", Toast.LENGTH_LONG).show();
/*...*/
}
EDIT:
编辑:
To get the Item, you can do like this:
要获取项目,您可以这样做:
(Item) arg0.getAdapter().getItem(arg2);
TL;DR
TL; 博士
You can see in this case that it is important to use good names for parameters. With the default names given by Eclipse - or by others IDEs - this line:
在这种情况下,您可以看到为参数使用好的名称很重要。使用 Eclipse - 或其他 IDE 提供的默认名称 - 这一行:
(Item) arg0.getAdapter().getItem(arg2);
looks pretty strange. But if you are using the Android names - which gives you onItemClick(AdapterView<Item> parent, View view, int position, long id)
- the line becomes:
看起来很奇怪。但是,如果您使用的是 Android 名称 - 这给了您onItemClick(AdapterView<Item> parent, View view, int position, long id)
- 该行变为:
(Item) parent.getAdapter().getItem(position);
Which is clearer and easier to use.
哪个更清晰,更容易使用。
回答by Javier
You are doing well in setOnItemClickListener the problem its the autocomplete its not working well in parameters you should have something like this: lv1.setOnItemClickListener(new OnItemClickListener() {
你在 setOnItemClickListener 中做得很好 问题是它的自动完成它在参数中不能很好地工作你应该有这样的东西: lv1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
}
});
the position is in your arg2, and as a tip you can get the item you click using this snippet:
该位置在您的 arg2 中,作为提示,您可以使用以下代码段获取您单击的项目:
parent.getAdapter().getItem(position);
回答by Guy S
this should work:
这应该有效:
lv1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Toast.makeText(getApplicationContext(), "Selected item at position: " + position, Toast.LENGTH_LONG).show();
}
}
回答by yassino
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Intent i1 = new Intent(getApplicationContext(), Plaeeng.class);
// List<ScanResult> wifiList;
// i1.putExtra("cuor", );
// startActivity(i1);
Toast.makeText(ListCours.this, "=>"+parent.getAdapter().getItem(position), 5000).show();
}
});