2016-01-03 3 views
0

Я пытаюсь получить изображение с MySQL и отобразить его на listView, но не удался. Я что-то пропустил?Нет изображения на listView

Это то, что я пробовал.

Таблица staff_benefit

enter image description here

public void BuildEditStaffList(final String id) 
    { 
     class GetDataJSON extends AsyncTask<String, Void, String> { 

      @Override 
      protected String doInBackground(String... params) { 
       DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams()); 
       HttpPost httppost = new HttpPost("http://192.168.1.7/Android/CRUD/staffRetrieve.php?id="+id); 


       // Depends on your web service 
       httppost.setHeader("Content-type", "application/json"); 

       InputStream inputStream = null; 
       String result = null; 
       try { 
        HttpResponse response = httpclient.execute(httppost); 
        HttpEntity entity = response.getEntity(); 

        inputStream = entity.getContent(); 
        // json is UTF-8 by default 
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8); 
        StringBuilder sb = new StringBuilder(); 

        String line = null; 
        while ((line = reader.readLine()) != null) 
        { 
         sb.append(line + "\n"); 
        } 
        result = sb.toString(); 
       } catch (Exception e) { 
        // Oops 
       } 
       finally { 
        try{if(inputStream != null)inputStream.close();}catch(Exception squish){} 
       } 
       return result; 
      } 

      @Override 
      protected void onPostExecute(String result){ 
       myJSON=result; 
       showList(); 
      } 
     } 
     GetDataJSON g = new GetDataJSON(); 
     g.execute(); 
    } 

    protected void showList(){ 
    try { 
     JSONObject jsonObj = new JSONObject(myJSON); 
     details= jsonObj.getJSONArray(Config.TAG_RESULTS); 

     for(int i=0;i<details.length();i++){ 
      JSONObject c = details.getJSONObject(i); 
      String type = c.getString(Config.TAG_TYPE); 
      String description = c.getString(Config.TAG_DESCRIPTION); 
      String amount=c.getString(Config.TAG_AMOUNT); 
      String image=c.getString(Config.TAG_IMAGE); 
      byte[] data = Base64.decode(image, 0); 
      Bitmap b =BitmapFactory.decodeByteArray(data, 0, data.length); 
      Drawable d=new BitmapDrawable(getResources(),b); 
      int ID=c.getInt(Config.TAG_ID); 
      //v.setImageBitmap(b); 
      HashMap<String,Object> info = new HashMap<String,Object>(); 
      info.put(Config.TAG_TYPE, type); 
      info.put(Config.TAG_DESCRIPTION, description); 
      info.put(Config.TAG_AMOUNT,amount); 
      info.put(Config.TAG_IMAGE,d); 
      info.put(Config.TAG_ID,ID+""); 
      StaffDetails.add(info); 
     } 

     ListAdapter adapter = new SimpleAdapter(
       getActivity(),StaffDetails, R.layout.retrieve_staff, 
       new String[]{Config.TAG_IMAGE,Config.TAG_TYPE,Config.TAG_AMOUNT,Config.TAG_DESCRIPTION}, 
       new int[]{R.id.image,R.id.type,R.id.amount,R.id.description} 
     ); 

     listViewEdit.setAdapter(adapter); 

    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 

} 

staffRetrieve.php

<?php 
    define('HOST','127.0.0.1:3307'); 
    define('USER','root'); 
    define('PASS',''); 
    define('DB','androiddb'); 

    $con = mysqli_connect(HOST,USER,PASS,DB) or die('unable to connect'); 

    $tws = $_GET['id']; 

$sql = "select * from staff_benefit WHERE ts_id= '". $tws."' "; 

    $res = mysqli_query($con,$sql); 

    $result=array(); 

    while($row=mysqli_fetch_array($res)){ 
     array_push($result,array('id'=>$row[0],'type'=>$row[1],'amount'=>$row[2],'description'=>$row[3],'image'=>$row[4], 
     'ts_id'=>$row[5])); 
    } 

echo (json_encode(array("result"=>$result))); 

mysqli_close($con); 

?> 

Любая помощь будет принята с благодарностью.

Текст получить отображается, но не для изображения

ListView

enter image description here

Изображение должно быть показано слева.

ответ

1

Отредактировано

Создать объект Штата в первую очередь.

public class Staff { 
    private int id; 
    private String image; 
    private String type; 
    private String amount; 
    private String description; 

    public Staff(int id, String type, String description, String amount, String image) { 
     this.id = id; 
     this.image = image; 
     this.type = type; 
     this.amount = amount; 
     this.description = description; 
    } 

    public String getType() { 
     return type; 
    } 

    public void setType(String type) { 
     this.type = type; 
    } 

    public String getAmount() { 
     return amount; 
    } 

    public void setAmount(String amount) { 
     this.amount = amount; 
    } 

    public String getDescription() { 
     return description; 
    } 

    public void setDescription(String description) { 
     this.description = description; 
    } 

    public int getId() { 
     return id; 
    } 

    public void setId(int id) { 
     this.id = id; 
    } 

    public String getImage() { 
     return image; 
    } 

    public void setImage(String image) { 
     this.image = image; 
    } 
} 

Вот Adapter.class

public class Adapter extends ArrayAdapter<Staff> { 
    Activity context; 
    List<Staff> staffs; 
    static class ViewHolder { 
     public ImageView image; 
     public TextView type; 
     public TextView amount; 
     public TextView description; 
    } 
    @Override 
    public int getCount() { 
     return staffs.size(); 
    } 
    public Adapter(Activity context, List<Staff> staffs) { 
     super(context, R.layout.retrieve_staff, staffs); 
     this.context = context; 
     this.staffs = staffs; 
    } 

    @Override 
    public Staff getItem(int position) { 
     return null; 
    } 

    @Override 
    public long getItemId(int position) { 
     return 0; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder; 
     if(convertView==null){ 
      ViewHolder v = new ViewHolder(); 
      LayoutInflater inflater = context.getLayoutInflater(); 
      convertView = inflater.inflate(R.layout.retrieve_staff, null); 
      v.image = (ImageView)convertView.findViewById(R.id.imageview); 
      v.amount = (TextView)convertView.findViewById(R.id.amount); 
      v.type = (TextView)convertView.findViewById(R.id.type); 
      v.description = (TextView)convertView.findViewById(R.id.description); 
      convertView.setTag(v); 
     } 
     holder = (ViewHolder)convertView.getTag(); 
     Log.v("TEST",staffs.get(position).getImage()); 
     byte[] data = Base64.decode(staffs.get(position).getImage(), 0); 
     Bitmap b = BitmapFactory.decodeByteArray(data, 0, data.length); 
     Drawable d=new BitmapDrawable(context.getResources(),b); 
     holder.image.setImageDrawable(d); 
     holder.amount.setText(staffs.get(position).getAmount()); 
     holder.type.setText(staffs.get(position).getType()); 
     holder.description.setText(staffs.get(position).getDescription()); 
     return convertView; 
    } 
} 

и это showList() метод

protected void showList(){ 
     try { 
      JSONObject jsonObj = new JSONObject(myJSON); 
      details= jsonObj.getJSONArray(Config.TAG_RESULTS); 

      for(int i=0;i<details.length();i++){ 
       JSONObject c = details.getJSONObject(i); 
       String type = c.getString(Config.TAG_TYPE); 
       String description = c.getString(Config.TAG_DESCRIPTION); 
       String amount=c.getString(Config.TAG_AMOUNT); 
       String image=c.getString(Config.TAG_IMAGE); 
       Staff staff = new Staff(ID,type,description,amount,image); 
       staffs.add(staff); 
      } 

      Adapter adapter = new Adapter(getActivity(),staffs); 
      listViewEdit.setAdapter(adapter); 

     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

    } 
+0

спасибо за вашу response..Am я делать в правильном пути? Изображение не отображается, и я получаю это в журнале 'E/BitmapFactory: не удается декодировать поток: java.io.FileNotFoundException: /[email protected]: open failed: ENOENT (Нет такого файла или каталога)' –

+0

Если я удаляю 'Drawable d ', я получаю это' E/BitmapFactory: Невозможно декодировать поток: java.io.FileNotFoundException: /: open failed: EISDIR (есть каталог) ' –

+0

Я проверил коды. Все прошло хорошо. Я использовал 'imageView.setImageDrawable (d)' для установки образа в ImageView. На какой линии произошло исключение? –