繁体   English   中英

如何在listview中获取所选项目的价值 - Android

[英]How to get value(s) of selected Item in listview - Android

我刚接触Android(Java),我想从我的Custom 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 / Item:Selected:com.app.com.smsapp.ThreeStrings@42829718 02-04 18:16:33.323 10255-10255 / com。 app.com.sms I / Item:Selected:com.app.com.smsapp.ThreeStrings@428297c0 02-04 18:16:34.463 10255-10255 / com.app.com.sms I / Item:Selected: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 am target center中作为选定值。 我如何获得所选项目的价值/中心?

您可以执行以下操作: -

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());       
    }
});
String val = [yourList].get(position).getCentre();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM