【问题标题】:Get position of array in Onclick在 Onclick 中获取数组的位置
【发布时间】:2018-07-19 14:02:44
【问题描述】:

我正在尝试创建一个对话框,显示一些数据,这些数据是从另一个 java 类中检索到的,该类从我的 Azure SQL 数据库中获取。

在我当前的班级中声明我的String array 后,我尝试使用.get(position),但它给了我一个错误:Cannot resolve symbol get(int)

这是我的getData.java

public class GetData {
Connection connect;
String ConnectionResult = "";
Boolean isSuccess = false;


public List<Map<String,String>> doInBackground() {

    List<Map<String, String>> data = null;
    data = new ArrayList<Map<String, String>>();
    try
    {
        ConnectionHelper conStr=new ConnectionHelper();
        connect =conStr.connectionclass();        // Connect to database
        if (connect == null)
        {
            ConnectionResult = "Check Your Internet Access!";
        }
        else
        {
            // Change below query according to your own database.
            String query = "select * from cc_rail";
            Statement stmt = connect.createStatement();
            ResultSet rs = stmt.executeQuery(query);
            while (rs.next()){
                Map<String,String> datanum=new HashMap<String,String>();
                datanum.put("NAME",rs.getString("RAIL_NAME"));

                datanum.put("PRICE",rs.getString("RAIL_UNIT_PRICE"));

                datanum.put("RANGE",rs.getString("RAIL_RANGE"));

                datanum.put("SUPPLIER",rs.getString("RAIL_SUPPLIER"));

                datanum.put("SIZE",rs.getString("RAIL_SIZE"));
                data.add(datanum);
            }


            ConnectionResult = " successful";
            isSuccess=true;
            connect.close();
        }
    }
    catch (Exception ex)
    {
        isSuccess = false;
        ConnectionResult = ex.getMessage();
    }

    return data;
}

这是我的mainactivity.java,它填充了我的列表视图和我的对话框代码:

            List<Map<String,String>> MyData = null;
            GetValence mydata =new GetValence();
            MyData= mydata.doInBackground();
           final String[] fromwhere = { "NAME","PRICE","SIZE","SUPPLIER","RANGE" };

            int[] viewswhere = {R.id.Name_txtView , R.id.price_txtView,R.id.size_txtView};

            ADAhere = new SimpleAdapter(getActivity(), MyData,R.layout.list_valence, fromwhere, viewswhere);

            list.setAdapter(ADAhere);



    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        private ImageView icon;
        private String fullName,partyName;

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            /** To change selected state view */
            view.setSelected(true);
            String Slecteditem = fromwhere.get(position);

String Slecteditem = fromwhere.get(position); 收到我的错误

【问题讨论】:

  • 你遇到了什么错误??
  • @D-johnAnshani Cannot resolve symbol get(int)
  • @Mohammedis271 我已经添加了答案,请看一下

标签: android arrays listview azure-sql-database


【解决方案1】:

fromWhere 是一个数组,它没有.get() 方法,因为您使用的是数组,那么您必须像formWhere[position] 一样通过索引直接访问该值如果您想使用arrayList 而不是普通的数组那么你可以使用.get() 方法

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多