以下是我的应用程序中的代码,我尝试从Foursquare API获取场地列表并在recyclerview中显示
searchRetrofitAcitivity.java
allow_signal()
VenuesAdapter.java
public class searchRetrofitActivity extends AppCompatActivity {
private static final String TAG = searchRetrofitActivity.class.getSimpleName();
// TODO - insert your foursquare OAuth KEY here
private final static String OAUTH_TOKEN = "L2GTUPPPWMEZ2HUT5D4HHYOOY5YNNNE1UVMJCZAOOFI2NNG3";
private static final String LIMIT_VENUE_RESULT = "5";
String latitudeLongitude = "40.7,-74";
ArrayList<Venue> venues = new ArrayList<Venue>();
String currentDate = searchRetrofitActivity.getCurrentDate();
private RecyclerView recyclerView;
public static String getCurrentDate() {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat mdformat = new SimpleDateFormat("yyyyMMdd");
String formattedDate = mdformat.format(calendar.getTime());
return formattedDate;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_retrofit);
final VenuesAdapter vAdapter = new VenuesAdapter(venues, R.layout.list_venue, getApplicationContext());
//method call to get data from api
recyclerView = (RecyclerView) findViewById(R.id.venues_recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(vAdapter);
ApiInterface apiService =
FourSquareClient.getClient().create(ApiInterface.class);
Call<VenueResponses> call = apiService.getExploredVenues(latitudeLongitude, OAUTH_TOKEN, currentDate);
call.enqueue(new Callback<VenueResponses>() {
@Override
public void onResponse(Call<VenueResponses> call, Response<VenueResponses> response) {
int statusCode = response.code();
List<Item_> items;
Venue tempVenue;
items = response.body().getResponse().getGroups().get(0).getItems();
for (int j = 0; j < items.size(); j++) {
tempVenue = new Venue();
tempVenue = items.get(j).getVenue();
venues.add(tempVenue);
}
recyclerView.setAdapter(new VenuesAdapter(venues, R.layout.list_venue, getApplicationContext()));
Log.d(TAG, "No Of venues added " + venues.size());
}
@Override
public void onFailure(Call<VenueResponses> call, Throwable t) {
// Log error here since request failed
Log.e(TAG, t.toString());
}
});
}
}
activity_search_retrofit.xml
public class VenuesAdapter extends RecyclerView.Adapter<VenuesAdapter.VenueViewHolder> {
private ArrayList<Venue> venues;
private int rowLayout;
private Context context;
public VenuesAdapter(ArrayList<Venue> venues, int rowLayout, Context context) {
this.venues = venues;
this.rowLayout = rowLayout;
this.context = context;
}
public static class VenueViewHolder extends RecyclerView.ViewHolder {
LinearLayout venuesLayout;
TextView venueName;
TextView venueRating;
TextView venueCategory;
TextView venuePrice;
TextView venueDistance;
TextView venueAddress;
public VenueViewHolder(View view) {
super(view);
venuesLayout = (LinearLayout) view.findViewById(R.id.venue_item);
venueName = (TextView) view.findViewById(R.id.tv_venue_name);
venueRating = (TextView) view.findViewById(R.id.tv_rating);
venueCategory = (TextView) view.findViewById(R.id.tv_category);
venuePrice = (TextView) view.findViewById(R.id.tv_price);
venueDistance = (TextView) view.findViewById(R.id.tv_distance);
venueAddress = (TextView) view.findViewById(R.id.tv_address);
}
}
@Override
public VenueViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(rowLayout,parent,false);
return new VenueViewHolder(view);
}
@Override
public void onBindViewHolder(VenueViewHolder holder, int position) {
holder.venueName.setText(venues.get(position).getName());
// holder.venueRating.setText(venues.get(position).get);
}
@Override
public int getItemCount() {
return venues.size();
}
}
list_venue.xml
<android.support.v7.widget.RecyclerView
android:id="@+id/venues_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
应用程序因以下错误而崩溃
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="6dp">
<ImageView
android:id="@+id/image_view"
android:layout_width="113dp"
android:layout_height="113dp" />
<TextView
android:id="@+id/venue_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="@id/image_view"
android:text="venue name"
android:textColor="#1D1D26"
android:textSize="16.88sp" />
<TextView
android:id="@+id/tv_rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/venue_name"
android:layout_marginLeft="10dp"
android:layout_marginTop="30dp"
android:layout_toRightOf="@id/image_view"
android:padding="2dp"
android:text="10.0"
android:textSize="11.25dp"
tools:background="#58FA58" />
<TextView
android:id="@+id/tv_category"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_rating"
android:layout_marginLeft="10dp"
android:layout_marginTop="6dp"
android:layout_toRightOf="@id/image_view"
android:text="category"
android:textColor="#000000"
android:textSize="12.38sp" />
<TextView
android:id="@+id/tv_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_rating"
android:layout_marginLeft="10dp"
android:layout_marginTop="6dp"
android:layout_toRightOf="@id/tv_category"
android:text="price"
android:textColor="#000000"
android:textSize="12.38sp" />
<TextView
android:id="@+id/tv_distance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_rating"
android:layout_marginLeft="10dp"
android:layout_marginTop="6dp"
android:layout_toRightOf="@id/tv_price"
android:text="distance"
android:textColor="#000000"
android:textSize="12.38sp" />
<TextView
android:id="@+id/tv_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_category"
android:layout_marginLeft="10dp"
android:layout_marginTop="6dp"
android:layout_toRightOf="@id/image_view"
android:textColor="#000000"
android:textSize="12.38sp"
tools:text="Enter the adress" />
<Button
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
tools:background="@drawable/close_icon_grey_hdpi" />
</RelativeLayout>
答案 0 :(得分:1)
你应该改变
venueName = (TextView) view.findViewById(R.id.tv_venue_name);
到
venueName = (TextView) view.findViewById(R.id.venue_name);
您的 TextView ID是 venue_name 而非 tv_venue_name
答案 1 :(得分:0)
Venu View项目没有LinearLayout你有RelativeLayout更改:
venuesLayout = (LinearLayout) view.findViewById(R.id.venue_item);
要
RelativeLayout venuesLayout;
venuesLayout = (RelativeLayout) view.findViewById(R.id.venue_item);
在View Holder类中
在xml:
中为您的项目父根布局添加ID <RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/venue_item"
android:layout_marginTop="6dp">