2017-03-24 9 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

が正しくこれはあなたのトーストなどitemaの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

私はAPI呼び出しのためRetrofit2を使用するためにあなたをアドバイスしたい、それは@SerializedNameによって、モデル内のAsyncTaskとマークされたフィールドその後、はるかに簡単です、改造はあなた自身のためにそれを行います。ウェブ電話をかけるには

0

はい使用改造、あなたはまだあなたのJSONを解析するために使用にGSONをAsyncTaskを使用する場合は、このLink

または参照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

ありがとうございます –

関連する問題