Java 如何在警报对话框中按顺序显示选定的ListView项?
这是我的代码,我不知道如何编写以显示单个选定的项目。 如何在选定的listview视图项中显示用户id、名称和其他详细信息以提醒对话框。盒子。主要问题是所选项目的位置Java 如何在警报对话框中按顺序显示选定的ListView项?,java,Java,这是我的代码,我不知道如何编写以显示单个选定的项目。 如何在选定的listview视图项中显示用户id、名称和其他详细信息以提醒对话框。盒子。主要问题是所选项目的位置 列表项 私有字符串标记=MainActivity.class.getSimpleName() private ProgressDialog pDialog; 私有ListView lv; ArrayList联系人列表; @凌驾 创建时受保护的void(Bundle savedInstanceState){ super.onCr
- 列表项
private ProgressDialog pDialog;
私有ListView lv;
ArrayList联系人列表;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contactList=新的ArrayList();
lv=findViewById(R.id.list);
新建GetContacts().execute();
}
/**
*异步任务类通过HTTP调用获取json
*/
@SuppressLint(“StaticFieldLeak”)
私有类GetContacts扩展异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
//显示进度对话框
pDialog=新建进度对话框(MainActivity.this);
setMessage(“请稍候…”);
pDialog.setCancelable(假);
pDialog.show();
}
@凌驾
受保护的Void doInBackground(Void…arg0){
HttpHandler sh=新的HttpHandler();
//向url发出请求并获得响应
//获取联系人JSON的URL
字符串url=”https://api.androidhive.info/contacts/";
字符串jsonStr=sh.makeServiceCall(url);
Log.e(标签,“来自url的响应:+jsonStr”);
if(jsonStr!=null){
试一试{
JSONObject jsonObj=新的JSONObject(jsonStr);
//获取JSON数组节点
JSONArray contacts=jsonObj.getJSONArray(“contacts”);
//通过所有触点循环
对于(int i=0;ivalue
contact.put(“\n”+“id”,id);
联系人。填写(“姓名”,姓名);
联系方式。放置(“+”电子邮件),电子邮件);
联系人。放置(“+”mobile),mobile);
联系方式。填写(“地址”,地址);
联系人。输入(“\n”+“性别”,性别);
联系人。放置(“\n”+“home”,home);
联系人。put(“\n”+“office”,office);
//将联系人添加到联系人列表
联系人列表。添加(联系人);
}
}捕获(最终JSONException e){
Log.e(标记“Json解析错误:”+e.getMessage());
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
Toast.makeText(getApplicationContext(),
Json分析错误:“+e.getMessage(),
吐司长度(长)
.show();
}
});
}
}否则{
e(标记“无法从服务器获取json”);
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
Toast.makeText(getApplicationContext(),
“无法从服务器获取json。请检查LogCat以了解可能的错误!”,
吐司长度(长)
.show();
}
});
}
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
super.onPostExecute(结果);
//关闭进度对话框
if(pDialog.isShowing())
pDialog.disclose();
最终列表适配器=新SimpleAdapter(
MainActivity.this,联系人列表,
R.layout.list_项,新字符串[]{“name”,“email”,
“手机”、“地址”、“性别”},新int[]{R.id.name,
R.id.email,R.id.mobile,R.id.address});
低压设置适配器(适配器);
lv.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView父级、视图、,
内部位置,长id)
{
AlertDialog.Builder alert=新建AlertDialog.Builder(MainActivity.this);
View mView=GetLayoutFlater().充气(R.layout.list_项,空);
最终列表适配器=新SimpleAdapter(
MainActivity。此,联系人列表,
R.layout.list_项,新字符串[]{“name”,“email”,
“手机”、“地址”、“性别”},新int[]{R.id.name,
R.id.email,R.id.mobile,R.id.address});
联系人列表。获取(位置);
alert.setAdapter(适配器,null);
警报.setView(mView);
最终的androidx.appcompat.app.AlertDialog AlertDialog=alert.create();
alertDialog.setCanceledOnTouchOutside(false);
btn_cancel.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
警惕
private ProgressDialog pDialog;
private ListView lv;
ArrayList<HashMap<String, String>> contactList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contactList = new ArrayList<>();
lv = findViewById(R.id.list);
new GetContacts().execute();
}
/**
* Async task class to get json by making HTTP call
*/
@SuppressLint("StaticFieldLeak")
private class GetContacts extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
// Making a request to url and getting response
// URL to get contacts JSON
String url = "https://api.androidhive.info/contacts/";
String jsonStr = sh.makeServiceCall(url);
Log.e(TAG, "Response from url: " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
JSONArray contacts = jsonObj.getJSONArray("contacts");
// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
String id = c.getString("id");
String name = c.getString("name");
String email = c.getString("email");
String address = c.getString("address");
String gender = c.getString("gender");
// Phone node is JSON Object
JSONObject phone = c.getJSONObject("phone");
String mobile = phone.getString("mobile");
String home = phone.getString("home");
String office = phone.getString("office");
// tmp hash map for single contact
HashMap<String, String> contact = new HashMap<>();
// adding each child node to HashMap key => value
contact.put("\n"+"id", id);
contact.put("name", name);
contact.put(""+ "email", email);
contact.put(""+"mobile", mobile);
contact.put("address",address);
contact.put("\n"+"gender",gender);
contact.put("\n"+ "home",home);
contact.put("\n"+"office",office);
// adding contact to contact list
contactList.add(contact);
}
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG)
.show();
}
});
}
} else {
Log.e(TAG, "Couldn't get json from server.");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Couldn't get json from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG)
.show();
}
});
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
final ListAdapter adapter = new SimpleAdapter(
MainActivity.this, contactList,
R.layout.list_item, new String[]{"name", "email",
"mobile", "address", "gender"}, new int[]{R.id.name,
R.id.email, R.id.mobile, R.id.address});
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
View mView = getLayoutInflater().inflate(R.layout.list_item,null);
final ListAdapter adapter = new SimpleAdapter(
MainActivity.this,contactList,
R.layout.list_item, new String[]{"name", "email",
"mobile", "address", "gender"}, new int[]{R.id.name,
R.id.email, R.id.mobile, R.id.address});
contactList.get(position);
alert.setAdapter(adapter, null);
alert.setView(mView);
final androidx.appcompat.app.AlertDialog alertDialog = alert.create();
alertDialog.setCanceledOnTouchOutside(false);
btn_cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
alertDialog.dismiss();
}
});
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
textView.setText(text.getText().toString());
}
});
alertDialog.show();
}
});
}
}