我有這段代碼。
我想獲取文本值取決於我在列表中單擊的項目。如何獲取JSON列表項值
package com.example.a3316.studentapp;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.a3316.studentapp.StudentsMsg.StudentsMsg;
import com.example.a3316.studentapp.StudentsMsg.StudentsMsgAdapter;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.net.HttpURLConnection;
import java.util.ArrayList;
public class Fetch extends Activity {
ListView lv;
ArrayList<StudentsMsg> StudentObj = new ArrayList<StudentsMsg>();
Intent n ;
String url;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fetch);
n = getIntent();
String Student = n.getStringExtra("StudentID_Key");
url ="http://example/St/St_Msg.svc/Student/" + Student;
new BackTask().execute(url);
lv = (ListView) findViewById(R.id.listView);
}
public class BackTask extends AsyncTask<String,String,String>{
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... strings) {
String content =HttpULRConnect.getData(url);
return content;
}
@Override
protected void onPostExecute(String s) {
try {
JSONArray ar = new JSONArray(s);
for (int i=0; i<ar.length(); i++){
JSONObject jsonobject = ar.getJSONObject(i);
StudentsMsg student = new StudentsMsg();
student.Subject(jsonobject.getString("Subject"));
student.StudentID(jsonobject.getString("StudentID"));
student.CenterDesc(jsonobject.getString("CenterDesc"));
StudentObj.add(student);
}
}
catch (JSONException e){
e.printStackTrace();
}
StudentsMsgAdapter adapter = new StudentsMsgAdapter(Fetch.this, R.layout.student_items, StudentObj);
lv = (ListView) findViewById(R.id.listView);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView textView = (TextView) lv.findViewById(R.id.Subject);
String text = textView.getText().toString();
Toast.makeText(Fetch.this, "1", Toast.LENGTH_SHORT).show();
}
});
}
}
}
標題:'JSON'? –