我引用了一个教程并在 fragment 中创建了一个自定义 ListView ,但是我无法在 ListView 的项目点击中获取任何项目的位置或值。我想显示一个包含项目位置的 toast 。我不知道如何实现它。
自定义列表适配器.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
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;
}
最佳答案
被点击项目在 ListView
中的位置可以在 onItemClick
上轻松检索您可以在文档中看到的方法:
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.
所以在您的例子中,被点击项目的位置是 arg2 值。你的代码应该是这样的:
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Toast.makeText(getActivity(), arg2+"", Toast.LENGTH_LONG).show();
/*...*/
}
编辑:
要获取元素,您可以这样做:
(Item) arg0.getAdapter().getItem(arg2);
长话短说
在这种情况下,您可以看到为参数使用好的名称很重要。使用 Eclipse 或其他 IDE 给出的默认名称,这一行:
(Item) arg0.getAdapter().getItem(arg2);
看起来很奇怪。但是,如果您使用的是 Android 名称 - 这会为您提供 onItemClick(AdapterView<Item> parent, View view, int position, long id)
- 该行变为:
(Item) parent.getAdapter().getItem(position);
哪个更清晰易用。
关于java - 如何在android中的 ListView 中获取项目值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24250280/