Yuu
Yuu

Reputation: 619

Default selection on first row in ListView : Android

I'm having difficulty in automatically select or default select the first row in ListView. Please help me

here's my code :

//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

Note: My code is working fine I can view the ListView. All I want to know is how to make a default Selection. Thanks

Upvotes: 1

Views: 497

Answers (1)

Tran Vinh Quang
Tran Vinh Quang

Reputation: 595

EDIT: Just add function

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);
}

Called it after lstpro.setSelection(0)


After set Adapter for ListView add this line if you want select first row:

lstpro.setSelection(0);

Add this line if you want select last row:

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

Upvotes: 1

Related Questions