ListView中第一行的默认选择:Android

时间:2015-11-18 02:23:33

标签: android listview android-asynctask

我在自动选择或默认选择ListView中的第一行时遇到困难。请帮帮我

这是我的代码:

//OnLoad
public class Onload extends AsyncTask<String, String, String> {

    List<Map<String, String>> prolist  = new ArrayList<Map<String, String>>();
    String z = "";
    Boolean isSuccess = false;

    @Override
    protected void onPreExecute() {
        pbbar.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onPostExecute(String r) {
        if(isSuccess==true) {
            //display customer name
            pbbar.setVisibility(View.GONE);
            Toast.makeText(getApplicationContext(), r, Toast.LENGTH_SHORT).show();
            String[] from = {"A", "B"};
            int[] views = { R.id.lblproid};
            final SimpleAdapter ADA = new SimpleAdapter(workingStation.this,
                    prolist, R.layout.activity_lsttemplate, from,
                    views);
            lstpro.setAdapter(ADA);
            lstpro.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,
                                        int arg2, long arg3) {
                    HashMap<String, Object> obj = (HashMap<String, Object>) ADA
                            .getItem(0);
                    String userName = (String) obj.get("A");
                    String c_id = (String) obj.get("B");
                    edtUname.setText(userName);
                    edtUserID2.setText(c_id);
                }
            });
        }
    }

    @Override
    protected String doInBackground(String... params) {

        try {
            Connection con = connectionClass.CONN();
            if (con == null) {
                z = "Error in connection with SQL server!!!!";
            } else {
                SharedPreferences pref = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
                String win_name = pref.getString("window_number", "");
                String userName = pref.getString("username", "");
               // TextView window = (TextView) findViewById(R.id._try);
                //window.setText(win_name);
                String query = "SELECT TOP 1 customer_name, customer_id from customer_process where customer_window = '"+win_name+"' and customer_status = 'Pending' ";
                PreparedStatement ps = con.prepareStatement(query);
                ResultSet rs = ps.executeQuery();
                ArrayList<String> data1 = new ArrayList<String>();
                while (rs.next()) {
                    Map<String, String> datanum = new HashMap<String, String>();
                    datanum.put("A", rs.getString("customer_name"));
                    datanum.put("B", rs.getString("customer_id"));
                    prolist.add(datanum);
                }
                z = "Success";
                isSuccess = true;
            }
        } catch (Exception ex) {
            isSuccess = false;
            z = "Something went wrong!";
        }

        return z;
    }
} // end Onload

注意:我的代码工作正常我可以查看ListView。我想知道的是如何进行默认选择。感谢

1 个答案:

答案 0 :(得分:1)

修改 只需添加功能

public void filledData() {
       HashMap<String, Object> obj = (HashMap<String, Object>) ADA.getItem(0);
       String userName = (String) obj.get("A");
       String c_id = (String) obj.get("B");
       edtUname.setText(userName);
       edtUserID2.setText(c_id);
}

lstpro.setSelection(0)

之后将其称为

为ListView设置适配器后,如果要选择第一行,请添加此行:

lstpro.setSelection(0);

如果要选择最后一行,请添加此行:

lstpro.setSelection(ADA.getCount() - 1);