java - 如何在android中的 ListView 中获取项目值?

标签 java android listview

我引用了一个教程并在 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/

相关文章:

Android多个屏幕尺寸具有相同的密度

Android TextView 使 http 和自定义方案 url 可点击

java - Android 上的 HighChart 动态更新

android - 从代码而非 xml 创建选择器时出现意外结果

java - 为什么使用Behavior渲染textField需要这么长时间

java - Android 更改CameraOnFly

android - ExpandableListView中的SwipeListView,可以吗?

java - Android 自定义 ListView 文本过滤器不起作用

Java:将属性声明为 Java 给定类型或自编写类型(也许带有接口(interface)?)

java - 跑表。数秒。 javafx