使用JSON和Coverflow库

时间:2016-05-21 14:26:42

标签: android json image coverflow

我在diplayin图片时遇到问题,没有什么是在显示中显示的,似乎是空的只有数据显示没有图片。我认为解析图片中的JSON数据时的问题。 在酒店的片段中,我想要显示图片库。

public class ViewHotels extends AppCompatActivity {
    private Bitmap bitmap;
    private TextView nom1;
    private TextView grade;
    private TextView tele;
    private ImageView image;
    private TextView sit;
    private TextView add1;
    private TextView email1;
    private FloatingActionButton fab;
    LinearLayout layout;
    private String id;
    public static final String URL="http://gabes.comlu.com/Base_Controle/getEmpdetail.php";
    private FeatureCoverFlow coverFlow;
    private CoverFlowAdapter adapter;
    private ArrayList<Game> games;
    String image2;
    String addresses;
    String subject;
    public static final String TAG_JSON_ARRAY="result";

    ImageView im;
    private int imageSource;


    public String stringUrl;
    public String stringUrl1;
    public String stringUrl2;
    public String stringUrl3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.hotelsdetails);
        final Intent intent = getIntent();
        layout=(LinearLayout) findViewById(R.id.layout);
       coverFlow = (FeatureCoverFlow) findViewById(R.id.coverflow);

        settingDummyData();
        adapter = new CoverFlowAdapter(this, games);
        coverFlow.setAdapter(adapter);
        coverFlow.setOnScrollPositionListener(onScrollListener());

        id = intent.getStringExtra("Type");
        nom1 = (TextView) findViewById(R.id. nom);
        grade = (TextView) findViewById(R.id. grade);
        tele = (TextView) findViewById(R.id. tele);
        image= (ImageView)findViewById(R.id.imageView);
        sit=(TextView) findViewById(R.id.site);
        add1=(TextView) findViewById(R.id.adde);
        email1=(TextView)findViewById(R.id.email);


        nom1.setText(id);
        im =new ImageView (this);
        getEmployee();
    }


    private void getEmployee(){
        final String login11 = nom1.getText().toString().trim();
        class GetEmployee extends AsyncTask<Void,Void,String>{
            ProgressDialog loading;
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                loading = ProgressDialog.show(ViewHotels.this,"Fetching...","Wait...",false,false);
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                loading.dismiss();
                showEmployee(s);
            }

            @Override
            protected String doInBackground(Void... v) {
                HashMap<String,String> params = new HashMap<>();
                params.put("id",login11)

                RequestHandler rh = new RequestHandler();

                String res= rh.sendPostRequest(URL, params);

                return res;
            }
        }
        GetEmployee ge = new GetEmployee();
        ge.execute();
    }
    private void showEmployee(String json){
        try {

            JSONArray result = new JSONArray(json);
            JSONObject c = result.getJSONObject(0);
            String name = c.getString("nom");
            String tel = c.getString("tele");
            String grade1 = c.getString("grade");
            String image1 = c.getString("image");
            image2 = c.getString("img1");
            String image3 = c.getString("img2");
            String image4 = c.getString("img3");
            String site= c.getString("site");
            String add11= c.getString("add");
            String email11= c.getString("email");

            tele.setText("Tel : \t"+tel);
            grade.setText("Grade  : \t"+grade1);
            sit.setText("site: \t"+site);
            add1.setText("adresse :\t" + add11);
            email1.setText("email :\t"+ email11);


            final ImageView im1 =new ImageView (this);
            final ImageView im2 =new ImageView (this);
            final ImageView im3 =new ImageView (this);

            stringUrl = ("http://gabes.comlu.com/Base_Controle/ImageBD/"+image1+".jpg");
            stringUrl1 = ("http://gabes.comlu.com/Base_Controle/ImageBD/"+image2+".jpg");
            stringUrl2 = ("http://gabes.comlu.com/Base_Controle/ImageBD/"+image3+".jpg");
            stringUrl3 = ("http://gabes.comlu.com/Base_Controle/ImageBD/"+image4+".jpg");

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

        }

    private FeatureCoverFlow.OnScrollPositionListener onScrollListener() {
        return new FeatureCoverFlow.OnScrollPositionListener() {
            @Override
            public void onScrolledToPosition(int position) {
                Log.v("ViewHotels", "position: " + position);
            }

            @Override
            public void onScrolling() {
                Log.i("ViewHotels", "scrolling");
            }
        };
    }
    private void settingDummyData() {
        games = new ArrayList<>();
        Game game1 = new Game("stringUrl","");
        Game game2 = new Game("stringUrl1","");
        Game game3 = new Game("stringUrl2","");

        games.add(game1);
        games.add(game2);
        games.add(game3);


    }
}

这是coverFlow适配器:

public class CoverFlowAdapter extends BaseAdapter {

private ArrayList<Game> data;
private AppCompatActivity activity;

public CoverFlowAdapter(AppCompatActivity context, ArrayList<Game> objects) {
    this.activity = context;
    this.data = objects;
}
@Override
public int getCount() {
    return data.size();
}

@Override
public Game getItem(int position) {
    return data.get(position);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder viewHolder;

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.item_flow_view, null, false);

        viewHolder = new ViewHolder(convertView);
        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }
    try {
        URL myFileUrl = new URL(data.get(position).getImageSource());
        Log.e("TAG stringUri",myFileUrl+"" );
        HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
        conn.setDoInput(true);
        conn.connect();
        InputStream is = conn.getInputStream();
        viewHolder.gameImage.setImageBitmap(BitmapFactory.decodeStream(is));
    }
    catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    //viewHolder.gameImage.setImageResource(data.get(position).getImageSource());
    viewHolder.gameName.setText(data.get(position).getName());
    return convertView;
}

private static class ViewHolder {
    private TextView gameName;
    private ImageView gameImage;

    public ViewHolder(View v) {
        gameImage = (ImageView) v.findViewById(R.id.image);
        gameName = (TextView) v.findViewById(R.id.name);
    }
}

} 这就是游戏类:

public class Game {

private String name;
private String imageSource;

public Game (String imageSource, String name) {
    this.name = name;
    this.imageSource = imageSource;
}

public String getName() {
    return name;
}

public String getImageSource() {
    return imageSource;
}

}

1 个答案:

答案 0 :(得分:0)

我认为(其中一个)你的问题就是这个部分:

Game game1 = new Game("stringUrl","");
Game game2 = new Game("stringUrl1","");
Game game3 = new Game("stringUrl2","");

你正在移交String&#34; stringUrlx&#34;到你的游戏构造函数。 在Java中,引号(&#34;&#34;)用于显式定义字符串,这意味着您正在移交&#34; stringUrl&#34;而不是变量 stringUrl ,它将具有正确的内容(&#34; http://gabes.comlu.com/Base_Controle/ImageBD/&#34; + image1 +&#34; .jpg&#34;)。

所以请替换。

Game game1 = new Game("stringUrl","");

通过

Game game1 = new Game(stringUrl,"");

这应解决这个问题。

除此之外,您还会遇到下载文件和内存管理问题。我建议你研究一下java特定的教程,它们使用预构建的异步图像加载和缓存库(比如Glide或Picasso!)