即時通訊新這裏,對不起,如果我做一些白癡的問題,ANDROID>如何使用微調
爲Issues
設置JSON數據我已經創建象下面這樣一個應用程序,並在所有的價值通過微調JSON在這個
獲得,這種情況發生在應用程序的運行, Spinner
,但我想設置,如果有人選擇「ardie哈利姆」號e第二個微調只顯示「移動開發者」,如果有人選擇「indah」顯示「數據庫oracle」的第二個微調,等等
我試圖從go * gle找到教程,但我不知道什麼是正確的keyword
找出來,
僅供參考我的代碼
MainActivity.java
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ArrayList<String> listItems=new ArrayList<>();
ArrayAdapter<String> adapter;
Spinner sp;
ArrayList<String> listItems2=new ArrayList<>();
ArrayAdapter<String> adapter2;
Spinner sp2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sp=(Spinner)findViewById(R.id.spinner);
sp2=(Spinner)findViewById(R.id.spinner2);
adapter= new ArrayAdapter<>(this, R.layout.spinner_layout, R.id.txt, listItems);
adapter2= new ArrayAdapter<>(this, R.layout.spinner_layout, R.id.txt, listItems2);
sp.setAdapter(adapter);
sp2.setAdapter(adapter2);
}
public void onStart(){
super.onStart();
BackTask bt=new BackTask();
bt.execute();
}
private class BackTask extends AsyncTask<Void,Void,Void> {
ArrayList<String> list;
ArrayList<String> list2;
protected void onPreExecute(){
super.onPreExecute();
list=new ArrayList<>();
list2=new ArrayList<>();
}
protected Void doInBackground(Void...params){
InputStream is=null;
String result="";
try{
HttpClient httpclient=new DefaultHttpClient();
HttpPost httppost= new HttpPost("http://zxccvvv.netne.net/dosen.php");
HttpResponse response=httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
// Get our response as a String.
is = entity.getContent();
}catch(IOException e){
e.printStackTrace();
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"));
String line;
while ((line = reader.readLine()) != null) {
result+=line;
}
is.close();
//result=sb.toString();
}catch(Exception e){
e.printStackTrace();
}
// parse json data
try{
JSONArray jArray =new JSONArray(result);
for(int i=0;i<jArray.length();i++){
JSONObject jsonObject=jArray.getJSONObject(i);
// add interviewee name to arraylist
list.add(jsonObject.getString("nama_dosen"));
list2.add(jsonObject.getString("mat_kul"));
}
}
catch(JSONException e){
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void result){
listItems.addAll(list);
listItems2.addAll(list2);
adapter.notifyDataSetChanged();
adapter2.notifyDataSetChanged();
}
}
}
添加您的JSON輸出whic h你想加入微調 –
@AshishKudale這是什麼意思先生?你能舉個例子嗎?我認爲它很好添加一個'if else'功能,但我不知道應該在哪裏放置'if else'代碼在我的項目中 – yudi
只是在這裏顯示您的json數據 –