Reputation: 111
I am using retrofit 2.0 and This code is part of my fragment and adapter. I don't get values from this method because of asyn. Values are date, soup, maindinner, thirdkind, fourtkind, fifthkind. I want to add them in mylistview. How can I get this values. I want to show them on listview.
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_dining, container, false);
rowItemDinings = new ArrayList<RowItemDining>();
DiningInterface diningInterface = RetroClient.getClient().create(DiningInterface.class);
Call<Dining[]> call = diningInterface.getJsonValues();
call.enqueue(new Callback<Dining[]>()
{
@Override
public void onResponse(Call<Dining[]> call, Response<Dining[]> response)
{
dinings = Arrays.asList(response.body());
cacheVersion = dinings.get(0).cacheVersion;
for(int i=0; i<dinings.size(); i++)
{
date.add(dinings.get(i).date);
soup.add(dinings.get(i).soup);
mainDinner.add(dinings.get(i).mainDinner);
thirdKind.add(dinings.get(i).thirdKind);
fourthKind.add(dinings.get(i).fourthKind);
fifthKind.add(dinings.get(i).fifthKind);
RowItemDining item = new RowItemDining(date.get(i), soup.get(i), mainDinner.get(i), thirdKind.get(i), fourthKind.get(i), fifthKind.get(i));
rowItemDinings.add(item);
}
//Toast.makeText(getActivity(), "This is my date! " + soup.get(0) + "" + soup.get(4)+ "" + soup.get(5) + soup.get(6)+ "" + soup.get(7), Toast.LENGTH_LONG).show();
}
@Override
public void onFailure(Call<Dining[]> call, Throwable t)
{
Log.e(TAG, "Error: " + t.toString());
}
});
mylistview = (ListView) getView().findViewById(R.id.dining_list);
CustomAdapterDining adapter = new CustomAdapterDining(getActivity(), rowItemDinings);
mylistview.setAdapter(adapter);
return view;
}
CustomAdapterDining.java
public class CustomAdapterDining extends BaseAdapter
{
Context context;
List<RowItemDining> rowItemDinings;
List<RowItemDining> data;
public ListView mylistview;
public CustomAdapterDining( List<RowItemDining> rowItemDinings)
{
this.rowItemDinings = rowItemDinings;
}
public CustomAdapterDining(Context context, List<RowItemDining> rowItemDinings)
{
this.context = context;
this.rowItemDinings = rowItemDinings;
}
@Override
public int getCount()
{
return rowItemDinings.size();
}
@Override
public Object getItem(int position)
{
return rowItemDinings.get(position);
}
@Override
public long getItemId(int position)
{
return rowItemDinings.indexOf(getItem(position));
}
/* private view holder class */
private class ViewHolder
{
TextView date;
TextView soup;
TextView mainDinner;
TextView thirdKind;
TextView fourthKind;
TextView fifthKind;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_row_dining, null);
holder = new ViewHolder();
holder.date = (TextView) convertView.findViewById(R.id.date);
holder.soup = (TextView) convertView.findViewById(R.id.soup);
holder.mainDinner = (TextView) convertView.findViewById(R.id.main_dinner);
holder.thirdKind = (TextView) convertView.findViewById(R.id.third_kind);
holder.fourthKind = (TextView) convertView.findViewById(R.id.fourth_kind);
holder.fifthKind = (TextView) convertView.findViewById(R.id.fifth_kind);
RowItemDining row_pos = rowItemDinings.get(position);
holder.date.setText(row_pos.getDate());
holder.soup.setText(row_pos.getSoup());
holder.mainDinner.setText(row_pos.getMainDinner());
holder.thirdKind.setText(row_pos.getThirdKind());
holder.fourthKind.setText(row_pos.getFourthKind());
holder.fifthKind.setText(row_pos.getFifthKind());
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
return convertView;
Upvotes: 1
Views: 697
Reputation: 92
because the request turn in background and the response will be after the Adapter initialisation so you need to add in your adapter a setter for data example :
in Adapter
void setData(List<RowItemDining> data){
this.data=data;
notifyDataSetChanged();
}
in fragment :
mylistview = (ListView) view.findViewById(R.id.dining_list);
CustomAdapterDining adapter = new CustomAdapterDining(getActivity(), rowItemDinings);
mylistview.setAdapter(adapter);
call.enqueue(new Callback<Dining[]>()
{
@Override
public void onResponse(Call<Dining[]> call, Response<Dining[]> response)
{
dinings = Arrays.asList(response.body());
cacheVersion = dinings.get(0).cacheVersion;
for(int i=0; i<dinings.size(); i++)
{
[...]
}
adapter.setData(rowItemDinings);
}
UPDATE : Add adapter changes
@Override
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_row_dining, null);
holder = new ViewHolder();
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.date = (TextView) convertView.findViewById(R.id.date);
holder.soup = (TextView) convertView.findViewById(R.id.soup);
holder.mainDinner = (TextView) convertView.findViewById(R.id.main_dinner);
holder.thirdKind = (TextView) convertView.findViewById(R.id.third_kind);
holder.fourthKind = (TextView) convertView.findViewById(R.id.fourth_kind);
holder.fifthKind = (TextView) convertView.findViewById(R.id.fifth_kind);
RowItemDining row_pos = rowItemDinings.get(position);
holder.date.setText(row_pos.getDate());
holder.soup.setText(row_pos.getSoup());
holder.mainDinner.setText(row_pos.getMainDinner());
holder.thirdKind.setText(row_pos.getThirdKind());
holder.fourthKind.setText(row_pos.getFourthKind());
holder.fifthKind.setText(row_pos.getFifthKind());
return convertView;
}
Upvotes: 1