Java 在listview中加载图像
我有一个使用json获取链接和文本的应用程序,现在我想从Listview中下载的url(通过json)加载图像 我可以在listview中加载文本,但我可以加载图像 请帮帮我,我花了2天的时间来搜索,。。。但是没有机会!我真的需要你的帮助!任何人都可以帮助我,我会在申请时非常感谢他。请帮帮我。 我的代码:Java 在listview中加载图像,java,android,image,listview,Java,Android,Image,Listview,我有一个使用json获取链接和文本的应用程序,现在我想从Listview中下载的url(通过json)加载图像 我可以在listview中加载文本,但我可以加载图像 请帮帮我,我花了2天的时间来搜索,。。。但是没有机会!我真的需要你的帮助!任何人都可以帮助我,我会在申请时非常感谢他。请帮帮我。 我的代码: package rappage.rapfarsi.media.appteam.rapnews; import android.app.AlertDialog; import android
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);
}
}
package rappage.rapfarsi.media.appteam.rapnews;
导入android.app.AlertDialog;
导入android.app.ProgressDialog;
导入android.content.DialogInterface;
导入android.content.Intent;
导入android.graphics.Bitmap;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.support.annotation.Nullable;
导入android.support.v4.app.ListFragment;
导入android.util.Log;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.ListAdapter;
导入android.widget.ListView;
导入android.widget.simpledapter;
导入android.widget.TextView;
导入android.widget.Toast;
导入org.apache.http.NameValuePair;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.List;
导入rappage.rapfarsi.media.appteam.ArticleActivity;
导入rappage.rapfarsi.media.appteam.Main;
导入rappage.rapfarsi.media.appteam.R;
导入rappage.rapfarsi.media.appteam.json.JSONParser;
公共类tab2扩展了ListFragment{
静态最终字符串url\u所有产品=”http://normaluse.persianrapapp021.ml/article/get_all_products.php";
//进度对话框
私人对话;
//创建JSON解析器对象
JSONParser jParser=新的JSONParser();
ArrayList productsList;
//获取所有产品列表的url
最终字符串标记_SUCCESS=“SUCCESS”;
最后一个字符串TAG_RAPNEWS=“article”;
最终字符串标记_PID=“PID”;
最终字符串标记_TITLE=“TITLE”;
最终字符串标记_PIC=“PIC”;
最终字符串标记_WRITER=“WRITER”;
位图bmp;
//JSON节点名称
//产品JSONArray
JSONArray rapnews=null;
创建视图时的公共视图(LayoutFlater充气机、@Nullable ViewGroup容器、@Nullable Bundle savedInstanceState){
视图v=充气机。充气(R.layout.tab_2,容器,假);
最终视图rootView=super.onCreateView(充气机、容器、savedInstanceState);
//ListView的Hashmap
productsList=新的ArrayList();
//在后台线程中加载产品
/**
*通过发出HTTP请求加载所有产品的后台异步任务
* */
试一试{
新建LoadAllProducts().execute();
}捕获(例外e)
{
final AlertDialog.Builder dlgAlert=new AlertDialog.Builder(getActivity());
15.5.半半半半个个头。半半个头。半半个头。半个头。半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半㶪;
setTitle(“连接错误!”);
dlgAlert.setNegativeButton(“باشهگفتم”),新的对话框界面。OnClickListener(){
public void onClick(DialogInterface对话框,int-id){
startActivity(新意图(getActivity(),Main.class));
}
});
dlgAlert.create().show();
}
返回v;
}
类LoadAllProducts扩展了AsyncTask{
/**
*在启动后台线程显示进度对话框之前
*/
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=newprogressdialog(getActivity());
setMessage(“正在加载,等待…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(假);
pDialog.show();
}
/**
*从url获取所有产品
*/
受保护的字符串doInBackground(字符串…args){
//建筑参数
List params=new ArrayList();
//从URL获取JSON字符串
JSONObject json=jParser.makeHttpRequest(url_all_products,“GET”,params);
//检查日志cat中的JSON响应
Log.d(“所有产品:,json.toString());
试一试{
//检查成功标签
int success=json.getInt(TAG_success);
如果(成功==1){
//发现的产品
//获取一系列产品
rapnews=json.getJSONArray(TAG_rapnews);
//在所有产品中循环
对于(int i=0;i<11;i++){
JSONObject c=rapnews.getJSONObject(i);
//将每个json项存储在变量中
String id=c.getString(TAG_PID);
String name=c.getString(TAG\u TITLE);
String pic=c.getString(TAG_pic);
//创建新的HashMap
HashMap=newHashMap();
//将每个子节点添加到HashMap key=>value
地图放置(标签PID,id);
地图放置(标签标题、名称);
地图放置(标签图片,图片);
//将哈希列表添加到ArrayList
productsList.add(地图);
}
}否则{
//没有发现任何产品
//启动添加新产品活动
意图i=新意图(getActivity().getApplicationContext(),
主要类别),;
//关闭以前的所有活动
i、 添加标志(意图、标志、活动、清除、顶部);
星触觉(i);
}
}捕获(JSONException e){
e、 printStackTrace();
}
返回null;
}
/**
*完成后台任务后,关闭“进度”对话框
* *
*/
受保护的void onPostExecute(字符串文件\u url){
//获取所有产品后关闭对话框
pDialog.discover
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
public class CustomArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values1;
private final String[] values2;
public CustomArrayAdapter(Context context, String[] values1, String[] values2) {
super(context, R.layout.some_layout, values1);
this.context = context;
this.values1 = values1;
this.values2 = values2;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
// LayoutInflater inflater = LayoutInflater.from(context);
// View rowView = inflater.inflate(R.layout.some_layout, null);
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = LayoutInflater.from(context);
rowView = inflater.inflate(R.layout.some_layout, null);
holder = new ViewHolder();
holder.text1 = (TextView) rowView.findViewById(R.id.firstLine);
holder.text2 = (TextView) rowView.findViewById(R.id.secondLine);
holder.img = (ImageView) rowView.findViewById(R.id.imageView);
rowView.setTag(holder);
}
else {
holder=(ViewHolder)rowView.getTag();
}
holder.text1.setText(values1[position]);
holder.text2.setText(values2[position]);
Picasso.with(rowView.getContext()).load("http://www.9ori.com/store/media/images/8ab579a656.jpg").into(holder.img);
// if (position%2==0) {
// holder.img.setImageResource(R.drawable.correct);
// }
// else {
// holder.img.setImageResource(R.drawable.incorrect);
// }
return rowView;
}
public static class ViewHolder {
public TextView text1, text2;
public ImageView img;
}
}