2017-09-01 1 views
-1

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 imageList

+0

Eigentlich, welches Problem Sie konfrontiert sind? –

+0

Ich kann nur das erste Bild aus der Liste der Bilder in der Listenansicht in meinem Layout ... (ein Bild von 3, zum Beispiel) @ UpendraShah –

Antwort

1

Sie können keine Listenansicht in Scrollview verwenden, so Listview Berührung listner hinzufügen handle scrolling von listview als wenn Sie blättern Liste Sie können alle Bilder anzeigen. Also fügen Sie diesen Listener in 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; 
      } 
     }); 
+0

Vielen Dank .. seine Arbeit jetzt überprüfen !! :) –

1

diese Methoden in Ihrem ImageAdapter Klasse hinzufügen:

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; 

} 
    } 
+0

Vielen Dank für Ihre Wiedergabe ... aber ich habe diesen Fehler 'getItem (int) 'in' (Mein Paketname) .CustomDrawerAdapter kollidiert mit 'getItem (int)' in 'android.widget.ArrayAdapter'; versuchen, inkompatiblen Rückgabetyp zu verwenden "weil meine Klasse ArrayAdapter erweitert, wie ich es behebe? –

+0

ändern Sie es in BaseAdapter: => öffentliche Klasse ImagesAdapter erweitert BaseAdapter { –

+0

@HayaSVU ändern Sie es in BaseAdapter: => öffentliche Klasse ImagesAdapter erweitert BaseAdapter { –

1

hinzufügen:

@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; 
} 
+0

Vielen Dank für Ihre Wiederholung. .. aber ich habe diesen Fehler 'getItem (int)' in '(Mein Paketname) .CustomDrawerAdapter' kollidiert mit 'getItem (int)' in 'android.widget.ArrayAdapter'; versuche, inkompatiblen Rückgabetyp zu verwenden, "weil meine Klasse erweitert ArrayAdapter , wie ich es beheben? –

+0

Ändern Sie es in BaseAdapter –

+0

Sorry, aber das Problem ist nicht gelöst ..Ich weiß nicht, was ich verpasst habe. Kannst du bitte meinen Code in GitHub überprüfen? https://github.com/haya51033/TravelAndTourism-master1 –

Verwandte Themen