我是机器人初中。
我正在尝试将JSON URL解析为片段并在Listview中显示它。我被各种教程困住了几个小时,每个人都遵循不同的方式。
所以,请这就是我所达到的,让我知道我应该做些什么才能让它发挥作用!感谢您的帮助 - 很多代码都是从不同的教程中复制/粘贴,加上JSON文件只是应用在不同的教程中,基本的我只是试图让它显示出来。
activity_main.xml中
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.sampleapp.MainActivity" />
fragment_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.sampleapp.MainActivity$PlaceholderFragment" >
<TextView
android:id="@+id/section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/section_label"
android:layout_centerHorizontal="true"
android:layout_marginTop="171dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
itune_frag.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:id="@+id/vehicleType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="vehicleType" />
<TextView android:id="@+id/vehicleColor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="vehicleColor" />
</LinearLayout>
fav_frag.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/section_label"
android:layout_centerHorizontal="true"
android:layout_marginTop="171dp"
android:text="Favvvvvv"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
MainActivity.java
package com.example.sampleapp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.support.v13.app.FragmentPagerAdapter;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainActivity extends Activity implements ActionBar.TabListener {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
actionBar.addTab(actionBar.newTab().setText("iTunes List").setTabListener(this));
actionBar.addTab(actionBar.newTab().setText("Favourite").setTabListener(this));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onTabSelected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
mViewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
@Override
public void onTabReselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragment = new Fragment();
switch (position) {
case 0:
return fragment = new iTuneFragment();
case 1:
return fragment = new favFragment();
default:
break;
}
return fragment;
}
@Override
public int getCount() {
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
return null;
}
}
}
iTuneFragment.java
class iTuneFragment extends Fragment {
JSONArray user = null;
ArrayList<HashMap<String, String>> jsonlist = new ArrayList<HashMap<String, String>>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
iTuneList ll = new iTuneList();
return inflater.inflate(R.layout.itune_frag, container, false);
}
}
iTuneList.java
public final class iTuneList extends ListFragment {
// URL to get JSON Array
private static String url = "http://docs.blackberry.com/sampledata.json";
// JSON Node Names
private static final String VTYPE = "vehicleType";
private static final String VCOLOR = "vehicleColor";
JSONArray user = null;
ArrayList<HashMap<String, String>> jsonlist = new ArrayList<HashMap<String, String>>();
public iTuneList() {
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
new asy().execute();
}
private class asy extends
AsyncTask<Void, Void, ArrayList<HashMap<String, String>>> {
@Override
protected ArrayList<HashMap<String, String>> doInBackground(
Void... params) {
// Creating new JSON Parser
JSONParser jParser = new JSONParser();
// Getting JSON from URL
JSONArray json = jParser.getJSONFromUrl(url);
for (int i = 0; i < json.length(); i++) {
try {
JSONObject c = json.getJSONObject(i);
String vtype = c.getString(VTYPE);
String vcolor = c.getString(VCOLOR);
Log.e("iTuneList.java", "JSON: " + vtype);
HashMap<String, String> map = new HashMap<String, String>();
// Add child node to HashMap key & value
map.put(VTYPE, vtype);
map.put(VCOLOR, vcolor);
jsonlist.add(map);
} catch (JSONException e) {
e.printStackTrace();
}
}
return jsonlist;
}
@Override
protected void onPostExecute(
ArrayList<HashMap<String, String>> result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
ListAdapter adapter = new SimpleAdapter(getActivity(), result,
R.layout.itune_frag, new String[] { VTYPE, VCOLOR, },
new int[] { R.id.vehicleType, R.id.vehicleColor });
setListAdapter(adapter);
}
}
}
此外,我有使用getJSONFromUrl的JSONParser.java - 看起来很好的包装类。而且,favFragment.java是不同的故事,现在不介意它,它工作正常。
THAAAAAAAAAANKS !!
答案 0 :(得分:0)
public static class iTuneList extends ListFragment {
// URL to get JSON Array
private static String url = "http://docs.blackberry.com/sampledata.json";
// JSON Node Names
private static final String VTYPE = "vehicleType";
private static final String VCOLOR = "vehicleColor";
JSONArray user = null;
ArrayList<HashMap<String, String>> jsonlist = new ArrayList<HashMap<String, String>>();
public iTuneList() {
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
new asy().execute();
}
private class asy extends
AsyncTask<Void, Void, ArrayList<HashMap<String, String>>> {
@Override
protected ArrayList<HashMap<String, String>> doInBackground(
Void... params) {
// Creating new JSON Parser
JSONParser jParser = new JSONParser();
// Getting JSON from URL
JSONArray json = jParser.getJSONFromUrl(url);
for (int i = 0; i < json.length(); i++) {
try {
JSONObject c = json.getJSONObject(i);
String vtype = c.getString(VTYPE);
String vcolor = c.getString(VCOLOR);
HashMap<String, String> map = new HashMap<String, String>();
// Add child node to HashMap key & value
map.put(VTYPE, vtype);
map.put(VCOLOR, vcolor);
jsonlist.add(map);
} catch (JSONException e) {
e.printStackTrace();
}
}
return jsonlist;
}
@Override
protected void onPostExecute(
ArrayList<HashMap<String, String>> result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
ListAdapter adapter = new SimpleAdapter(getActivity(), result,
R.layout.itune_frag, new String[] { VTYPE, VCOLOR, },
new int[] { R.id.vehicleType, R.id.vehicleColor });
setListAdapter(adapter);
}
}
}
将此作为您的片段。
答案 1 :(得分:0)