Ich arbeite mit Android und Retrofit API Web Services aufrufen die Daten wurden erfolgreich empfangen, aber als ich versuchte, die Liste der Bilder in anzuzeigen mein Layout das Ergebnis ist nur die ersten Bilder, obwohl die Größe der Bilder Array oberen .. Meine Aktivitäten sind: CityActivity get city info wie Name und Beschreibung und Liste der Stadt Bilder .. und in Stadt Bilder, ich habe das Problem, was ich fragte nach mein Code: CityActivity:Ich habe versucht, Liste der Bilder in Listview anzuzeigen, aber ich habe nur das erste Element aus der Liste
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;
}
}
Meine Layouts: 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>
<!--
-->
und 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>
jede Hilfe! und danke Aktualisieren
Eigentlich, welches Problem Sie konfrontiert sind? –
Ich kann nur das erste Bild aus der Liste der Bilder in der Listenansicht in meinem Layout ... (ein Bild von 3, zum Beispiel) @ UpendraShah –