我有一個應用程序,它使用json來獲取鏈接和文本,現在我想從列表視圖中下載的URL(通過json)加載圖像。在列表視圖中加載圖像
我可以在列表視圖中加載文本,但我可以加載圖像!
PLZ幫我,我已經花了2天時間搜尋這個......但沒機會!我真的需要你的幫助!任何人都可以幫助我,我會非常感謝他的應用。 PLZZZ幫助我。
我的代碼:
package rappage.rapfarsi.media.appteam.rapnews;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import rappage.rapfarsi.media.appteam.Articlectivity;
import rappage.rapfarsi.media.appteam.Main;
import rappage.rapfarsi.media.appteam.R;
import rappage.rapfarsi.media.appteam.json.JSONParser;
public class tab2 extends ListFragment {
static final String url_all_products = "http://normaluse.persianrapapp021.ml/article/get_all_products.php";
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> productsList;
// url to get all products list
final String TAG_SUCCESS = "success";
final String TAG_RAPNEWS = "article";
final String TAG_PID = "pid";
final String TAG_TITLE = "title";
final String TAG_PIC = "pic";
final String TAG_WRITER = "writer";
Bitmap bmp;
// JSON Node names
// products JSONArray
JSONArray rapnews = null;
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.tab_2, container, false);
final View rootView = super.onCreateView(inflater, container, savedInstanceState);
// Hashmap for ListView
productsList = new ArrayList<HashMap<String, String>>();
// Loading products in Background Thread
/**
* Background Async Task to Load all product by making HTTP Request
* */
try {
new LoadAllProducts().execute();
}catch (Exception e)
{
final AlertDialog.Builder dlgAlert = new AlertDialog.Builder(getActivity());
dlgAlert.setMessage("لطفا اینترنت خودرا چک کنید! یا ممکن است سرور از دسترس خارج شده باشد ، برای اخبار اپدیت و مشکلات به ویکی رپ مراجعه کنید!");
dlgAlert.setTitle("Connection Error!");
dlgAlert.setNegativeButton("باشه گرفتم", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
startActivity(new Intent(getActivity() , Main.class));
}
});
dlgAlert.create().show();
}
return v;
}
class LoadAllProducts extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
*/
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Loading, Wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All products from url
*/
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
// Check your log cat for JSON reponse
Log.d("All Products: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
rapnews = json.getJSONArray(TAG_RAPNEWS);
// looping through All Products
for (int i = 0; i < 11; i++) {
JSONObject c = rapnews.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_PID);
String name = c.getString(TAG_TITLE);
String pic = c.getString(TAG_PIC);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_PID, id);
map.put(TAG_TITLE, name);
map.put(TAG_PIC, pic);
// adding HashList to ArrayList
productsList.add(map);
}
} else {
// no products found
// Launch Add New product Activity
Intent i = new Intent(getActivity().getApplicationContext(),
Main.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 products
pDialog.dismiss();
// updating UI from Background Thread
getActivity().runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
getActivity(), productsList,
R.layout.list_item, new String[]{TAG_PID,
TAG_TITLE},
new int[]{R.id.pid, R.id.txttitle}
);
setListAdapter(adapter);
}
});
}
}
public void onListItemClick(ListView l, View v, int position, long id) {
Toast.makeText(getActivity().getApplicationContext(), "Loading, Please Wait...",
Toast.LENGTH_LONG).show();
String pid = ((TextView) v.findViewById(R.id.pid)).getText()
.toString();
// Starting new intent
Intent in = new Intent(getActivity(),Articlectivity.class);
// sending pid to next activity
in.putExtra(TAG_PID, pid);
// starting new activity and expecting some response back
startActivity(in);
}
}
我想ATLEAST加載圖像在此基礎上......或者任何其它的事情...
以及它是一個單一的imageveiw!但我有一個listadapter它可以是100張圖片!該怎麼辦?我可以在listadapter中使用picasso嗎? – AliAk
是的,你可以這樣做 – vaibhav
這是一個自定義數組適配器,用於具有兩個文本和圖像的列表視圖。 – vaibhav