获取数组在Onclick中的位置

时间:2018-07-19 14:02:44

标签: android arrays listview azure-sql-database

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

在当前课程中声明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);弄错了我的错误

1 个答案:

答案 0 :(得分:1)

fromWhere是一个数组,并且没有.get()方法,因为您正在使用数组,则必须像formWhere[position]一样直接通过索引访问值。使用arrayList而不是普通数组,则可以使用.get()方法