我试图在Listview中显示图像列表,但我只有列表中的第一项

时间:2017-09-01 08:37:02

标签: android listview retrofit

我正在使用android和retrofit调用API Web服务成功接收数据但是当我尝试在我的布局中显示图像列表时,结果只是第一个图像,尽管图像数组的大小是上面的.. 我的活动是:CityActivity获取城市信息,如名称和描述以及城市图像列表。 在城市图像中,我遇到了我所问的问题 我的代码: CityActivity:

package com.example.android.travelandtourism.Activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.android.travelandtourism.Adapters.ImagesAdapter;
import com.example.android.travelandtourism.Interfaces.IApi;
import com.example.android.travelandtourism.Models.City;
import com.example.android.travelandtourism.Models.Countries;
import com.example.android.travelandtourism.Models.Images;
import com.example.android.travelandtourism.R;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by haya on 29/08/2017.
 */

public class CityActivity extends AppCompatActivity {
    private ListView listView;
    public static final String BASE_URL = "http://www.travel-tourism1.somee.com/api/";
    String url="http://www.travel-tourism1.somee.com";


    Gson gson = new GsonBuilder()
            .setDateFormat("66yyyy-MM-dd'T'HH:mm:ssZ")
            .create();

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create(gson))
            .build();

    IApi service = retrofit.create(IApi.class);

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

        Intent intent = this.getIntent();
        int cityId = intent.getIntExtra(Intent.EXTRA_TEXT,0);

        listView = (ListView) findViewById(R.id.listCityImages);
        Call<ResponseValue> call = service.getCity(cityId);
        call.enqueue(new Callback<ResponseValue>() {
            @Override
            public void onResponse(Call<ResponseValue> call, Response<ResponseValue> response) {
                listView.setVisibility(View.VISIBLE);
                ResponseValue cityResponse = response.body();
                City cc = cityResponse.getCity();
                TextView tv1 = (TextView) findViewById(R.id.tvCityInfoNameEn);
                tv1.setText(cc.getNameEn());
                TextView tv2 = (TextView) findViewById(R.id.tvCityInfoNameAr);
                tv2.setText(cc.getNameAr());
                TextView tv3 = (TextView) findViewById(R.id.tvCityInfoDesEng);
                tv3.setText(cc.getDescriptionEn());
                TextView tv4 = (TextView) findViewById(R.id.tvCityInfoDesAr);
                tv4.setText(cc.getDescriptionAr());

                ArrayList<Images> arrayList = new ArrayList<>();
                ArrayList<Images> images = cityResponse.getCity().getImages();
                arrayList.addAll(images);
              // final ImagesAdapter adapter = new ImagesAdapter(getApplicationContext(),R.layout.row_image,arrayList);
                    ImagesAdapter adapter = new ImagesAdapter(getApplicationContext(),arrayList);
                listView.setAdapter(adapter);


            }

            @Override
            public void onFailure(Call<ResponseValue> call, Throwable t) {

            }
        });


    }
}

ImagesAdapter:

package com.example.android.travelandtourism.Adapters;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.example.android.travelandtourism.Models.City;
import com.example.android.travelandtourism.Models.Images;
import com.example.android.travelandtourism.R;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by haya on 29/08/2017.
 */

public class ImagesAdapter extends BaseAdapter {

    String url = "http://www.travel-tourism1.somee.com";
    private Context context;
    private List<Images> imagesList;

    public ImagesAdapter(Context context,  List<Images> objects) {
      //  super(context, resource, objects);
        this.context = context;
        this.imagesList = objects;
    }


    @Override
    public int getCount() {

        return imagesList.size();
    }

    @Override
    public Object getItem(int position) {

        return imagesList.get(position);
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.row_image,parent,false);


        Images image = imagesList.get(position);
        ImageView iv = (ImageView)view.findViewById(R.id.ivImage);
        String imgPath = image.getPath().substring(1);
        Picasso.with(this.context).load(url+imgPath).resize(300,300).into(iv);


        return view;

    }


}

我的布局: activity_city_info:

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:fillViewport="true">


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="4dp">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">



    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tvCityInfoNameEn"
        android:text="EngName"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tvCityInfoNameAr"
        android:text="ArName"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">


    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tvCityInfoDesEng"
        android:text="EngDes"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tvCityInfoDesAr"
        android:text="ArDes"/>



    </LinearLayout>


</LinearLayout>
    <ListView
        android:id="@+id/listCityImages"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="gone"
        tools:listitem="@layout/row_image"
        />

</LinearLayout>


    </ScrollView>




<!--


-->

和finaly row_image:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="4dp">




        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingLeft="10dp">
            <ImageView
                android:id="@+id/ivImage"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@mipmap/ic_launcher" />




    </LinearLayout>
</LinearLayout>

任何帮助!谢谢你 更新 imageList

3 个答案:

答案 0 :(得分:1)

在ImageAdapter类中添加此方法:

 public class ImagesAdapter extends BaseAdapter {

String url = "http://www.travel-tourism1.somee.com";
private Context context;
private List<Images> imagesList;

public ImagesAdapter(Context context, int resource, List<Images> objects) {
    super(context, resource, objects);
    this.context = context;
    this.imagesList = objects;
}



@Override
public int getCount() {

    return imagesList.size();
}

@Override
public Object getItem(int position) {

    return null;
}

@Override
public long getItemId(int position) {

    return position;
}

 @Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.row_image,parent,false);


    Images image = imagesList.get(position);
    ImageView iv = (ImageView)view.findViewById(R.id.ivImage);
    String imgPath = image.getPath().substring(1);
    Picasso.with(getContext()).load(url+imgPath).resize(300,300).into(iv);


    return view;

 }
  }

答案 1 :(得分:1)

添加:

@Override
public int getCount() {

    return imagesList.size();
}

@Override
public Object getItem(int position) {

    return imageList.get(position);
}

@Override
public long getItemId(int position) {

    return position;
}

答案 2 :(得分:1)

您无法在scrollview中使用列表视图,因此添加listview的触摸列表器来处理列表视图的滚动比滚动列表时可以查看所有图像。因此,请在CityActivity.java

中添加此列表器
 listView.setOnTouchListener(new View.OnTouchListener() {
            // Setting on Touch Listener for handling the touch inside ScrollView
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // Disallow the touch request for parent scroll on touch of child view
                v.getParent().requestDisallowInterceptTouchEvent(true);
                return false;
            }
        });