gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:45:38 25 4
gpt4 key购买 nike

我在自动选择或默认选择 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。我只想知道如何进行默认选择。谢谢

最佳答案

编辑:只需添加功能

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

关于android - ListView : Android 中第一行的默认选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33770758/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com