我想从json上读取url地址列表视图,但是还有显示。 这是我的代码。 主要活动。
package customlistviewvolley;
import com.example.customlistviewvolley.R;
/*import java.io.IOException;
import java.io.InputStream;*/
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.ListView;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonArrayRequest;
import customlistviewvolleyadater.CustomListAdapter;
import customlistviewvolleyapp.AppController;
import customlistviewvolleymodel.App;
public class MainActivity extends Activity {
// Log tag
private static final String TAG = MainActivity.class.getSimpleName();
// Movies json url
private static final String url = "http://192.168.1.5/public_html/android/new.json";
private ProgressDialog pDialog;
private List<App> appList = new ArrayList<App>();
private ListView listView;
private CustomListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list);
adapter = new CustomListAdapter(this, appList);
listView.setAdapter(adapter);
pDialog = new ProgressDialog(this);
// Showing progress dialog before making http request
pDialog.setMessage("Loading...");
pDialog.show();
// changing action bar color
getActionBar().setBackgroundDrawable(
new ColorDrawable(Color.parseColor("#1b1b1b")));
// Creating volley request obj
JsonArrayRequest appReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
hidePDialog();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
App app = new App();
app.setAppTitle(obj.getString("app_title"));
app.setAppImageUrl(obj.getString("app_image"));
app.setRatingBar(((Number) obj.get("app_rating"))
.doubleValue());
app.setAppCreator(obj.getString("app_creator"));
app.setAppCase(obj.getString("app_case"));
// adding app to app array
appList.add(app);
} catch (JSONException e) {
e.printStackTrace();
}
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(appReq);
}
/* JSONObject obj1 = new JSONObject(loadJSONFromAsset());
JSONArray m_jArry = obj1.getJSONArray("apps");
//ArrayList<HashMap<String, String>> formList= new ArrayList<HashMap<String, String>>();
// HashMap<String, String> m_li;
for (int i = 0; i < m_jArry.length(); i++) {
try {
JSONObject obj = m_jArry.getJSONObject(i);
App app = new App();
app.setAppTitle(obj.getString("app_title"));
app.setAppImageUrl(obj.getString("app_image"));
app.setRatingBar(((Number) obj.get("rating_bar"))
.doubleValue());
app.setAppCreator(obj.getString("app_creator"));
app.setAppCase(obj.getString("app_case"));
// adding movie to movies array
appList.add(app);
} catch (JSONException e) {
e.printStackTrace();
}
}
//Same way for other value...
}*/
/*public String loadJSONFromAsset() {
String json = null;
try {
InputStream is = getAssets().open("jsonfile.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
} */
@Override
public void onDestroy() {
super.onDestroy();
hidePDialog();
}
private void hidePDialog() {
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
自定义适配器。
package customlistviewvolleyadater;
import com.example.customlistviewvolley.R;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RatingBar;
import android.widget.TextView;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import customlistviewvolleyapp.AppController;
import customlistviewvolleymodel.App;
public class CustomListAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<App> appItems;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
public CustomListAdapter(Activity activity, List<App> appItems) {
this.activity = activity;
this.appItems = appItems;
}
@Override
public int getCount() {
return appItems.size();
}
@Override
public Object getItem(int location) {
return appItems.get(location);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.top_new_free, null);
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
NetworkImageView appImage = (NetworkImageView) convertView
.findViewById(R.id.app_image);
TextView appTitle = (TextView) convertView.findViewById(R.id.app_title);
TextView appCase = (TextView) convertView.findViewById(R.id.app_case);
TextView appCreator = (TextView) convertView.findViewById(R.id.app_creator);
RatingBar ratingBar = (RatingBar) convertView.findViewById(R.id.app_rating_bar);
// getting app data for the row
App app = appItems.get(position);
// app image
appImage.setImageUrl(app.getAppImageUrl(), imageLoader);
// title
appTitle.setText(app.getAppTitle());
// app creator
appCreator.setText( app.getAppCreator());
// appCase
appCase.setText(app.getAppCase());
// rating bar
ratingBar.setNumStars((int) app.getRatingBar());
return convertView;
}
}
app控制器。
package customlistviewvolleyapp;
import android.app.Application;
import android.text.TextUtils;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import customlistviewvolleyutil.LruBitmapCache;
public class AppController extends Application {
public static final String TAG = AppController.class.getSimpleName();
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static AppController mInstance;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
public static synchronized AppController getInstance() {
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
}
return mRequestQueue;
}
public ImageLoader getImageLoader() {
getRequestQueue();
if (mImageLoader == null) {
mImageLoader = new ImageLoader(this.mRequestQueue,
new LruBitmapCache());
}
return this.mImageLoader;
}
public <T> void addToRequestQueue(Request<T> req, String tag) {
// set the default tag if tag is empty
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
getRequestQueue().add(req);
}
public <T> void addToRequestQueue(Request<T> req) {
req.setTag(TAG);
getRequestQueue().add(req);
}
public void cancelPendingRequests(Object tag) {
if (mRequestQueue != null) {
mRequestQueue.cancelAll(tag);
}
}
}
应用程序。
package customlistviewvolleymodel;
public class App {
private String appTitle, appImageUrl, appCreator;
private double ratingBar;
private String appCase;
public App() {
}
public App(String appName, String appImageUrl, String appCreator, double ratingBar,
String appCase) {
this.appTitle = appName;
this.appImageUrl = appImageUrl;
this.appCreator = appCreator;
this.ratingBar = ratingBar;
this.appCase = appCase;
}
public String getAppTitle() {
return appTitle;
}
public void setAppTitle(String name) {
this.appTitle = name;
}
public String getAppImageUrl() {
return appImageUrl;
}
public void setAppImageUrl(String appImageUrl) {
this.appImageUrl = appImageUrl;
}
public String getAppCreator() {
return appCreator;
}
public void setAppCreator(String appCreator) {
this.appCreator = appCreator;
}
public double getRatingBar() {
return ratingBar;
}
public void setRatingBar(double ratingBar) {
this.ratingBar = ratingBar;
}
public String getAppCase() {
return appCase;
}
public void setAppCase(String appCase) {
this.appCase = appCase;
}
}
这是我的缓存代码。
` `package customlistviewvolleyutil;
import com.android.volley.toolbox.ImageLoader.ImageCache;
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
public class LruBitmapCache extends LruCache<String, Bitmap> implements
ImageCache {
public static int getDefaultLruCacheSize() {
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
final int cacheSize = maxMemory / 8;
return cacheSize;
}
public LruBitmapCache() {
this(getDefaultLruCacheSize());
}
public LruBitmapCache(int sizeInKiloBytes) {
super(sizeInKiloBytes);
}
@Override
protected int sizeOf(String key, Bitmap value) {
return value.getRowBytes() * value.getHeight() / 1024;
}
@Override
public Bitmap getBitmap(String url) {
return get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
put(url, bitmap);
}
}
我的主要活动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"
tools:context=".MainActivity" >
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="@android:color/transparent"
android:dividerHeight="10.0sp"
android:padding="5dip"
android:clipToPadding="false"
android:listSelector="@drawable/list_row_selector" />
</RelativeLayout>
我的行在这里。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="8dp" >
<!-- Thumbnail Image -->
<com.android.volley.toolbox.NetworkImageView
android:id="@+id/app_image"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentRight="true"
android:layout_marginRight="8dp" />
<!-- App(movie) Title -->
<TextView
android:id="@+id/app_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/app_image"
android:layout_toLeftOf="@+id/app_image"
android:textSize="@dimen/title"
android:textStyle="bold" />
<!-- App Creator -->
<TextView
android:id="@+id/app_creator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/app_image"
android:layout_toLeftOf="@+id/app_image"
android:textSize="@dimen/creator"
android:layout_marginTop="1dip"
android:textStyle="bold" />
<!-- Type Free or NotFree or Installed -->
<TextView
android:id="@+id/app_case"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:textColor="@color/year"
android:textSize="@dimen/type" />
<!-- Rating Bar -->
<RatingBar
android:id="@+id/app_rating_bar"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_toLeftOf="@+id/app_image"
android:layout_below="@+id/app_creator"
android:stepSize="0.5" />
</RelativeLayout>
请帮助我修复它。
答案 0 :(得分:0)
您应该以这种方式访问localhost站点:
http://localhost:8080/MyTestPage.html
阅读本文以获取更多信息: