2

I'm trying to make the First Selected item to be highlighted or be Focus without clicking it. I can already get the data from the ListView adapter of the first item without clicking it. Please Help me Thanks

Here's my code for Listview

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="5"
android:padding="5dp"
android:layout_marginTop="2dp"
>
<TextView
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:textColor="#ffffff"
    android:textSize="25sp"
    android:text="1"
    android:id="@+id/lblproid"
    android:layout_marginLeft="15dp"
    android:editable="false" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:textColor="#ffffff"
    android:textSize="25sp"
    android:text="1"
    android:id="@+id/lblproid1"
    android:layout_marginLeft="15dp"
    android:editable="false" />


</LinearLayout>

And this for populating the ListView

 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", "C"};
            int[] views = { R.id.lblproid, R.id.lblproid1};
            final SimpleAdapter ADA = new SimpleAdapter(workingStation.this,
                    prolist, R.layout.activity_lsttemplate, from,
                    views);
            lstpro.setAdapter(ADA);
            lstpro.setSelection(0);
            lstpro.setItemChecked(0,true);
            lstpro.setSelector(R.drawable.selector_color);
        try{
            HashMap<String, Object> obj = (HashMap<String, Object>) ADA.getItem(0);
            String userName = (String) obj.get("A");
            String c_id = (String) obj.get("C");
            edtUname.setText(userName);
            edtUserID2.setText(c_id);

        }catch (Exception e){
            Check onLOadquery = new Check();
            onLOadquery.execute("");

        }

     }


    }

    @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", "");
                String query = "SELECT customer_name, customer_id, customer_transaction from customer_process where customer_window = '" + win_name + "' and customer_status = 'Pending' ";
                Statement ps = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
                ResultSet rs = ps.executeQuery(query);
                    ArrayList<String> data = 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_transaction"));
                        datanum.put("C", rs.getString("customer_id"));
                        prolist.add(datanum);
                    }
                if(rs.last()){
                    Integer count = rs.getRow();
                    rs.beforeFirst();
                   if(count == 0){
                       isSuccess = false;


                   }else {
                       isSuccess = true;

                   }
                }

                //Toast.makeText(getApplicationContext(), "Count : "+count, Toast.LENGTH_SHORT).show();

                z = "Success";


            }

        } catch (Exception ex) {
            isSuccess = false;
        }
        return z;
    }
} // end Onload

And my Selector_color.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/gray" android:state_pressed="true"/>
<item android:drawable="@color/gray" android:state_selected="true"/>
<item android:drawable="@color/gray" android:state_activated="true"/>
</selector>
Yuu
  • 619
  • 3
  • 13
  • 34

1 Answers1

0

This will do the job lv.setItemChecked(position, true);

position refers to row number you want to highlight, the row gets selected but it will not visible. Now to make it visible you will need to pass activated layout constructor in Arrayadapter. R.layout.simple_list_item_activated_2

Like this:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_activated_2, android.R.id.text1, myList);

if you're using custom ArrayAdapter then refer to this thread, it's clearly explained there. Programmatically select item ListView in Android

Tom Aranda
  • 5,919
  • 11
  • 35
  • 51
Amir Dora.
  • 2,831
  • 4
  • 40
  • 61