我是Android的新手,创建了具有不同用户的数据库,每个用户都有图像URL,现在想从特定用户的数据库中检索图像URL,并在Android列表视图中显示。
公共类 SnapflowActivity 扩展了 ListActivity {
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> imagesList;
private static String url_all_images = "PHP FILE URL";
private static final String TAG_SUCCESS = "success";
private static final String TAG_ALBUM_PICS = "album_pics";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
// images JSONArray
JSONArray album_pics = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
}
super.onCreate(savedInstanceState);
setContentView(R.layout.snapflow);
// Hashmap for ListView
imagesList = new ArrayList<HashMap<String, String>>();
// Loading images in Background Thread
new LoadAllImages().execute();
ListView lv = getListView();
}
/**
* Background Async Task to Load all Images by making HTTP Request
* */
class LoadAllImages extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(SnapflowActivity.this);
pDialog.setMessage("Loading Images. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All images from url
* */
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_images, "GET",
params);
// Check your log cat for JSON reponse
Log.d("All images: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of images
album_pics = json.getJSONArray(TAG_ALBUM_PICS);
// looping through All images
for (int i = 0; i < album_pics.length(); i++) {
JSONObject c = album_pics.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
String name = c.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);
// adding HashList to ArrayList
imagesList.add(map);
}
} else {
// no images found
// Launchnew activity Activity
Intent i = new Intent(getApplicationContext(),
SnapflowActivity.class);
// Closing all previous activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all images
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
SnapflowActivity.this, imagesList,
R.layout.list_image, new String[] { TAG_ID,
TAG_NAME }, new int[] { R.id.id,
R.id.imageview1 });
// updating listview
setListAdapter(adapter);
}
});
}
}
}
好的,
为了做你想做的事,我稍微修改了你的类并创建了一些额外的类。
编辑类:
public class SnapflowActivity extends ListActivity {
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<ParseItem> imagesList;
private static String url_all_images = "PHP FILE URL";
private static final String TAG_SUCCESS = "success";
private static final String TAG_ALBUM_PICS = "album_pics";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
// images JSONArray
JSONArray album_pics = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
}
super.onCreate(savedInstanceState);
setContentView(R.layout.snapflow);
// Hashmap for ListView
imagesList = new ArrayList<ParseItem>();
// Loading images in Background Thread
new LoadAllImages().execute();
ListView lv = getListView();
}
/**
* Background Async Task to Load all Images by making HTTP Request
* */
class LoadAllImages extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(SnapflowActivity.this);
pDialog.setMessage("Loading Images. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All images from url
* */
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_images, "GET",
params);
// Check your log cat for JSON reponse
Log.d("All images: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of images
album_pics = json.getJSONArray(TAG_ALBUM_PICS);
// looping through All images
for (int i = 0; i < album_pics.length(); i++) {
JSONObject c = album_pics.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
//I CREATED A CLASS ParseItem to CREATE THE LIST YOU WANT
imagesList.add(new ParseItem(id, name));
}
} else {
// no images found
// Launchnew activity Activity
Intent i = new Intent(getApplicationContext(),
SnapflowActivity.class);
// Closing all previous activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all images
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new MyCustomAdapter(SnapflowActivity.this, R.layout.list_item, imagesList);
// updating listview
setListAdapter(adapter);
}
});
}
}
}
自定义适配器:
public class MyCustomAdapter extends ArrayAdapter<ParseItem>{
public MyCustomAdapter(Context context, int resource, List<ParseItem> objects) {
super(context, resource, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolderItems holder;
View rootView = null;
ParseItem current = null;
if(rootView == null){
LayoutInflater li = LayoutInflater.from(getContext());
rootView = li.inflate(R.layout.list_item, parent, false);
current = getItem(position);
holder = new ViewHolderItems();
holder.id = (TextView)rootView.findViewById(R.id.textViewForList);
holder.name = (ImageView)rootView.findViewById(R.id.imageForList);
rootView.setTag(holder);
}else{
holder = (ViewHolderItems)rootView.getTag();
}
if(current != null){
holder.id.setText(current.getId());
Ion.with(holder.name).placeholder(R.drawable.ic_launcher).error(R.drawable.ic_launcher).load(current.getName());
}
return rootView;
}
static class ViewHolderItems {
TextView id;
ImageView name;
}
}
ParseItem 类:
public class ParseItem {
private String id, name;
public ParseItem(String id, String name) {
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
每个列表视图行的list_item.xml布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:padding="10dp"
android:id="@+id/imageForList" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New Text"
android:padding="25dp"
android:id="@+id/textViewForList" />
</LinearLayout>
为了以简单的方法获取图像,请编译以下依赖项:
compile 'com.koushikdutta.ion:ion:2.+'
希望对您有所帮助!!!