请指导以下代码出了什么问题,该代码未创建正确的列表活动,包括动态图像及其来自服务器的描述
输出.这是图片
就像这样
可以看出,仅加载了一张没有说明的图像,其余的甚至没有空间也没有图像,请指导xml和代码出了什么问题.
这是代码
主班
public class Test extends ListActivity { @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.Listplaceholder); // install handler for processing gui update messages ArrayList<HashMap<String, Object>> myList = new ArrayList<HashMap<String, Object>>(); JsONObject Json = JsONfunctions.getJsONfromURL("http://mIDsweden.gofreeserve.com/proj/androIDJson.PHP?IDentifIEr=123" ); try{ JsONArray earthquakes = Json.getJsONArray("services"); for(int i=0;i<earthquakes.length();i++){ HashMap<String, Object> map = new HashMap<String, Object>(); JsONObject e = earthquakes.getJsONObject(i); map.put("ID", e.getString("taskID")); map.put("pic", "Service name : " + e.getString("employeepic")); map.put("serviceinfo", "" + e.getString("employeename")+ " : "+ e.getString("starttime") +" To " + e.getString("endtime")); String imageID = e.getString("employeepic"); ImageVIEw iv = (ImageVIEw) findVIEwByID(R.ID.image); Bitmap b = getUserPic(imageID); iv.setimageBitmap(b); map.put("img",b); myList.add(map); } }catch(JsONException e) { Log.e("log_tag", "Error parsing data "+e.toString()); } SimpleAdapter adapter = new SimpleAdapter(this, myList , R.layout.test,new String[] {"img", "servicename", "serviceinfo" }, new int[] { R.ID.image ,R.ID.items, R.ID.item_subTitle }); setlistadapter(adapter); final ListVIEw lv = getListVIEw(); lv.setTextFilterEnabled(true); lv.setonItemClickListener(new OnItemClickListener() { public voID onItemClick(AdapterVIEw<?> parent, VIEw vIEw, int position, long ID) { @SuppressWarnings("unchecked") HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtposition(position); Toast.makeText(Test.this, "ID '" + o.get("ID") + "' was clicked.", Toast.LENGTH_SHORT).show(); } }); } public Bitmap getUserPic(String picID) { String imageURL; Bitmap bitmap = null; Log.d("BITMAP", "Loading Picture"); imageURL = "http://mIDsweden.gofreeserve.com/proj/admin/"+picID; try { bitmap = BitmapFactory.decodeStream((inputStream)new URL(imageURL).getContent()); } catch (Exception e) { Log.d("BITMAP", "Loading Picture Failed"); e.printstacktrace(); } return bitmap; }}
这是我认为需要发布的Jsonfunction类,只是为了让其他人理解
public class JsONfunctions { public static JsONObject getJsONfromURL(String url){ inputStream is = null; String result = ""; JsONObject jArray = null; //http post try{ httpClIEnt httpclIEnt = new DefaulthttpClIEnt(); httpPost httppost = new httpPost(url); httpResponse response = httpclIEnt.execute(httppost); httpentity entity = response.getEntity(); is = entity.getContent(); }catch(Exception e){ Log.e("log_tag", "Error in http connection "+e.toString()); } //convert response to string try{ BufferedReader reader = new BufferedReader(new inputStreamReader(is,"iso-8859-1"),8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readline()) != null) { sb.append(line + "\n"); } is.close(); result=sb.toString(); }catch(Exception e){ Log.e("log_tag", "Error converting result "+e.toString()); } try{ jArray = new JsONObject(result); }catch(JsONException e){ Log.e("log_tag", "Error parsing data "+e.toString()); } return jArray; }}
我有两个xml文件,除了这个项目的主要文件之外
test.xml
<TextVIEw androID:ID="@+ID/items" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:textAppearance="?androID:attr/textAppearanceMedium" androID:padding="2dp" androID:textSize="20dp" /> <TextVIEw androID:ID="@+ID/item_subTitle" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:padding="2dp" androID:textSize="18dp" /></linearLayout>
列表位置持有人
<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:orIEntation="vertical" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent"> <ImageVIEw androID:ID="@+ID/image" androID:layout_wIDth="50dip" androID:layout_height="50dip" androID:scaleType="centerCrop"/> <ListVIEw androID:ID="@ID/androID:List" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:layout_weight="1" androID:drawSelectorOntop="false" /> <TextVIEw androID:ID="@ID/androID:empty" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:text="No data"/></linearLayout>
我知道有可用的惰性列表,但是我想为我的理解进行设置.谢谢
解决方法:
正确,我为您提供了另一个示例项目,您可以下载here.它的代码太多,无法发布,因此我将项目压缩后可以下载.如果您不确定如何工作,请询问!请享用.
顺便说一句,它看起来像这样:
总结以上是内存溢出为你收集整理的android-设置动态listactivity全部内容,希望文章能够帮你解决android-设置动态listactivity所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)