建立动态listactivity [英] setting up dynamic listactivity

查看:91
本文介绍了建立动态listactivity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请与该公司没有创造相应的列表活动,包括动态影像,并从服务器的描述如下code指导什么是错 输出。这里是图像

Please guide whats wrong with the following code that its not creating proper list activity including dynamic image and their description from server output. Here is the image

,因为它应该是这样的。

as it should be like this

由于只能将一个图像加载,没有描述和其余的没有图像,即使没有空间可以看到,请指导什么错的xml和code。 这里是code

As it can be seen only one image is loaded with no description and for the rest there are no image even there is no space , please guide whats wrong with the xml and code . Here is the code

主类

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类我觉得有需要发布,但只为理解为他人如下

Here is the jsonfunction class i think there is need to post but just for the understanding for others to follows

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          

I have two xml file other then main for this project which are 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>

listplace持有

listplace holder

<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>

我知道有lazylist可用,但我想设置此为我的理解。谢谢

I know there is lazylist available but i want to set this up for my understanding . Thanks

推荐答案

对的,我已经得到了另外一个例子项目,你,你可以下载中心<一href="https://docs.google.com/leaf?id=0BwAnjRVwT4WzMWZiMGJjYzUtMWNmYS00OWEyLWE2OTEtYmU2YTU1ZjdhYWU3&hl=en_US"相对=nofollow>这里其过多code后,所以我已经压缩了这个项目,所以你可以下载它。如果你不知道如何什么工作只是问!享受。

Right i've got another example project for you which you can dowload here. Its too much code to post so i've zipped up the project so you can download it. If your not sure how anything works just ask! Enjoy.

顺便说一句,它看起来是这样的:

BTW it looks like this:

这篇关于建立动态listactivity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