2017-03-24 64 views
-1

我有這段代碼。
我想獲取文本值取決於我在列表中單擊的項目。如何獲取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(); 

       } 
      }); 
     } 
    } 
} 
+0

標題:'JSON'? –

回答

1

如果你能夠分析和看到的結果是正確的,這將讓你的意達DN演出敬酒

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
       @Override 
       public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 


        String text = StudentObj.get(position).get //what you want from modal class goes here 
        Toast.makeText(Fetch.this, text, Toast.LENGTH_SHORT).show(); 

       } 
      }); 

請注意以下,如果面臨着一些問題

0

我建議你用Retrofit2的API調用,它在你的模型由@SerializedName更容易那麼的AsyncTask和顯着的領域,改造將使其成爲你自己。

0

是利用改造,使網絡電話,沒有多餘的痛苦來解析JSON看到這個Link

,或者如果你仍然想使用的AsyncTask使用GSON解析你JSON。在這個環節,你會得到here

閱讀這些顯然

0

可以使用object.get(位置),如下面的代碼更改獲得價值

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

      String Text = StudentObj.get(position).Subject; 

      } 
     }); 
+0

謝謝你的工作 –