当我打电话给json并且让它变成“你不能拥有相同的东西时,我想创造一个条件”。当您想要向数据库添加更多数据时,在json值中。
这是我的代码
String nim2, ruang2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_kelas);
nim = (EditText) findViewById(R.id.editNim);
ruang = (EditText) findViewById(R.id.ruangPinjam);
new daftarMahasiswa().execute();
}
public void nextButton (View view) {
nim2 = nim.getText().toString();
ruang2 = ruang.getText().toString();
if (ruang2.equals(ruangan)) {
Toast.makeText(KelasActivity.this, "this room has already borrow",Toast.LENGTH_LONG).show();
ruang.setError("you cannot borrow the room again");
}else if (nim2.equals(nimMahasiswa)){
Toast.makeText(KelasActivity.this, "this NIM has borrow the room",Toast.LENGTH_LONG).show();
nim.setError("you cannot borrow it again");
}
class daftarMahasiswa extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(KelasActivity.this);
pDialog.setMessage("Loading...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected String doInBackground(String... params) {
String link_url = "http://192.168.43.54/datapeminjamankelas/read_mahasiswa.php";
JSONParser jParser = new JSONParser();
JSONObject json = null;
try {
json = jParser.AmbilJson(link_url);
} catch (JSONException e) {
e.printStackTrace();
}
try {
str_json = json.getJSONArray("data");
for (int i = 0; i < str_json.length(); i++) {
JSONObject ar = str_json.getJSONObject(i);
ruangan = ar.getString("ruang").trim();
nimMahasiswa = ar.getString("nim").trim();
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
pDialog.dismiss();
}
}
这是我的json
{
"data": [
{
"nim": "103452",
"ruang": "2702"
},
{
"nim": "102341",
"ruang": "2504"
},
{
"nim": "103421",
"ruang": "1101"
}
]
}
我想要做的是&#39; nim&#39;和#ruang&#39;从json无法再添加相同的值。它诚实地工作,但只有当数据只有1,当它有更多它不再工作。请帮助我做些什么来使它正确
答案 0 :(得分:0)
您可以添加&#39; nim&#39;和&#39; ruang&#39;单独列表中的值,从那里,您可以检查输入的值是否已经存在。
List<String> nimList = new ArrayList<>();
List<String> ruangList = new ArrayList<>();
for (int i = 0; i < str_json.length(); i++) {
JSONObject ar = str_json.getJSONObject(i);
ruangan = ar.getString("ruang").trim();
nimMahasiswa = ar.getString("nim").trim();
nimList.add(nimMahasiswa);
ruangList.add(ruangan);
}
您可以查看如下:
nim2 = nim.getText().toString();
ruang2 = ruang.getText().toString();
if (ruangList.contains(ruang2)) {
Toast.makeText(KelasActivity.this, "this room has already borrow",Toast.LENGTH_LONG).show();
ruang.setError("you cannot borrow the room again");
}else if (nimList.contains(nim2)){
Toast.makeText(KelasActivity.this, "this NIM has borrow the room",Toast.LENGTH_LONG).show();
nim.setError("you cannot borrow it again");
}
答案 1 :(得分:0)
最好你可以拥有一个pojo类并覆盖equals方法,如下所示
public class MyData {
private String myNim;
private String myRuang;
//setter & getters here
@Override
public boolean equals(Object obj) {
if(obj instanceOf MyData)
{
MyData myData = (MyData) obj;
return myData.getMyNim().equals(this.myNim)&&myData.getMyRuang().equals(this.myRuang);
}
return false;
}
}
现在您可以使用以下代码检查元素是否存在。
try {
str_json = json.getJSONArray("data");
List<MyData> myDataList = new ArrayList<MyData>();
for (int i = 0; i < str_json.length(); i++) {
JSONObject ar = str_json.getJSONObject(i);
ruangan = ar.getString("ruang").trim();
nimMahasiswa = ar.getString("nim").trim();
MyData myData = new MyData();
myData.setMyNim(nimMahasiswa);
myData.setMyRuang(ruangan);
if(myDataList.contains(myData))
//use your logic here to show error 'you cannot have the same thing'
else
myDataList.add(myData);
}
} catch (JSONException e) {
e.printStackTrace();
}
现在你可以使用具有唯一元素的myDataList。