如何将 Selected Listview 项的值转换为 String?

How to get value of Selected Listview item to String?

我有一个自定义的ListView,里面的数据来自SQLite数据库。 我已经使用 Cursor 和 Simple Cursor Adapter 来填充列表视图。

设置 OnItemClickListener 后,我无法将所选项目的值获取到字符串中。我试过 lv.getSelectedItemPosition(position) 但它不起作用。 有人可以帮忙吗?

适配器Class代码

public class AllBankBranch_adapter extends BaseAdapter {

Activity macactivity;
ArrayList<all_bankbranch> list2;

public AllBankBranch_adapter(Activity activity, ArrayList<all_bankbranch> list){
    super();
    this.macactivity = activity;
    list2 = list;
}

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

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

@Override
public long getItemId(int position) {
    return 0;
}
private class ViewHolder{

    TextView bankbranchcity;
}

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

    final ViewHolder holder;
    LayoutInflater inflater = macactivity.getLayoutInflater();
    convertView = inflater.inflate(R.layout.engineeringcollbankbranch_listview,null);
    holder = new ViewHolder();
    holder.bankbranchcity = (TextView) convertView.findViewById(R.id.tv_enggcoll_bankbranch_city);
    holder.bankbranchcity.setText(list2.get(position).getBankCity());
    return convertView;
}

}

正在从数据库中检索数据:

 public List<all_bankbranch> getallbank(String city){

    List<all_bankbranch> banklist = new ArrayList<all_bankbranch>();
    String query = "Select BankBranch, BankAddress from all_bankbranch where BankCity = " + city;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(query,null);

    if(cursor.moveToFirst()){
        do{
            all_bankbranch bbracnh = new all_bankbranch();
            bbracnh.setBankBranch(cursor.getString(2));
            bbracnh.setBankAddress(cursor.getString(3));

            banklist.add(bbracnh);
        }
        while (cursor.moveToNext());
    }
    return banklist;
}

MainActivity 代码

 ListView allbankbranch;
Cursor curbb;
DB_AllBankBranch DABB;
SimpleCursorAdapter scabb;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_engg_college_bank_branch);

    DABB = new DB_AllBankBranch(this);
    banksearchbar = (EditText) findViewById(R.id.et_enggcoll_bankbranch_searchbar);
    curbb = DABB.select_bankcity();
    allbankbranch = (ListView) findViewById(R.id.lv_enggcoll_bankbranch);

    String[] columns = new String[]{"BankCity"};
    int[] to = new int[]{R.id.tv_enggcoll_bankbranch_city};

    scabb = new SimpleCursorAdapter(this, R.layout.engineeringcollbankbranch_listview, curbb, columns, to);
    if (curbb.getCount() > 0) {
        allbankbranch.setAdapter(scabb);
    }
 allbankbranch.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


String s = allbankbranch.getItemAtPosition(position);

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

            }
        });

在项目点击侦听器上使用,项目选择侦听器不适用于列表视图。它仅适用于微调器。

String selectedFromList = lv.getItemAtPosition(position);

根据您的要求,

给你..

如果你 row.xml 有多个视图 ,

如果您希望在单击您的列表视图位置后为特定视图分配数据..

字符串名称=((TextView)view.findViewById(R.id.price_name)).getText().toString();

您定义的名称具有该值, 你会变成这样 ,

尝试...希望它会起作用

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> adapter, View v, int position,long arg3)
        {

           String s = listView.getItemAtPosition(position).toString();
           //or:
           String ss = (String)adapter.getItemAtPosition(position);
        }
    });
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) 

{ 字符串名称 =((TextView)view.findViewById(R.id.price_name)).getText().toString();

Log.d("string",姓名);

        }
    });