Android(无需点击即可在Listview上设置焦点突出显示)

【Android(无需点击即可在Listview上设置焦点突出显示)】宝剑锋从磨砺出,梅花香自苦寒来。这篇文章主要讲述Android:无需点击即可在Listview上设置焦点突出显示相关的知识,希望能为你提供帮助。
我正在尝试使首选项目突出显示或成为焦点而不点击它。我已经可以从第一个项目的ListView适配器获取数据而不单击它。请帮帮我谢谢
这是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>

这用于填充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 = https://www.songbingjia.com/android/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

还有我的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>

答案这将做lv.setItemChecked(position, true); 的工作
position是指您想要突出显示的行号,该行被选中但不可见。现在要使其可见,您需要在Arrayadapter中传递激活的布局构造函数。 R.layout.simple_list_item_activated_2
像这样:
ArrayAdapter< String> adapter = new ArrayAdapter< String> (this, android.R.layout.simple_list_item_activated_2, android.R.id.text1, myList);

如果你正在使用自定义ArrayAdapter,那么请参考这个帖子,它在那里有明确的解释。 Programmatically select item ListView in Android

    推荐阅读