如何在列表视图中获取所选项目的值 - Android

Posted

技术标签:

【中文标题】如何在列表视图中获取所选项目的值 - Android【英文标题】:How to get value(s) of selected Item in listview - Android 【发布时间】:2018-07-14 14:18:57 【问题描述】:

我刚接触 android (Java),我想获取用户从我的自定义 ListView 中选择的一个或多个项目的值,下面是我试图检索数据的示例代码

ContactsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() 
    public void onItemClick(AdapterView<?> parent, View view,int position, long id) 

       // String str= ContactsListView.getSelectedItem();
        //String selectedFromList = (ContactsListView.getItemAtPosition(position).toString());
        String val =  parent.getAdapter().getItem(position).toString();
        Log.i("Item", "Selected: " + val);       
    
);

但这就是我得到的结果

02-04 18:16:31.773 10255-10255/com.app.com.sms I/项目:已选择: com.app.com.smsapp.ThreeStrings@42829718 02-04 18:16:33.323 10255-10255/com.app.com.sms I/项目: 已选择: com.app.com.smsapp.ThreeStrings@428297c0 02-04 18:16:34.463 10255-10255/com.app.com.sms I/项目: 已选择: com.app.com.smsapp.ThreeStrings@42829718

这是我的适配器

public class ThreeHorizontalTextViewsAdapter extends
ArrayAdapter<ThreeStrings> 

private int layoutResource;

public ThreeHorizontalTextViewsAdapter(Context context, int layoutResource, List<ThreeStrings> threeStringsList) 
    super(context, layoutResource, threeStringsList);
    this.layoutResource = layoutResource;


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

    View view = convertView;

    if (view == null) 
        LayoutInflater layoutInflater = LayoutInflater.from(getContext());
        view = layoutInflater.inflate(layoutResource, null);
    

    ThreeStrings threeStrings = getItem(position);

    if (threeStrings != null) 
        TextView leftTextView = (TextView) view.findViewById(R.id.leftTextView);
        TextView rightTextView = (TextView) view.findViewById(R.id.rightTextView);
        TextView centreTextView = (TextView) view.findViewById(R.id.centreTextView);
        TextView text_from = (TextView) view.findViewById(R.id.text_from);

        if (leftTextView != null) 
            leftTextView.setText(threeStrings.getLeft().toUpperCase());
        

        if (rightTextView != null) 
            rightTextView.setText(threeStrings.getRight());
        

        if (centreTextView != null) 
            centreTextView.setText(threeStrings.getCentre());
        

        if (text_from != null) 
            text_from.setText(threeStrings.getBottom());
        
    

    return view;
 
public class ThreeStrings 
    private String left;
    private String right;
    private String centre;
    private String bottom;

    public ThreeStrings(String left, String right, String centre, String bottom) 
        this.left   = left;
        this.right  = right;
        this.centre = centre;
        this.bottom = bottom;
    
    public String getLeft() 
        return left;
    
    public String getCentre() 
        return centre;
    
    public String getRight() 
        return right;
    
    public String getBottom() return bottom;

在 listView 中将 center 定位为选定值。我如何获得所选项目的值/中心?

【问题讨论】:

【参考方案1】:

你可以在下面做这样的事情:-

ContactsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() 
    public void onItemClick(AdapterView<?> parent, View view,int position, long id) 

      ThreeStrings threeStrings = (ThreeStrings)ContactsListView.getItemAtPosition(position);

        Log.i("Item", "Selected: " + threeStrings.getCentre());       
    
);

【讨论】:

@JoDee 有效吗?如果是,请接受已回答。谢谢【参考方案2】:
String val = [yourList].get(position).getCentre();

【讨论】:

以上是关于如何在列表视图中获取所选项目的值 - Android的主要内容,如果未能解决你的问题,请参考以下文章

如何从listview android的主列表中获取所选项目的文本

获取列表框中所选项目的值作为字符串

如何从黑莓10级联中的复选框选定项目中获取值..?

无法刷新 Spinner 所选项目的 ListView 以从 url 获取新数据

从数据库中获取数据的下拉列表中选择值

jquery在下拉菜单中找到所选项目的值