如何通过Intent从JSON传递解析数据?

时间:2015-12-12 17:20:58

标签: android json

我用json解析器(tutorial which I followed)创建了一个Android应用程序并成功显示了我的数据。但是我无法在SingleView中显示更多数据,然后我在ListView中显示。 (可以,我可以。但它只是所需的字符串名称。)我该怎么做?

SingleFilmActivity.java

public class SingleFilmActivity  extends AppCompatActivity {

// JSON Node names
private static final String TAG_NAME = "jmeno";
private static final String TAG_START = "zacatek";
private static final String TAG_END = "konec";
private static final String TAG_CATEGORY = "kategorie";
private static final String TAG_POPIS = "popis";
private static final String TAG_BAN = "omezeni";
private static final String TAG_DAY = "den";
private static final String TAG_CLASS = "trida";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_single_film);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    // getting intent data
    Intent in = getIntent();

    // Get JSON values from previous intent
    String name = in.getStringExtra(TAG_NAME);
    String zacatek = in.getStringExtra(TAG_START);
    String konec = in.getStringExtra(TAG_END);
    String kategorie = in.getStringExtra(TAG_CATEGORY);
    String popis = in.getStringExtra(TAG_POPIS);
    String omezeni = in.getStringExtra(TAG_BAN);
    String den = in.getStringExtra(TAG_DAY);
    String trida = in.getStringExtra(TAG_CLASS);

    // Displaying all values on the screen
    TextView lblName = (TextView) findViewById(R.id.name_label);
    TextView lblZacatek = (TextView) findViewById(R.id.zacatek_label);
    TextView lblKonec = (TextView) findViewById(R.id.konec_label);
    TextView lblKategorie = (TextView) findViewById(R.id.kategorie_label);
    TextView lblPopis = (TextView) findViewById(R.id.popis_label);
    TextView lblOmezeni = (TextView) findViewById(R.id.omezeni_label);
    TextView lblDen = (TextView) findViewById(R.id.den_label);
    TextView lblTrida = (TextView) findViewById(R.id.trida_label);

    lblName.setText(name);
    lblZacatek.setText(zacatek);
    lblKonec.setText(konec);
    lblKategorie.setText(kategorie);
    lblPopis.setText(popis);
    lblOmezeni.setText(omezeni);
    lblDen.setText(den);
    lblTrida.setText(trida);

    assert getSupportActionBar() != null;
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}

Den2Fragment.java

public class Den2Fragment extends ListFragment {
private ProgressDialog pDialog;

// JSON Node names
private static final String TAG_FILMY = "filmy";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "jmeno";
private static final String TAG_START = "zacatek";
private static final String TAG_END = "konec";
private static final String TAG_CATEGORY = "kategorie";
private static final String TAG_POPIS = "popis";
private static final String TAG_BAN = "omezeni";
private static final String TAG_DAY = "den";
private static final String TAG_CLASS = "trida";

// Hashmap for ListView
private ArrayList<HashMap<String, String>> filmList = new ArrayList<>();

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_den2, container, false);
}

@Override
public void onViewCreated (View view, Bundle savedInstanceState) {
    ListView lv = getListView();

    // ListView on item click listener
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // getting values from selected ListItem
            String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
            String zacatek = ((TextView) view.findViewById(R.id.zacatek)).getText().toString();
            String kategorie = ((TextView) view.findViewById(R.id.kategorie)).getText().toString();

            Intent in = new Intent(getActivity().getApplicationContext(), SingleFilmActivity.class);
            in.putExtra(TAG_NAME, name);
            in.putExtra(TAG_START, zacatek);
            in.putExtra(TAG_END,TAG_END);
            in.putExtra(TAG_CATEGORY, kategorie);
            in.putExtra(TAG_POPIS, TAG_POPIS);
            in.putExtra(TAG_BAN, TAG_BAN);
            in.putExtra(TAG_DAY, TAG_DAY);
            in.putExtra(TAG_CLASS, TAG_CLASS);
            startActivity(in);
        }
    });
    // Calling async task to get json
    new GetFilmy().execute();
}

