如何在json ListView中显示来自url的图像?

时间:2014-06-17 08:44:58

标签: android json listview android-imageview

显示了ImageView,但它没有显示所有列表项,如何更正?

*这是图像视图代码*

try {
            URL thumb_u = new URL("http://yathu.net46.net/uploads/19.jpg");
            Drawable thumb_d = Drawable.createFromStream(thumb_u.openStream(), "src");
            image.setImageDrawable(thumb_d);
        }
        catch (Exception e) {
            // handle it
        }

以下完整代码

public class Solutions extends Activity {
ArrayList<Person> arrayofWebData=new ArrayList<Person>();
class Person{
    public String id;
    public String Diseases_type, Treatments_type, desise_name, img_url;
}
FancyAdapter aa=null;
static ArrayList<String> resultRow;

public void onCreate(Bundle savedInstanceState){
    try{
        super.onCreate(savedInstanceState);
        setContentView(R.layout.solutions);
        final String data = getIntent().getExtras().getString("Diseases_type");
        String post_id = getIntent().getExtras().getString("id");

        final String gen = getIntent().getExtras().getString("gen_id");


        Toast.makeText(getApplicationContext(),
                "Solution.java value is : "+post_id, Toast.LENGTH_LONG).show();

        String result="";

        try{
            HttpClient httpclient=new DefaultHttpClient();
            HttpPost httppost=new HttpPost("http://yathu.net46.net/application/database/view_treadment.php");

            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("desise_id",post_id));
            params.add(new BasicNameValuePair("gender_id",gen));

            httppost.setEntity(new UrlEncodedFormEntity(params)); 
            HttpResponse response=httpclient.execute(httppost);
            HttpEntity entity=response.getEntity();
            InputStream webs= entity.getContent();

            try{
                BufferedReader reader= new BufferedReader(new InputStreamReader(webs,"iso-8859-1"),8);
                StringBuilder sb= new StringBuilder();
                String line=null;
                while((line=reader.readLine())!=null){
                    sb.append(line+"\n");
                }
                webs.close();
                result=sb.toString();
            }catch(Exception e){
                Log.e("log_tag", "Error converting result" + e.toString());
            }

        }catch(Exception e){
            Log.e("log_tag","Error in http connection"+e.toString());
        }

        try{
            JSONArray jArray=new JSONArray(result);
            for(int i=0;i<jArray.length();i++)
            {
                JSONObject json_data=jArray.getJSONObject(i);
                Person resultRow=new Person();
                resultRow.id=json_data.getString("image");
                resultRow.Treatments_type=json_data.getString("Treatments_type");
                resultRow.desise_name=data+" - jPh;T "+(i+1);

                resultRow.img_url="http://yathu.net46.net/uploads/"+json_data.getString("image");
                arrayofWebData.add(resultRow);

            }
        }
        catch(JSONException e){
            Log.e("log_tag","Error parsing data"+e.toString());
        }
        final ListView myListView =(ListView)findViewById(R.id.solutionListView);
        aa=new FancyAdapter();
        myListView.setAdapter(aa);

        myListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3)
            {
                Person p = (Person) myListView.getItemAtPosition(position);
                Log.i("SomeTag", "diseases_id: " + p.Treatments_type);
                Log.i("SomeTag", "Tid: " + p.id);

                Intent i = new Intent(Solutions.this, Single_diseases.class);
                i.putExtra("diseases_id", p.Treatments_type);
                i.putExtra("Tid", p.id);
                i.putExtra("gend_id", gen);
                i.putExtra("des_name", data);


                Solutions.this.startActivity(i);
            }
        });

    }
    catch(Exception e){
        Log.e("ERROR","ERROR IN CODE"+e.toString());
        e.printStackTrace();
    }

}

class FancyAdapter extends ArrayAdapter<Person> {
    FancyAdapter(){
        super(Solutions.this,android.R.layout.simple_list_item_1,arrayofWebData);
    }
    public View getView(int position,View convertView, ViewGroup parent){
        ViewHolder holder;
        if(convertView==null){
            LayoutInflater inflater=getLayoutInflater();
            convertView=inflater.inflate(R.layout.sol_list, null);
            holder=new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        else{
            holder=(ViewHolder)convertView.getTag();
        }
        holder.populateFrom(arrayofWebData.get(position));
        return(convertView);
    }
}

class ViewHolder{
    public TextView desise_name=null;
    public ImageView image = null;
    public TextView showresult=null;
    public TextView image_urldisplay=null;

    int loader;
    ImageLoader imgLoader;

    public Activity activity = null;

    ViewHolder(View row){
        this.activity = activity;
        Typeface font1 = Typeface.createFromAsset(getAssets(), "fonts/Bamini.ttf");
        desise_name=(TextView)row.findViewById(R.id.solutions_types);
        desise_name.setTypeface(font1);

        showresult=(TextView)row.findViewById(R.id.showresult);
        image_urldisplay=(TextView)row.findViewById(R.id.textView1);
        image = (ImageView) findViewById(R.id.image);


    }
    void populateFrom(Person r){
        desise_name.setText(r.desise_name);
        showresult.setText(r.Treatments_type);
        image_urldisplay.setText(r.img_url);

        try {
            URL thumb_u = new URL("http://yathu.net46.net/uploads/19.jpg");
            Drawable thumb_d = Drawable.createFromStream(thumb_u.openStream(), "src");
            image.setImageDrawable(thumb_d);
        }
        catch (Exception e) {
            // handle it
        }
    }


}
}

请帮助任何人

1 个答案:

答案 0 :(得分:0)

我可以推荐一种不同于魅力的方式:Android查询。

您可以从here

下载该JAR文件
AQuery androidAQuery = new AQuery(this);

举个例子:

androidAQuery.id(YOUR IMAGEVIEW).image(YOUR IMAGE TO LOAD, true, true, getDeviceWidth(), ANY DEFAULT IMAGE YOU WANT TO SHOW);

它非常快速和准确,使用它你可以找到更多的功能,如加载时的动画,获取位图(如果需要)等。