2016-11-09 4 views
-2

Ich folge diesem Tutorial, aber ich bekomme ein Problem (https://www.simplifiedcoding.net/android-recyclerview-and-cardview-tutorial/). Ich habe eine Tabelle namens Bilder mit Spalten ID und Bildern.Bild nicht in recyclerview vom Server

Das ist mein getData.php

<?php 

    $sql = "SELECT * FROM images"; 

    require_once('dbConnect.php'); 

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

    $result = array(); 

    while($row = mysqli_fetch_array($r)){ 
     array_push($result,array(
     'name'=>$row['name'], 
     'url'=>$row['image'] 
     )); 
    } 

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

    mysqli_close($con); 

Das ist mein config.java ist

package com.example.mdesigntemp; 
import android.graphics.Bitmap; 
public class ConfigChildItem { 
public static String[] names; 
public static String[] urls; 
public static Bitmap[] bitmaps; 

public static final String GET_URL = "http://www.kinandayu.com/image_content/getData.php"; 
public static final String TAG_IMAGE_URL = "url"; 
public static final String TAG_IMAGE_NAME = "name"; 
public static final String TAG_JSON_ARRAY="result"; 

public ConfigChildItem(int i){ 
    names = new String[i]; 
    urls = new String[i]; 
    bitmaps = new Bitmap[i]; 
    } 
} 

das ist mein recyclerviewAdapter:

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> { 

List<ListItem> items; 

public RecyclerAdapter(String[] names, String[] urls, Bitmap[] images){ 
    super(); 
    items = new ArrayList<ListItem>(); 
    for(int i =0; i<names.length; i++){ 
     ListItem item = new ListItem(); 
     item.setName(names[i]); 
     item.setUrl(urls[i]); 
     item.setImage(images[i]); 
     items.add(item); 
    } 
} 

@Override 
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    View layoutView = LayoutInflater.from(parent.getContext()). 
      inflate(R.layout.item_list, parent, false); 
    ViewHolder viewHolder = new ViewHolder(layoutView); 
    return viewHolder; 
} 

@Override 
public void onBindViewHolder(ViewHolder holder, int position) { 
    ListItem list = items.get(position); 
    holder.imageView.setImageBitmap(list.getImage()); 
    holder.textViewName.setText(list.getName()); 
    holder.textViewUrl.setText(list.getUrl()); 
} 

@Override 
public int getItemCount() { 
    return items.size(); 
} 

class ViewHolder extends RecyclerView.ViewHolder { 
    public ImageView imageView; 
    public TextView textViewName; 
    public TextView textViewUrl; 

    public ViewHolder(View itemView) { 
     super(itemView); 

     imageView = (ImageView) itemView.findViewById(R.id.items); 
     textViewName = (TextView) itemView.findViewById(R.id.textViewTitle); 
     textViewUrl = (TextView) itemView.findViewById(R.id.SimpleDescCardSub1); 

    } 
} 
} 

das ist mein childtab1 Aktivität:

