我是新android开发,我解析使用JSON解析方法我的数据,我致以类目录片段,我想我的列表视图数据,但问题是我收到的所有数据完全不同的图像,我不知道如何解决这个问题,我的回答是这样的
{匹配:[{名:单胞菌Dano","profile_id":"GM335695","image":"http://mywebsitename.com/images/Girlnoimage.jpg","cast":"","age":"24","location":"Ivory海岸}]}
公共类HomeFragment扩展ListFragment {
// CustomAdapter适配器;
//私有列表< RowItem> rowItems;
私人ProgressDialog pDialog;
// JSON解析器类
JSONParser jsonParser =新JSONParser();
JSONArray匹配= NULL;
ArrayList的< HashMap的<字符串,字符串>>一个列表;
私有静态字符串MATCH_URL = NULL;
私有静态最后弦乐TAG_MATCH =匹配;
私有静态最后弦乐TAG_NAME =名;
私有静态最后弦乐TAG_PROFILE =PROFILE_ID;
私有静态最后弦乐TAG_IMAGE =形象;
私有静态最后弦乐TAG_CAST =投;
私有静态最后弦乐TAG_AGE =时代;
私有静态最后弦乐TAG_LOCATION =地利;
私人的ListView列表视图;
公共HomeFragment(){}
@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
捆绑savedInstanceState){
。串strText的= getArguments()的getString(user_login_id);
MATCH_URL =http://mywebsitename.com/webservice/matching?version=apps&user_login_id="+strtext;
查看rootView = inflater.inflate(R.layout.fragment_home,集装箱,假);
ALIST =新的ArrayList< HashMap的<字符串,字符串>>();
// rowItems =新的ArrayList< RowItem>();
列表视图=(ListView控件)rootView.findViewById(android.R.id.list);
新LoadAlbums()执行();
返回rootView;
}
类LoadAlbums扩展的AsyncTask<字符串,字符串,字符串> {
/ **
*在启动后台线程显示进度对话框
* * /
@覆盖
在preExecute保护无效(){
super.on preExecute();
pDialog =新ProgressDialog(HomeFragment.this.getActivity());
pDialog.setMessage(载入中...);
pDialog.setIndeterminate(假);
pDialog.setCancelable(假);
pDialog.show();
}
保护字符串doInBackground(字符串参数... args){
ServiceHandler SH =新ServiceHandler();
//制作一个请求URL并得到响应
字符串jsonStr = sh.makeServiceCall(MATCH_URL,ServiceHandler.GET);
Log.d(回应:,>中+ jsonStr);
如果(jsonStr!= NULL){
尝试 {
JSONObject的jsonObj =新的JSONObject(jsonStr);
//获取JSON数组节点
匹配= jsonObj.getJSONArray(TAG_MATCH);
//遍历所有联系人
的for(int i = 0; I< matching.length();我++){
JSONObject的C = matching.getJSONObject(我);
//保存在变量中的每个JSON项目的值
字符串用户名= c.getString(TAG_NAME);
字符串USER_PROFILE = c.getString(TAG_PROFILE);
字符串user_image = c.getString(TAG_IMAGE);
字符串user_cast = c.getString(TAG_CAST);
字符串user_age = c.getString(TAG_AGE);
字符串user_location = c.getString(TAG_LOCATION);
//创建新的HashMap
HashMap的<字符串,字符串>图=新的HashMap<字符串,字符串>();
//添加每个子节点HashMap中的key =>值
map.put(TAG_NAME,用户名);
map.put(TAG_PROFILE,USER_PROFILE);
map.put(TAG_IMAGE,user_image);
map.put(TAG_CAST,user_cast);
map.put(TAG_AGE,user_age +年);
map.put(TAG_LOCATION,user_location);
//添加HashList到ArrayList中
aList.add(图)
}
}赶上(JSONException E){
e.printStackTrace();
}
} 其他 {
Log.e(ServiceHandler,无法从URL中得到任何数据);
}
返回null;
}
保护无效onPostExecute(字符串file_url){
super.onPostExecute(file_url);
让所有专辑后//关闭该对话框
如果(pDialog.isShowing())
pDialog.dismiss();
//从后台线程更新UI
/ **
*更新解析JSON数据到ListView控件
* * /
//更新列表视图
CustomAdapter适配器=新CustomAdapter(getActivity(),ALIST);
setListAdapter(适配器);
}
}
}
解决方案
尝试的 AndroidQuery 自定义适配器:
公共类CustomAdapter扩展了BaseAdapter {
私人上下文的背景下;
私人的ArrayList< HashMap的<字符串,字符串>>的ListData;
私人AQuery aQuery;
私有静态最后弦乐TAG_NAME =名;
私有静态最后弦乐TAG_PROFILE =PROFILE_ID;
私有静态最后弦乐TAG_IMAGE =形象;
私有静态最后弦乐TAG_CAST =投;
私有静态最后弦乐TAG_AGE =时代;
私有静态最后弦乐TAG_LOCATION =地利;
公共CustomAdapter(上下文的背景下,ArrayList的< HashMap的<字符串,字符串>>的ListData){
this.context =背景;
this.listData =的ListData;
aQuery =新AQuery(this.context);
}
@覆盖
公众诠释getCount将(){
返回listData.size();
}
@覆盖
公共对象的getItem(INT位置){
返回listData.get(位置);
}
@覆盖
众长getItemId(INT位置){
返回的位置;
}
@覆盖
公共查看getView(最终诠释的立场,观点convertView,ViewGroup中父){
ViewHolder持有人;
如果(convertView == NULL){
持有人=新ViewHolder();
convertView = LayoutInflater.from(上下文).inflate(R.layout.list_item,NULL);
holder.propic =(ImageView的)convertView.findViewById(R.id.propic);
holder.txtproname =(TextView中)convertView.findViewById(R.id.txtproname);
holder.txtproid =(TextView中)convertView.findViewById(R.id.txtproid);
holder.txtprofilecast =(TextView中)convertView.findViewById(R.id.txtprofilecast);
holder.txtprofileage =(TextView中)convertView.findViewById(R.id.txtprofileage);
holder.txtprofileplace =(TextView中)convertView.findViewById(R.id.txtprofileplace);
convertView.setTag(保持器);
}其他{
支架=(ViewHolder)convertView.getTag();
}
holder.txtproname.setText(listData.get(位置)获得(TAG_NAME));
holder.txtproid.setText(listData.get(位置)获得(TAG_PROFILE));
holder.txtprofilecast.setText(listData.get(位置)获得(TAG_CAST));
holder.txtprofileage.setText(listData.get(位置)获得(TAG_AGE));
holder.txtprofileplace.setText(listData.get(位置)获得(TAG_LOCATION));
aQuery.id(holder.propic).image(listData.get(position).get(TAG_IMAGE),true,true,0,R.drawable.ic_launcher);
//图片参数:1:内存缓存,2:文件缓存,3:目标宽度,4:后备图片
返回convertView;
}
类ViewHolder {
ImageView的propic;
TextView的txtproname;
TextView的txtproid;
TextView的txtprofilecast;
TextView的txtprofileage;
TextView的txtprofileplace;
}
}
如何设置适配器的ListView:
CustomAdapter适配器=新CustomAdapter(getActivity(),ALIST);
setListAdapter(适配器);
I am new to android development,I am parsing my data using JSON Parsing method,I extend my class with List Fragment and I want my data in list view but the problem is i am getting all the data perfectly except the images,i don't know how to solve it,my response looks like this
{"matching":[{"name":"Monic Dano","profile_id":"GM335695","image":"http://mywebsitename.com/images/Girlnoimage.jpg","cast":"","age":"24","location":"Ivory Coast"}]}
public class HomeFragment extends ListFragment {
//CustomAdapter adapter;
//private List<RowItem> rowItems;
private ProgressDialog pDialog;
//JSON parser class
JSONParser jsonParser = new JSONParser();
JSONArray matching=null;
ArrayList<HashMap<String,String>> aList;
private static String MATCH_URL = null;
private static final String TAG_MATCH="matching";
private static final String TAG_NAME="name";
private static final String TAG_PROFILE="profile_id";
private static final String TAG_IMAGE="image";
private static final String TAG_CAST="cast";
private static final String TAG_AGE="age";
private static final String TAG_LOCATION="location";
private ListView listview;
public HomeFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String strtext = getArguments().getString("user_login_id");
MATCH_URL = "http://mywebsitename.com/webservice/matching?version=apps&user_login_id="+strtext;
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
aList = new ArrayList<HashMap<String,String>>();
// rowItems = new ArrayList<RowItem>();
listview=(ListView)rootView.findViewById(android.R.id.list);
new LoadAlbums().execute();
return rootView;
}
class LoadAlbums extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(HomeFragment.this.getActivity());
pDialog.setMessage("Loading...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... args) {
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(MATCH_URL, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
matching = jsonObj.getJSONArray(TAG_MATCH);
// looping through All Contacts
for (int i = 0; i < matching.length(); i++) {
JSONObject c = matching.getJSONObject(i);
// Storing each json item values in variable
String user_name = c.getString(TAG_NAME);
String user_profile=c.getString(TAG_PROFILE);
String user_image=c.getString(TAG_IMAGE);
String user_cast=c.getString(TAG_CAST);
String user_age=c.getString(TAG_AGE);
String user_location=c.getString(TAG_LOCATION);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_NAME,user_name);
map.put(TAG_PROFILE, user_profile);
map.put(TAG_IMAGE, user_image);
map.put(TAG_CAST, user_cast);
map.put(TAG_AGE, user_age+" years");
map.put(TAG_LOCATION, user_location);
// adding HashList to ArrayList
aList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
protected void onPostExecute(String file_url) {
super.onPostExecute(file_url);
// dismiss the dialog after getting all albums
if (pDialog.isShowing())
pDialog.dismiss();
// updating UI from Background Thread
/**
* Updating parsed JSON data into ListView
* */
// updating listview
CustomAdapter adapter = new CustomAdapter(getActivity(),aList);
setListAdapter(adapter);
}
}
}
解决方案
Try to AndroidQuery with custom adapter :
public class CustomAdapter extends BaseAdapter {
private Context context;
private ArrayList<HashMap<String,String>> listData;
private AQuery aQuery;
private static final String TAG_NAME="name";
private static final String TAG_PROFILE="profile_id";
private static final String TAG_IMAGE="image";
private static final String TAG_CAST="cast";
private static final String TAG_AGE="age";
private static final String TAG_LOCATION="location";
public CustomAdapter(Context context,ArrayList<HashMap<String,String>> listData) {
this.context = context;
this.listData=listData;
aQuery = new AQuery(this.context);
}
@Override
public int getCount() {
return listData.size();
}
@Override
public Object getItem(int position) {
return listData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.list_item, null);
holder.propic = (ImageView) convertView.findViewById(R.id.propic);
holder.txtproname = (TextView) convertView.findViewById(R.id.txtproname);
holder.txtproid = (TextView) convertView.findViewById(R.id.txtproid);
holder.txtprofilecast = (TextView) convertView.findViewById(R.id.txtprofilecast);
holder.txtprofileage = (TextView) convertView.findViewById(R.id.txtprofileage);
holder.txtprofileplace = (TextView) convertView.findViewById(R.id.txtprofileplace);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.txtproname.setText(listData.get(position).get(TAG_NAME));
holder.txtproid.setText(listData.get(position).get(TAG_PROFILE));
holder.txtprofilecast.setText(listData.get(position).get(TAG_CAST));
holder.txtprofileage.setText(listData.get(position).get(TAG_AGE));
holder.txtprofileplace.setText(listData.get(position).get(TAG_LOCATION));
aQuery.id(holder.propic).image(listData.get(position).get(TAG_IMAGE),true,true,0,R.drawable.ic_launcher);
// image parameter : 1 : memory cache,2:file cache,3:target width,4:fallback image
return convertView;
}
class ViewHolder{
ImageView propic;
TextView txtproname;
TextView txtproid;
TextView txtprofilecast;
TextView txtprofileage;
TextView txtprofileplace;
}
}
How to set adapter to ListView :
CustomAdapter adapter = new CustomAdapter(getActivity(),aList);
setListAdapter(adapter);