/** Async task class to get json by making HTTP call */
private class GetFilmy extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Showing progress dialog
        pDialog = new ProgressDialog(getActivity());
        pDialog.setMessage("Prosím čekejte");
        pDialog.setCancelable(false);
        pDialog.show();
    }

    @Override
    protected Void doInBackground(Void... arg0) {
        // Creating service handler class instance
        ServiceHandler sh = new ServiceHandler();

        // Making a request to url and getting response
        String url =" foo.com";
        String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);

        Log.d("Response: ", "> " + jsonStr);

        if (jsonStr != null) {
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);

                // Getting JSON Array node
                JSONArray filmy = jsonObj.getJSONArray(TAG_FILMY);

                // looping through All Films
                for (int i = 0; i < filmy.length(); i++) {
                    JSONObject f = filmy.getJSONObject(i);

                    String id = f.getString(TAG_ID);
                    String name = f.getString(TAG_NAME);
                    String zacatek = f.getString(TAG_START);
                    String kategorie = f.getString(TAG_CATEGORY);

                    // tmp hashmap for single film
                    HashMap<String, String> film = new HashMap<>();

                    // adding each child node to HashMap key => value
                    film.put(TAG_ID, id);
                    film.put(TAG_NAME, name);
                    film.put(TAG_START, zacatek);
                    film.put(TAG_CATEGORY, kategorie);

                    // adding film to film list
                    filmList.add(film);
                }
            } catch (JSONException e) {e.printStackTrace();}
        } else {Log.e("ServiceHandler", "Couldn't get any data from the url");}
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        // Dismiss the progress dialog
        if (pDialog.isShowing()) pDialog.dismiss();
        /** Updating parsed JSON data into ListView */
        ListAdapter adapter = new SimpleAdapter(
                getActivity(), filmList, R.layout.list_item_true,
                new String[] { TAG_NAME, TAG_START, TAG_CATEGORY },
                new int[] { R.id.name, R.id.zacatek, R.id.kategorie });
        setListAdapter(adapter);
    }
}
}

list_item_true.xml

<TextView
    android:id="@+id/name"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="2sp"
    android:paddingTop="6sp"
    android:textColor="#000000"
    android:textSize="16sp"
    android:textStyle="bold" />

<TextView
    android:id="@+id/zacatek"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="2sp"
    android:textColor="#5d5d5d"
    android:textStyle="bold" />

<TextView
    android:id="@+id/kategorie"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="start"
    android:textColor="#5d5d5d" />

content_single_film.xml

<TextView android:id="@+id/name_label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="25sp"
        android:textStyle="bold"
        android:paddingTop="10sp"
        android:paddingBottom="10sp"
        android:textColor="#000000" />

<TextView android:id="@+id/zacatek_label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#727272"
        android:textStyle="bold" />

<TextView android:id="@+id/konec_label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#727272" />

<TextView android:id="@+id/kategorie_label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#727272"
        android:textStyle="bold" />

<TextView android:id="@+id/omezeni_label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#727272"
        android:textStyle="bold" />

<TextView android:id="@+id/den_label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#727272" />

<TextView android:id="@+id/trida_label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#727272" />

<TextView android:id="@+id/popis_label"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="#212121" />

很抱歉发布了很多代码,但我不确定我做错了什么。

2 个答案:

答案 0 :(得分:0)

最好将整个json转换为字符串。

 Intent intent= new Intent(Current.this, Next.class);
intent.putExtra("jsonValue",YourJson.toString);
startActivity(intent);

在下一个活动中,您可以使用

获取字符串的值
 Intent get=getIntent();
  String json=  get.getStringExtra("jsonValue");

你现在有了json String,你可以解析它并获得值

答案 1 :(得分:0)

最好使用Gson。这是如此简单和容易

Film film = new Film(...);
String gsonStr = new Gson().toJson(film);
Intent intent= new Intent(Current.this, Next.class);
intent.putExtra("gson",gsonStr);
startActivity(intent);


//From Next Activity
 Intent intent=getIntent();
 String gStr = intent.getStringExtra("gson");
 Film getFilm = new Gson().fromJson(gStr,Film.class);