Java Android-将JSON加载到ListView
我对Android开发还是相当陌生,我正在做一个应用程序/项目,其中包括将这个JSON(如下所列)加载到一个带有名称、描述和图像的Java Android-将JSON加载到ListView,java,android,json,Java,Android,Json,我对Android开发还是相当陌生,我正在做一个应用程序/项目,其中包括将这个JSON(如下所列)加载到一个带有名称、描述和图像的ListView,但在模拟器上它一直是空白的。请告诉我我做错了什么-任何帮助都将不胜感激!(越具体越好,因为我是个新手……) JSON: { "success":true, "games":[ { "id":"1", "name":"Name of the Game", "descript
ListView
,但在模拟器上它一直是空白的。请告诉我我做错了什么-任何帮助都将不胜感激!(越具体越好,因为我是个新手……)
JSON:
{
"success":true,
"games":[
{
"id":"1",
"name":"Name of the Game",
"description":"This is what it is about",
"image":"IMAGE URL"
}
]
}
// URL to make request
private static final String url = "URL of API";
// JSON Node names
private static final String TAG_GAMES = "games";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_DESCRIPTION = "description";
private static final String TAG_IMAGE = "image";
// games JSONArray
JSONArray games = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Hash map for ListView
ArrayList<HashMap<String, String>> gameList = new ArrayList<HashMap<String, String>>();
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
try {
// Getting Array of Games
games = json.getJSONArray(TAG_GAMES);
// looping through All Games
for(int i = 0; i < games.length(); i++){
JSONObject c = games.getJSONObject(i);
// Storing each JSON item in variable
String id = c.getString(TAG_ID);
String image = c.getString(TAG_IMAGE);
// is again JSON Object
JSONObject author = c.getJSONObject(TAG_DESCRIPTION);
String name = author.getString(TAG_NAME);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_NAME, name);
map.put( TAG_IMAGE, image);
// adding HashList to ArrayList
gameList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(this, gameList,
R.layout.list_item,
new String[] { TAG_NAME, TAG_DESCRIPTION }, new int[] {
R.id.name, R.id.description });
setListAdapter(adapter);
// selecting single ListView item
ListView lv = getListView();
// Launching new screen on Selecting Single ListItem
lv.setOnItemClickListener(new OnItemClickListener() {
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 description = ((TextView) view.findViewById(R.id.description)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
in.putExtra(TAG_NAME, name);
in.putExtra(TAG_DESCRIPTION, description);
startActivity(in);
}
});
}
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<!-- Main ListView
Always give id value as list(@android:id/list)
-->
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
AndroidJSONParsingActivity:
{
"success":true,
"games":[
{
"id":"1",
"name":"Name of the Game",
"description":"This is what it is about",
"image":"IMAGE URL"
}
]
}
// URL to make request
private static final String url = "URL of API";
// JSON Node names
private static final String TAG_GAMES = "games";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_DESCRIPTION = "description";
private static final String TAG_IMAGE = "image";
// games JSONArray
JSONArray games = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Hash map for ListView
ArrayList<HashMap<String, String>> gameList = new ArrayList<HashMap<String, String>>();
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
try {
// Getting Array of Games
games = json.getJSONArray(TAG_GAMES);
// looping through All Games
for(int i = 0; i < games.length(); i++){
JSONObject c = games.getJSONObject(i);
// Storing each JSON item in variable
String id = c.getString(TAG_ID);
String image = c.getString(TAG_IMAGE);
// is again JSON Object
JSONObject author = c.getJSONObject(TAG_DESCRIPTION);
String name = author.getString(TAG_NAME);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_NAME, name);
map.put( TAG_IMAGE, image);
// adding HashList to ArrayList
gameList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(this, gameList,
R.layout.list_item,
new String[] { TAG_NAME, TAG_DESCRIPTION }, new int[] {
R.id.name, R.id.description });
setListAdapter(adapter);
// selecting single ListView item
ListView lv = getListView();
// Launching new screen on Selecting Single ListItem
lv.setOnItemClickListener(new OnItemClickListener() {
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 description = ((TextView) view.findViewById(R.id.description)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
in.putExtra(TAG_NAME, name);
in.putExtra(TAG_DESCRIPTION, description);
startActivity(in);
}
});
}
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<!-- Main ListView
Always give id value as list(@android:id/list)
-->
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
main.xml:
{
"success":true,
"games":[
{
"id":"1",
"name":"Name of the Game",
"description":"This is what it is about",
"image":"IMAGE URL"
}
]
}
// URL to make request
private static final String url = "URL of API";
// JSON Node names
private static final String TAG_GAMES = "games";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_DESCRIPTION = "description";
private static final String TAG_IMAGE = "image";
// games JSONArray
JSONArray games = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Hash map for ListView
ArrayList<HashMap<String, String>> gameList = new ArrayList<HashMap<String, String>>();
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
try {
// Getting Array of Games
games = json.getJSONArray(TAG_GAMES);
// looping through All Games
for(int i = 0; i < games.length(); i++){
JSONObject c = games.getJSONObject(i);
// Storing each JSON item in variable
String id = c.getString(TAG_ID);
String image = c.getString(TAG_IMAGE);
// is again JSON Object
JSONObject author = c.getJSONObject(TAG_DESCRIPTION);
String name = author.getString(TAG_NAME);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_NAME, name);
map.put( TAG_IMAGE, image);
// adding HashList to ArrayList
gameList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(this, gameList,
R.layout.list_item,
new String[] { TAG_NAME, TAG_DESCRIPTION }, new int[] {
R.id.name, R.id.description });
setListAdapter(adapter);
// selecting single ListView item
ListView lv = getListView();
// Launching new screen on Selecting Single ListItem
lv.setOnItemClickListener(new OnItemClickListener() {
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 description = ((TextView) view.findViewById(R.id.description)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
in.putExtra(TAG_NAME, name);
in.putExtra(TAG_DESCRIPTION, description);
startActivity(in);
}
});
}
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<!-- Main ListView
Always give id value as list(@android:id/list)
-->
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- Name Label -->
<TextView
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#43bd00"
android:textSize="16sp"
android:textStyle="bold"
android:paddingTop="6dip"
android:paddingBottom="2dip" />
<TextView
android:id="@+id/description"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#acacac"
android:paddingBottom="2dip" />
</LinearLayout>
添加LogCat信息:
01-03 06:16:15.254: E/ActivityThread(616): Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d47b40 that was originally bound here
01-03 06:16:15.254: E/ActivityThread(616): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d47b40 that was originally bound here
01-03 06:16:15.254: E/ActivityThread(616): at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
01-03 06:16:15.254: E/ActivityThread(616): at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
01-03 06:16:15.254: E/ActivityThread(616): at android.app.ContextImpl.bindService(ContextImpl.java:1418)
01-03 06:16:15.254: E/ActivityThread(616): at android.app.ContextImpl.bindService(ContextImpl.java:1407)
01-03 06:16:15.254: E/ActivityThread(616): at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
01-03 06:16:15.254: E/ActivityThread(616): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
01-03 06:16:15.254: E/ActivityThread(616): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
01-03 06:16:15.254: E/ActivityThread(616): at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191)
01-03 06:16:15.254: E/ActivityThread(616): at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850)
01-03 06:16:15.254: E/ActivityThread(616): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
01-03 06:16:15.254: E/ActivityThread(616): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
01-03 06:16:15.254: E/ActivityThread(616): at android.os.AsyncTask$2.call(AsyncTask.java:287)
01-03 06:16:15.254: E/ActivityThread(616): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
01-03 06:16:15.254: E/ActivityThread(616): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
01-03 06:16:15.254: E/ActivityThread(616): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
01-03 06:16:15.254: E/ActivityThread(616): at java.lang.Thread.run(Thread.java:856)
01-03 06:16:15.365: E/StrictMode(616): null
01-03 06:16:15.365: E/StrictMode(616): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d47b40 that was originally bound here
01-03 06:16:15.365: E/StrictMode(616): at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
01-03 06:16:15.365: E/StrictMode(616): at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
01-03 06:16:15.365: E/StrictMode(616): at android.app.ContextImpl.bindService(ContextImpl.java:1418)
01-03 06:16:15.365: E/StrictMode(616): at android.app.ContextImpl.bindService(ContextImpl.java:1407)
01-03 06:16:15.365: E/StrictMode(616): at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
01-03 06:16:15.365: E/StrictMode(616): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
01-03 06:16:15.365: E/StrictMode(616): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
01-03 06:16:15.365: E/StrictMode(616): at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191)
01-03 06:16:15.365: E/StrictMode(616): at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850)
01-03 06:16:15.365: E/StrictMode(616): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
01-03 06:16:15.365: E/StrictMode(616): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
01-03 06:16:15.365: E/StrictMode(616): at android.os.AsyncTask$2.call(AsyncTask.java:287)
01-03 06:16:15.254:E/ActivityThread(616):Service com.android.exchange.ExchangeService已泄漏ServiceConnection com.android.emailcommon.Service.ServiceProxy$ProxyConnection@40d47b40原来是订在这里的
01-03 06:16:15.254:E/ActivityThread(616):android.app.ServiceConnection泄漏:Service com.android.exchange.ExchangeService已泄漏ServiceConnection com.android.emailcommon.Service.ServiceProxy$ProxyConnection@40d47b40原来是订在这里的
01-03 06:16:15.254:E/ActivityThread(616):位于android.app.LoadedAppk$ServiceDispatcher(LoadedAppk.java:969)
01-03 06:16:15.254:E/ActivityThread(616):在android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
01-03 06:16:15.254:E/ActivityThread(616):位于android.app.ContextImpl.bindService(ContextImpl.java:1418)
01-03 06:16:15.254:E/ActivityThread(616):位于android.app.ContextImpl.bindService(ContextImpl.java:1407)
01-03 06:16:15.254:E/ActivityThread(616):位于android.content.ContextWrapper.bindService(ContextWrapper.java:473)
01-03 06:16:15.254:E/ActivityThread(616):位于com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
01-03 06:16:15.254:E/ActivityThread(616):位于com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
01-03 06:16:15.254:E/ActivityThread(616):位于com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191)
01-03 06:16:15.254:E/ActivityThread(616):位于com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850)
01-03 06:16:15.254:E/ActivityThread(616):位于com.android.emailcommon.utility.utility$2.doInBackground(utility.java:551)
01-03 06:16:15.254:E/ActivityThread(616):位于com.android.emailcommon.utility.utility$2.doInBackground(utility.java:549)
01-03 06:16:15.254:E/ActivityThread(616):在android.os.AsyncTask$2.call(AsyncTask.java:287)
01-03 06:16:15.254:E/ActivityThread(616):位于java.util.concurrent.FutureTask.run(FutureTask.java:234)
01-03 06:16:15.254:E/ActivityThread(616):位于java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
01-03 06:16:15.254:E/ActivityThread(616):位于java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
01-03 06:16:15.254:E/ActivityThread(616):在java.lang.Thread.run(Thread.java:856)上
01-03 06:16:15.365:E/StrictMode(616):空
01-03 06:16:15.365:E/StrictMode(616):android.app.ServiceConnection泄漏:Service com.android.exchange.ExchangeService已泄漏ServiceConnection com.android.emailcommon.Service.ServiceProxy$ProxyConnection@40d47b40原来是订在这里的
01-03 06:16:15.365:E/StrictMode(616):在android.app.LoadedApk$ServiceDispatcher上。(LoadedApk.java:969)
01-03 06:16:15.365:E/StrictMode(616):在android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
01-03 06:16:15.365:E/StrictMode(616):位于android.app.ContextImpl.bindService(ContextImpl.java:1418)
01-03 06:16:15.365:E/StrictMode(616):位于android.app.ContextImpl.bindService(ContextImpl.java:1407)
01-03 06:16:15.365:E/StrictMode(616):位于android.content.ContextWrapper.bindService(ContextWrapper.java:473)
01-03 06:16:15.365:E/StrictMode(616):位于com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
01-03 06:16:15.365:E/StrictMode(616):位于com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
01-03 06:16:15.365:E/StrictMode(616):位于com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191)
01-03 06:16:15.365:E/StrictMode(616):位于com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850)
01-03 06:16:15.365:E/stricmode(616):在com.android.emailcommon.utility.utility$2.doInBackground(utility.java:551)
01-03 06:16:15.365:E/stricmode(616):在com.android.emailcommon.utility.utility$2.doInBackground(utility.java:549)
01-03 06:16:15.365:E/StrictMode(616):在android.os.AsyncTask$2.call(AsyncTask.java:287)
AsyncTask.class
class LoadMovies extends AsyncTask<Void, Void, List<Category>>{
private YourActivity activity;
public LoadMovies(YourActivity activty){
activity = activty;
}
private final static String TAG = "Log";
@Override
protected List<Category> doInBackground(Void... params) {
InputStream ips = null;
JSONObject json = null;
try{
DefaultHttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(new HttpGet("http://youtsite"));
ips = response.getEntity().getContent();
BufferedReader buffer = new BufferedReader(new InputStreamReader(ips));
StringBuilder builder = new StringBuilder();
String line;
while((line = buffer.readLine()) != null){
builder.append(line+"\n");
}
json = new JSONObject(builder.toString());
}
catch(Exception e){
Log.i(TAG, e.toString());
}
List<Category> list = new ArrayList<Category>();
JSONArray array = json.optJSONObject("data").optJSONArray("reviews");
for (int i = 0; i < array.length(); i++){
JSONObject rss = array.optJSONObject(i);
Category cat = new Category();
cat.addName(rss.optString("name"));
cat.addCode(rss.optString("code"));
list.add(cat);
}
return list;
}
protected void onPostExecute(List<Category> result){
super.onPostExecute(result);
Youractivity.mMoviesAdapter.addCategory(result);
}
}
public class Category {
private String name;
private String code;
void addName(String name){
this.name = name;
}
void addCode(String code){
this.code = code;
}
public String getName(){
return this.name;
}
String getCode(){
return this.code;
}
}
适配器类
class MoviesAdapter extends ArrayAdapter<Category> {
Context c;
List<Category> mListCategory;
public MoviesAdapter(Context context, int resource, int textViewResource, List<Categoryy> list) {
super(context, resource, textViewResource, list);
c = context;
mListCategory= list;
}
public void addCategory(List<MoviesCategory> list){
mListCategory.addAll(list);
notifyDataSetChanged();
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
View v = convertView;
if(v == null){
LayoutInflater inflater = LayoutInflater.from(c);
v = inflater.inflate(R.layout.yourview, null);
}
TextView title = (TextView) v.findViewById(R.id.title);
TextView code= (TextView) v.findViewById(R.id.code);
Category cat = getItem(position);
if(mov != null){
title.setText(cat.getName());
code.setText(cat.getCode());
}
return v;
}
}
class MoviesAdapter扩展了ArrayAdapter{
上下文c;
列表类别;
公共MoviesAdapter(上下文上下文、int资源、int文本视图资源、列表){
超级(上下文、资源、textViewResource、列表);
c=上下文;
mListCategory=列表;
}
公共无效添加类别(列表){
mListCategory.addAll(列表);
notifyDataSetChanged();
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图v=转换视图;
如果(v==null){
LayoutInflater充气机=来自(c)的LayoutInflater;
v=充气机。充气(R.layout.yourview,null);
}
TextView title=(TextView)v.findViewById(R.id.title);
TextView代码=(TextView)v.findViewById(R.id.code);
类别cat=getItem(位置);
如果(mov!=null){
title.setText(cat.getName());
code.setText(cat.getCode());
}
返回v;
}
}
在main活动中,调用所有
public class YourActivity extends MainActivity{
private ListView mCategory;
private List<Category> mCategoryArray = new ArrayList<Category>();
private LoadMovies mLoadMovies;
public mMoviesAdapter adapter;
@Override
protected void onCreate(Bundle bundle){
super.onCreate(bundle);
//MoviesList
mMoviesAdapter = new MoviesAdapter(this, R.layout.main_movies_adapter, R.id.youradapterlayout, mCategoryArray );
mCategoryList = (ListView) findViewById(R.id.movies_adapter_list);
mMoviesList.setAdapter(mMoviesAdapter);
mLoadMovies = new LoadMovies(this);
mLoadMovies.execute();
}
}
公共类YourActivity扩展了MainActivity{
私有ListView McCategory;
私有列表mCategoryArray=new ArrayList();
私人下载电影;
公共mMoviesAdapter适配器;
@凌驾
创建时受保护的void(捆绑){
super.onCreate(bundle);
//电影名单
姆莫维萨达普