public class Child_Tab1 extends Activity{ 

private RecyclerView rvView; 
private RecyclerView.Adapter adapter; 
private RecyclerView.LayoutManager layoutManager; 
private ArrayList<String> dataSet; 
private ConfigChildItem configchilditem; 

@Override 
protected void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.child_tab_1); 

    ActionBarTitleGravity(); 

    rvView = (RecyclerView) findViewById(R.id.rv1); 

    rvView.setAdapter(adapter); 
    rvView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)); 
    SpacesItemsDecoration decoration = new SpacesItemsDecoration(4); 
    rvView.addItemDecoration(decoration); 
    getData(); 
} 

    private void ActionBarTitleGravity() { 
     ActionBar actionbar = getActionBar(); 
     TextView textview = new TextView(getApplicationContext()); 
     LayoutParams layoutparams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 
     textview.setLayoutParams(layoutparams); 
     textview.setText("Kinandayu"); 
     textview.setTextColor(Color.parseColor("#00acc1")); 
     textview.setGravity(Gravity.START); 
     textview.setTextSize(20); 
     actionbar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
     actionbar.setCustomView(textview); 

} 
    private void getData(){ 
     class GetData extends AsyncTask<Void,Void,String>{ 
      ProgressDialog progressDialog; 
      @Override 
      protected void onPreExecute() { 
       super.onPreExecute(); 
       progressDialog = ProgressDialog.show(Child_Tab1.this, "Fetching Data", "Please wait...",false,false); 
      } 

      @Override 
      protected void onPostExecute(String s) { 
       super.onPostExecute(s); 
       progressDialog.dismiss(); 
       parseJSON(s); 
      } 

      @Override 
      protected String doInBackground(Void... params) { 
       BufferedReader bufferedReader = null; 
       try { 
        URL url = new URL(ConfigChildItem.GET_URL); 
        HttpURLConnection con = (HttpURLConnection) url.openConnection(); 
        StringBuilder sb = new StringBuilder(); 

        bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream())); 

        String json; 
        while((json = bufferedReader.readLine())!= null){ 
         sb.append(json+"\n"); 
        } 

        return sb.toString().trim(); 

       }catch(Exception e){ 
        return null; 
       } 
      } 
     } 
     GetData gd = new GetData(); 
     gd.execute(); 
    } 

    public void showData() { 
     adapter = new RecyclerAdapter(ConfigChildItem.names,ConfigChildItem.urls, ConfigChildItem.bitmaps); 
     rvView.setAdapter(adapter); 
    } 

    private void parseJSON(String json){ 
     try { 
      JSONObject jsonObject = new JSONObject(json); 
      JSONArray array = jsonObject.getJSONArray(ConfigChildItem.TAG_JSON_ARRAY); 

      configchilditem = new ConfigChildItem(array.length()); 

      for(int i=0; i<array.length(); i++){ 
       JSONObject j = array.getJSONObject(i); 
       ConfigChildItem.names[i] = getName(j); 
       ConfigChildItem.urls[i] = getURL(j); 
      } 

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

     ChildItemListJSON gb = new ChildItemListJSON(this,this, ConfigChildItem.urls); 
     gb.execute(); 
    } 

    private String getName(JSONObject j){ 
     String name = null; 
     try { 
      name = j.getString(ConfigChildItem.TAG_IMAGE_NAME); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
     return name; 
    } 

    private String getURL(JSONObject j){ 
     String url = null; 
     try { 
      url = j.getString(ConfigChildItem.TAG_IMAGE_URL); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
     return url; 
    } 
} 

das ist mein GetBitmap:

public class ChildItemListJSON extends AsyncTask<Void,Void,Void> { 

private Context context; 
private String[] urls; 
private ProgressDialog loading; 
private Child_Tab1 child_Tab1; 

public ChildItemListJSON(Context context, Child_Tab1 child_Tab1, String[] urls){ 
    this.context = context; 
    this.urls = urls; 
    this.child_Tab1 = child_Tab1; 
} 

@Override 
protected void onPreExecute() { 
    super.onPreExecute(); 
    loading = ProgressDialog.show(context,"Downloading Image","Please wait...",false,false); 
} 

@Override 
protected void onPostExecute(Void aVoid) { 
    super.onPostExecute(aVoid); 
    loading.dismiss(); 
    child_Tab1.showData(); 
} 

@Override 
protected Void doInBackground(Void... params) { 
    for(int i=0; i<urls.length; i++){ 
     ConfigChildItem.bitmaps[i] = getImage(urls[i]); 
    } 
    return null; 
} 

private Bitmap getImage(String bitmapUrl){ 
    URL url; 
    Bitmap image = null; 
    try { 
     url = new URL(bitmapUrl); 
     image = BitmapFactory.decodeStream(url.openConnection().getInputStream()); 
    }catch(Exception e){} 
    return image; 
} 
} 

und das ist mein listitem.java:

public class ListItem { 
private String name; 
private String url; 
private Bitmap image; 

public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

public String getUrl() { 
    return url; 
} 

public void setUrl(String url) { 
    this.url = url; 
} 

public Bitmap getImage() { 
    return image; 
} 

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

ich eine Tabelle mit Spalten-ID und Bild (varchar300) haben

my table

Mein großes Problem ist:

  1. Warum ist das Bild in meiner Datenbank nicht in meinem recylerview geladen
  2. mache ich falsch mit meinem PHP-Skript? mein Bild war store in Ordner Uploads in meinem Dateimanager in cPanel

Jede Antwort für mich sehr hilfreich ist. Vielen Dank im Voraus

+1

Ihre URL Bild wird nicht –

+0

@VivekMishra vervollständigen, wo ich die URL erhalten in jedem Bild meiner Datenbank? Ich neu für diese –

+0

Sie nur Bildpfad von Home-Ordner speichern. Sie sollten auch Ihre Basis-URL an den Image-Pfad anhängen –

Antwort

0

Verwendung Glider Liberary

Bild vom Server in den OnBindViewHolder zeigen
@Override 
public void onBindViewHolder(ViewHolder holder, int position) { 
    ListItem list = items.get(position); 

    Glide.with(this) 
     .load(list.getImage()) 
     .into(imageView); 
    holder.textViewName.setText(list.getName()); 
    holder.textViewUrl.setText(list.getUrl()); 
} 

Dieses Compile in gradle

compile 'com.github.bumptech.glide:glide:3.7.0' 
+0

Hat dies für Sie funktioniert ..? –

+0

Ich benutze Eclipse, ich habe keine Picasso-Bibliothek, also benutze ich diesen Code. Ich habe nur Gleit-und Volley-Bibliothek :( –

+0

warten, ich ändere meine und für Segelflugzeug-Support-Bibliothek –

Verwandte Themen