这是我的android代码,我想验证从json获取的密码,因此我添加了EditText
和Button
,但我不知道在哪里实现以及如何实现
根据以下代码,我从json文件中获取了一堆密码,并将其打印在ListView
上。现在会有一个EditText
和一个Button
,当我放置任何我提取的密码时,它应该给我true
或false
敬酒。
public class MainActivity extends AppCompatActivity {
private String TAG = MainActivity.class.getSimpleName();
private ProgressDialog pDialog;
private ListView lv;
Button bt;
EditText et;
private static String url = "http://52.206.14.188:8080/Configuration";
ArrayList<HashMap<String, ArrayList<String>>> contactList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contactList = new ArrayList<HashMap<String, ArrayList<String>>>();
lv = (ListView) findViewById(R.id.list);
bt = (Button)findViewById(R.id.check);
et = (EditText)findViewById(R.id.val);
new GetContacts().execute();
}
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();
String jsonStr = sh.makeServiceCall(url);
Log.e(TAG, "Response from url: " + jsonStr);
if (jsonStr != null) {
try {
JSONArray Source = new JSONArray(jsonStr);
JSONObject config = Source.getJSONObject(6);
JSONObject configVal = new JSONObject(config.getString("configurationVal"));
JSONArray areas = configVal.getJSONArray("availableAreas");
ArrayList<String> pincodes = new ArrayList<String>();
for (int i = 0 ; i < areas.length(); i++) {
JSONObject area = areas.getJSONObject(i);
pincodes.add(area.getString("pincode"));
HashMap<String, ArrayList<String>> contact = new HashMap<>();
contact.put("pincode", pincodes);
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();
}
});
}
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (pDialog.isShowing())
pDialog.dismiss();
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, contactList,
R.layout.list_item, new String[]{"pincode"}, new int[]{R.id.title});
lv.setAdapter(adapter);
}
}
}
答案 0 :(得分:1)
用户按下按钮时,您需要检查用户输入的内容是否存在于列表中。
bt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String input = et.getText();
if(contactList.contains(input)) {
Toast.makeText(getApplicationContext(),
"Yes",
Toast.LENGTH_LONG)
.show();
} else {
Toast.makeText(getApplicationContext(),
"No",
Toast.LENGTH_LONG)
.show();
}
}
});
编辑:根据您的请求编辑代码:
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import com.facebook.stetho.server.http.HttpHandler;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import tv.accedo.airtel.wynk.R;
public class MainActivity extends AppCompatActivity {
private String TAG = MainActivity.class.getSimpleName();
private ProgressDialog pDialog;
private ListView lv;
Button bt;
EditText et;
private static String url = "http://52.206.14.188:8080/Configuration";
ArrayList<HashMap<String, ArrayList<String>>> contactList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contactList = new ArrayList<HashMap<String, ArrayList<String>>>();
lv = (ListView) findViewById(R.id.list);
bt = (Button)findViewById(R.id.check);
et = (EditText)findViewById(R.id.val);
new GetContacts().execute();
}
private class GetContacts extends AsyncTask<Void, Void, Void> {
ArrayList<String> pincodes = new ArrayList<String>();
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
et.setEnabled(false);
}
@Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
String jsonStr = sh.makeServiceCall(url);
Log.e(TAG, "Response from url: " + jsonStr);
if (jsonStr != null) {
try {
JSONArray Source = new JSONArray(jsonStr);
JSONObject config = Source.getJSONObject(6);
JSONObject configVal = new JSONObject(config.getString("configurationVal"));
JSONArray areas = configVal.getJSONArray("availableAreas");
for (int i = 0 ; i < areas.length(); i++) {
JSONObject area = areas.getJSONObject(i);
pincodes.add(area.getString("pincode"));
HashMap<String, ArrayList<String>> contact = new HashMap<>();
contact.put("pincode", pincodes);
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();
}
});
}
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
et.setEnabled(true);
if (pDialog.isShowing())
pDialog.dismiss();
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, contactList,
R.layout.list_item, new String[]{"pincode"}, new int[]{R.id.title});
lv.setAdapter(adapter);
bt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String input = et.getText().toString().trim();
if(pincodes.contains(input)) {
Toast.makeText(getApplicationContext(),
"Yes",
Toast.LENGTH_LONG)
.show();
} else {
Toast.makeText(getApplicationContext(),
"No",
Toast.LENGTH_LONG)
.show();
}
}
});
}
}
}
答案 1 :(得分:1)
我认为您想检查Json的有效性,然后使用此功能来验证json :(导入org.json。*;)
public boolean isValidJSON(String test) {
try {
new JSONObject(test); // check for json object
} catch (JSONException ex) {
// if no json object check for JSONArray
try {
new JSONArray(test);
} catch (JSONException ex1) {
return false; // no 'json object'/'json Array' found therefor returning false
}
}
return true; }
答案 2 :(得分:1)
当用户想要按下按钮时 创建此事件
YourButtton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(contactList.contains(YourEditText.getText().toString.trim())) {
//its yes
} else {
//its false});