尝试通过自定义适配器解析json时,我得到一个空白屏幕。
我几乎可以肯定这是由于名为oslist
的arraylist问题我正在从一个例子开始,你每次都传递一个索引/变量(但这是一个列表视图,我现在正在尝试一个gridview),但我跟随gridview的教程将整个数组传递给适配器。
我希望从json文件
接收路径列表这些路径是指向图像的链接
这些图片需要在我的应用程序中的gridview中显示
我正在使用毕加索来处理图像(而不是它应该有所作为
MainActivity
package com.example.alex.jsonparsegridview;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
public class MainActivity extends Activity {
GridView gridView;
static final String[] MOBILE_OS = new String[]{
"Android", "iOS", "Windows", "Blackberry"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new JSONParse().execute();
}
private class JSONParse extends AsyncTask<String, String, JSONArray> {
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Getting Data ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected JSONArray doInBackground(String... args) {
JSONParser jParser = new JSONParser();
// Getting JSON from URL
String url = "http://www.500kgiveaway.co.uk/getimagesmob.php";
JSONArray json = jParser.getJSONFromUrl(url);
return json;
}
@Override
protected void onPostExecute(JSONArray json) {
pDialog.dismiss();
try {
// Getting JSON Array from URL
JSONArray android = json;
String TAG_PATH = "path";
ArrayList<HashMap<String, String>> oslist = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < android.length(); i++) {
JSONObject c = android.getJSONObject(i);
// Storing JSON item in a Variable
String path = c.getString(TAG_PATH);
// Adding value HashMap key => value
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_PATH, path);
oslist.add(map);
}
//copied from onCreate
gridView = (GridView) findViewById(R.id.gridView1);
gridView.setAdapter(new ImageAdapter(MainActivity.this, oslist));
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(
getApplicationContext(),
((TextView) v.findViewById(R.id.grid_item_label))
.getText(), Toast.LENGTH_SHORT).show();
}
});
//End Copy
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
ImageAdaptor
package com.example.alex.jsonparsegridview;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ImageAdapter extends BaseAdapter {
private Context context;
public ImageAdapter(Context context, List<? extends Map<String, ?>> data) {
this.context = context;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
// get layout from mobile.xml
gridView = inflater.inflate(R.layout.mobile, null);
// set value into textview
TextView textView = (TextView) gridView
.findViewById(R.id.grid_item_label);
textView.setText(((Map)getItem(position)).get("path").toString());
// set image based on selected text
ImageView imageView = (ImageView) gridView
.findViewById(R.id.grid_item_image);
Picasso.with(context).load("http://www.500kgiveaway.co.uk/" + ((Map) getItem(position)).get("path").toString()).into(imageView);
} else {
gridView = (View) convertView;
}
return gridView;
}
@Override
public int getCount() {
return 0;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
}
答案 0 :(得分:2)
getCount
应该返回数据集的大小,而不是0.否则不会调用getView
。 getItem
应该返回数据集位置的项目,而不是null。