如何获取JSON列表项值

时间:2017-03-24 12:05:01

标签: android listview

我有这个代码。
我想获取文本值取决于我在列表值中单击的项目。

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();

                }
            });
        }
    }
}

4 个答案:

答案 0 :(得分:1)

如果您能够正确解析并查看结果,这将使您获得迭代dn show to toast

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();

                }
            });

如果面临一些问题,请在下面注明

答案 1 :(得分:0)

我建议您使用Retrofit2进行API调用,然后通过@SerializedName比模型中的AsyncTask和标记字段容易得多,改造将为您自己制作。

答案 2 :(得分:0)

是使用改造来进行网络通话,解析JSON没有额外的痛苦,请参阅此Link

或者如果您仍想使用AsyncTask,请使用GSON来解析JSON。在此链接中,您将获得here

清楚阅读

答案 3 :(得分:0)

您可以使用object.get(position)获取值,如下面的代码更改

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

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

            }
        });