【问题标题】:get image from mySQL to listview Android从 mySQL 获取图像到 listview Android
【发布时间】:2012-09-03 00:40:58
【问题描述】:

我想从我的数据库 mySQL 中获取图像以显示在列表视图中(图像 + 文本列表视图)

我的代码:

       public void cek(){

           String url_select = "http://10.0.2.2/BloodGlucose/selectDoctor.php";

           HttpClient httpClient = new DefaultHttpClient();
           HttpPost httpPost = new HttpPost(url_select);

           //parameter
           ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();

           try {
              //add parameter
               httpPost.setEntity(new UrlEncodedFormEntity(param));

             HttpResponse httpRespose = httpClient.execute(httpPost);
             HttpEntity httpEntity = httpRespose.getEntity();

             //read content
             InputStream in = httpEntity.getContent();
             BufferedReader read = new BufferedReader(new InputStreamReader(in));

             String content = "";
             String line = "";

             while((line = read.readLine())!=null){
                content += line;
             }

             Log.d("ADBUG", "content: "+content);


             //json
             if(!content.equals("null")){




                try {
                   JSONArray jArr = new JSONArray(content);
                   for(int i=0;i<jArr.length();i++){
                      JSONObject jObj = jArr.getJSONObject(i);
                      String id = jObj.getString("_id");
                      String name = jObj.getString("name");
                      String dateofbirth = jObj.getString("dateofbirth");
                      String phone = jObj.getString("telp");
                      String address = jObj.getString("clinicaddress");
                      String file = jObj.getString("file");
                      String uname = jObj.getString("username_doctor");
                      String lulusan = jObj.getString("lulusan");
                      String clinicname = jObj.getString("clinicname");

                      names.add(name);
                      date.add(dateofbirth);
                      telp.add(phone);      
                      clinic.add(address);
                      usernamedoctor.add(uname);
                      namaklinik.add(clinicname);
                      graduate.add(lulusan);



                   }



setListAdapter(new DoctorArrayAdapter(this, names)); 

                } catch (JSONException e) {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
                }

             }else{
                Toast.makeText(this, "Error", Toast.LENGTH_LONG).show();
             }

          } catch (ClientProtocolException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
          } catch (IOException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
          }
       }

DoctorArrayAdapter 代码:

package research.android.bloodglucose;


import java.util.ArrayList;

import research.android.bloodglucose.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class DoctorArrayAdapter extends ArrayAdapter<String> {
    private final Context context;
    private final ArrayList<String> values;

    public DoctorArrayAdapter(Context context, ArrayList<String> names) {
        super(context, R.layout.list_row, names);
        this.context = context;
        this.values = names;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = inflater.inflate(R.layout.list_row, parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.DoctorName);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.list_image);
        textView.setText(values.get(position));

        // Change icon based on name
        String s = values.get(position);

        System.out.println(s);

        /*if (s.equals("WindowsMobile")) {
            imageView.setImageResource(R.drawable.windowsmobile_logo);
        } else if (s.equals("iOS")) {
            imageView.setImageResource(R.drawable.ios_logo);
        } else if (s.equals("Blackberry")) {
            imageView.setImageResource(R.drawable.blackberry_logo);
        } else {
            imageView.setImageResource(R.drawable.android_logo);
        }*/

        return rowView;
    }
}

我的代码有什么问题吗?因为图片没有显示,只有文字,非常感谢

【问题讨论】:

  • 看来您是从URL 获取图像,而不是从Database 获取图像
  • 哇,要分析的代码太多了 - 下次尝试提取重要部分...:)
  • 我很抱歉,因为我找不到问题,这里是更新的重要部分(我认为)
  • @SpK 因为我托管了我的数据库,所以我想根据我之前上传的图像通过 URL 获取图像:D

标签: android mysql json listview


【解决方案1】:

方便,您应该创建一个从 ArrayAdapter 扩展的类,然后实现方法 getView 来告诉您的列表对每个项目执行什么操作(添加图像和文本)。

查看此链接Android listview example

(自定义 ArrayAdapter 示例)

(还有一个提示,您的错误部分来自构建列表视图,而不是来自从 MySQL 获取数据,因此请从您的问题中删除这部分,这是误导)

这是一个获取具有其 url 路径的图像的函数

public static Bitmap getURLImage(String path)
{

    try
    {
        URL aURL = new URL(path);
        final URLConnection conn = aURL.openConnection();
        conn.connect();
        final BufferedInputStream bis = new BufferedInputStream(
                conn.getInputStream());
        final Bitmap bm = BitmapFactory.decodeStream(bis);
        bis.close();
        return bm;
    }
    catch (Throwable e)
    {   
        Log.w("ERROR", "Error " + e.getMessage());
        return null;
    }
}

然后你做(你应该从线程调用它,因为从android 4.0开始你不允许从主线程建立网络连接,这是应该在getView中添加的部分):

    new Thread() {
                public void run() {
                    try {
                        //mBitmap is an attirbute
                        mBitmap = getRemoteImage(link);
                        handler.sendEmptyMessage(COMPLETE);

                    } finally {

                    }
                };
            };



/**
in your handler u set the image to the bitmap that you fetched
*/
    private final Handler handler = new Handler(new Callback() {

            public boolean handleMessage(Message msg) {
                   imageView.setImageBitmap(mBitmap);
            }
    }

【讨论】:

  • 非常感谢您的回复,您有来自 arrrayadapter 的扩展类的示例吗?不好意思问了这么多问题,我是安卓新手
  • 是的,它在链接中,mkyong.com/android/android-listview-example。转到它并搜索“自定义 ArrayAdapter 示例”。这正是您所需要的
  • 谢谢,但你能告诉我如何从 URL 获取图像吗?因为在示例中,图像在drawable中,我已经更新了问题,非常感谢
  • 我不太明白。网址是“10.0.2.2/BloodGlucose/img/"+uname+".jpg”;并且“uname”来自我使用JSON代码从我的数据库中获取的用户名,你如何将你的代码与用于从数据库中获取uname的JSON代码结合起来,谢谢,抱歉问了太多问题。而且,我把你的代码放在 DoctorArrayAddapter.java 中对吗?
  • 现在doctorArrayAdapter扩展ArrayAdapter&lt;String&gt;,你可以让它扩展ArrayAdapter&lt;UrCustomObject&gt;UrCustomObject包含两个属性,名字和url字符串。 values 应该是 ArrayList&lt;UrCustomObject&gt;。是的,获取图像的整个代码可以在DoctorArrayAdapter
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-03
  • 2016-12-05
  • 2013-03-05
  • 2015-12-08
相关资源
最近更新 更多