我正在尝试在Android中获取所选列表项的值。
因为我一直在使用下面的JSON解析代码,当选择列表项时,我试图得到它的值,但是得到的结果如下:{Name=SupRyY},所需的结果是“SuPRYA”,如何实现这一点,请帮助。
>代码>公共类弹出式扩展AppCuastActudio {
private string tag=mainActivity.class.getSimpleName();
私有进程对话框pDialog;
公共列表视图lv;
字符串名称;
字符串路径;
字符串类型;
字符串视频;
字符串音频;
字符串项;
字符串交互性;
//私有静态字符串url=“http://10.60.100.101/mydata/chapter.php”;
私有静态字符串url;
arraylist<hashmap<string,string>>联系人列表;
创建时受保护的void(bundle savedinstanceState){
超级OnCube(SaveDestChanStand);
setContentView(r.layout.popup);
CONTACTLISTAB=新的ARARYLIST & LT;& GT;();
//按钮bt=(button)findViewByID(r.id.next);
lv=(listview)findview byid(r.id.list);
//从mygrid.java页面接收内容
意向=getIntent();
item=intent.getStringExtra(“name”);
Soop.Out.PrtLn(“点击先前活动”项+项);
getSupportActionBar().settitle(项);
new getcontacts().execute();
}
私有类getContacts扩展异步任务<void、void、void>。{
@重写
受保护的void onPrexecute()。{
超级。
//显示进度对话框
pDialog=新建进程对话框(popup.this);
PistIOL.SET消息(“请等待…”);
pDialog.setCancelable(真);
Pultudio.Suffor();
}
@重写
保护空虚的背景(空虚…ARG0){
httphandler sh=new httphandler();
//…绕过SSL安全的代码。
trustManager[]trustAllcerts=new trustManager[]new x509trustManager()。{
公共Java.Security .Cur.X509Cudio[] GETAcExecdDeSuffSe(){
返回空;
}
public void checkclienttrusted(x509certificate[]certs,string authtype){
}
public void checkServerTrusted(x509certificate[]certs,string authtype){
}
}
};
尝试{
//安装全信任信任信任管理器
sslcontext sc=sslcontext.getInstance(“ssl”);
SC.IIT(NULL、TrimeCalvices、新Java.Security、SecureRANDO());
httpsurlConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
}
catch(异常e){
system.out.println(“supriya di error”+e);
}
//创建所有信任的主机名验证程序
hostnameverifier allhostsvalid=new hostnameverifier()。{
公共布尔验证(String主机名,SSLsession会话){
回归真实;
}
};
//安装所有信任主机验证程序
httpsurlConnection.setDefaultHostnameVerifier(所有主机有效);
//…..绕过证书的代码在此结束
//string url=“http://www.ebiebook.com/rest/drmrestservices.svc/rest/products”;
//url=“http://10.60.100.101/mydata/chapter.php”;
//请求URL并得到响应
url=“https://stage.indiancalearning.com/mydata/”;
字符串jsonstr=sh.makeservicecall(url);
log.e(tag,“来自url的响应:”+jsonstr);
如果(JSONSTR)!{NULL){
尝试{
jsonObject jsonObj=新的jsonObject(jsonStr);
//获取json数组节点
jsonarray contacts=jsonobj.getjsonarray(“supi”);
//循环访问所有联系人
对于(int i=0;I& lt;Clase.LangTh();I++){
jsonObject c=contacts.getjsonObject(i);
name=c.getString(“主题”);
路径=c.getstring(“url”);
dType=C.GETSHIPE(“类型”);
//字符串email=c.getstring(“email”);
system.out.println(“名称值:”+name);
system.out.println(“url值:”+path);
system.out.println(“数据类型值:”+dtype);
//单个联系人的tmp哈希映射
hashmap<string,string>contact=new hashmap<>();
//将每个子节点添加到hashmap key=>value
system.out.println(“联系人姓名值:”+name);
//system.out.println(“联系人URL值:”+url);
system.out.println(“联系人数据类型值:”+dtype);
contact.put(“姓名”,name);
//contact.put(“url”,url);
//contact.put(“dtype”,dtype);
//将联系人添加到联系人列表
联系人列表。添加(联系人);
}
}
catch(异常E)
system.out.println(“错误”+e);
}否则{
log.e(标记“无法从服务器获取JSON”);
runOnIthread(new runnable()){
@重写
public void run()。{
toast.makeText(getApplicationContext(),
“无法从服务器获取JSON。检查logcat有无可能的错误!“,
烤面包,长度
显示();
}
(});
}
返回空;
}
@重写
Protected void OnPostExecute(void结果){
super.onPostExecute(结果);
//关闭进度对话框
if(pDialog.IsShowing())
pDialog.Dismise();
final list adapter adapter=新simpleadapter(popup.this,contactlist,r.layout.list_item,
新字符串[]“name”,新int[]r.id.name);
低压设置适配器(适配器);
lv.setOnitEmclickListener(new adapterview.OnitEmclickListener()。{
@重写
public void OnItemClick(adapterView<?>父级,视图视图,
int位置,long id){
string text=parent.getitematposition(position.toString();
//字符串文本=(textView)view).getText().toString();
//toast.makeText(getApplicationContext(),arr[position],toast.length_long).show();
system.out.println(“弹出页面文本:”+text);
system.out.println(“弹出页面路径:”+path);
意向意向=新意向(popup.this,audioplayer.class);
intent.putExtra(“路径”,path);
意向。PutExtra(“名称”,文本);
开始活动(意图);
}
(});
}
}
}
< /代码>
IMGSRC=“HTTPS://I.STACK.IMGURUR.COM/YOQQR.PNG”ALT=“输入图像描述在这里”/> /pcTIN列表项目,我试图得到它的价值,但得到的结果如下:{Name=SupRyY},所需的结果是'SupRy',如何才能做到这一点,请帮助。
public class Popup extends AppCompatActivity {
private String TAG = MainActivity.class.getSimpleName();
private ProgressDialog pDialog;
public ListView lv;
String name;
String path;
String dtype;
String video;
String audio;
String item;
String interactivity;
//private static String url = "http://10.60.100.101/mydata/chapter.php";
private static String url;
ArrayList<HashMap<String, String>> contactList;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.popup);
contactList = new ArrayList<>();
//Button bt= (Button)findViewById(R.id.next);
lv = (ListView) findViewById(R.id.list);
//Receiving content from the Mygrid.java page
Intent intent = getIntent();
item = intent.getStringExtra("name");
System.out.println("Item clicked at the previous activity"+ item);
getSupportActionBar().setTitle(item);
new GetContacts().execute();
}
private class GetContacts extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(Popup.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
//...code for bypassing the ssl security..
TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
}
};
try {
// Install the all-trusting trust manager
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
}
catch(Exception e){
System.out.println("supriya di error"+ e);
}
// Create all-trusting host name verifier
HostnameVerifier allHostsValid = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
// Install the all-trusting host verifier
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
//.....code of bypassing certificate ends here
// String url= "http://www.ebiebook.com/rest/DRMRestServices.svc/rest/products";
// url = "http://10.60.100.101/mydata/chapter.php";
// Making a request to url and getting response
url = "https://stage.indiannicalearning.com/mydata/";
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("supi");
// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
name = c.getString("topics");
path = c.getString("url");
dtype = c.getString("type");
//String email = c.getString("email");
System.out.println("name value :"+name);
System.out.println("url value :"+path);
System.out.println("data type value :"+dtype);
// tmp hash map for single contact
HashMap<String, String> contact = new HashMap<>();
// adding each child node to HashMap key => value
System.out.println("contact name value :"+name);
//System.out.println("contact url value :"+url);
System.out.println("contact data type value :"+dtype);
contact.put("name", name);
//contact.put("url", url);
// contact.put("dtype", dtype);
// adding contact to contact list
contactList.add(contact);
}
}
catch(Exception e)
{ System.out.println("error"+ e);}
} 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(Popup.this, contactList, R.layout.list_item,
new String []{"name"}, new int[]{R.id.name});
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String text =parent.getItemAtPosition(position).toString();
// String text = ((TextView) view).getText().toString();
// Toast.makeText(getApplicationContext(),arr[position],Toast.LENGTH_LONG).show();
System.out.println("pop up page text:" +text);
System.out.println("pop up page path:" +path);
Intent intent = new Intent(Popup.this, AudioPlayer.class);
intent.putExtra("path", path);
intent.putExtra("name", text);
startActivity(intent);
}
});
}
}
}