获取listView onClick中rowItem的名称

时间:2018-12-03 15:50:18

标签: java android listview onitemclicklistener

我正在尝试使用listView功能实现简单的onClick。在单击listView中的项目时,我想在'toast消息中显示该项目的名称,但是我无法做到这一点。我使用了此linklink中建议的方法,但无法获得所需的结果。我在下面添加了我的代码:

MainActivity

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        rowItem = new AppList();
        final ListView userInstalledApps = (ListView)findViewById(R.id.appListView);

        List<AppList> installedApps = getInstalledApps();
        CustomAppAdapter installedAppAdapter = new CustomAppAdapter(MainActivity.this, installedApps);
        userInstalledApps.setAdapter(installedAppAdapter);

        userInstalledApps.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                TextView appName = (TextView)adapterView.findViewById(R.id.list_app_name);
                Toast.makeText(getApplicationContext(),
                        "Clicked "+ adapterView.getItemAtPosition(i),Toast.LENGTH_LONG).show();

            }
        });
    }

CustomAppAdapter.java

    public class CustomAppAdapter extends BaseAdapter {

    private LayoutInflater layoutInflater;
    private List<AppList> listStorage;
    Context context;

    public CustomAppAdapter(Context context, List<AppList> listStorage) {
        layoutInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.listStorage = listStorage;
    }

    @Override
    public int getCount() {
        return listStorage.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder listViewHolder;
        if(convertView == null){
            listViewHolder = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.installed_app_list, parent, false);

            listViewHolder.textInListView = (TextView)convertView.findViewById(R.id.list_app_name);
            listViewHolder.imageInListView = (ImageView)convertView.findViewById(R.id.app_icon);
            convertView.setTag(listViewHolder);
        }else{
            listViewHolder = (ViewHolder)convertView.getTag();
        }
        listViewHolder.textInListView.setText(listStorage.get(position).getName());
        listViewHolder.imageInListView.setImageDrawable(listStorage.get(position).getIcon());

        return convertView;
    }

    static class ViewHolder{

        TextView textInListView;
        ImageView imageInListView;
    }
}

下面是listView的屏幕截图:ListView

尝试此方法:

Toast.makeText(getApplicationContext(),
                    "Clicked "+ adapterView.getItemAtPosition(i),Toast.LENGTH_LONG).show();

假设我单击了listView中的第二项,所以我收到了一条toast的消息Clicked 2。但我希望它向我显示Clicked PhonePe

1 个答案:

答案 0 :(得分:1)

Type

尝试一下。