package ca.ualberta.cs.cmput301f14t14.questionapp.view;

import java.util.List;

import ca.ualberta.cs.cmput301f14t14.questionapp.R;
import ca.ualberta.cs.cmput301f14t14.questionapp.data.ClientData;
import ca.ualberta.cs.cmput301f14t14.questionapp.data.GenericSearchItem;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class GenericSearchItemAdapter extends ArrayAdapter<GenericSearchItem> implements IView {

	public GenericSearchItemAdapter(Context context, int resource, List<GenericSearchItem> objects) {
		super(context, resource, objects);
	}
	
	public View getView(int position, View convertView, ViewGroup parent){
		if(convertView == null){
			convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_generic, parent, false);
		}
		GenericSearchItem item = getItem(position);
		ClientData cd = new ClientData(getContext());

		if(!cd.getFavoriteQuestions().contains(item.getId())){
			((ImageView)convertView.findViewById(R.id.question_favourite)).setVisibility(View.INVISIBLE);
		}
		else{
			((ImageView)convertView.findViewById(R.id.question_favourite)).setVisibility(View.VISIBLE);
		}
		TextView gType = (TextView) convertView.findViewById(R.id.generic_type); 
		TextView gText = (TextView) convertView.findViewById(R.id.generic_body);
		TextView gLocation = (TextView) convertView.findViewById(R.id.generic_location);
		TextView gDate = (TextView) convertView.findViewById(R.id.generic_date);
		gType.setText(item.getType());
		gText.setText(item.getBody());
		if (item.getLocation() != null)
			gLocation.setText(item.getLocation().toString());
		gDate.setText(item.getDate().toString());
		return parent;
		
	}

	@Override
	public void update() {
		notifyDataSetChanged();
	}

}

@@ -1807,22 +1807,27 @@
 %09return 
-parent
+convertView
 ;%0A%09%09%0A%09%7D%0A

Fix search view crash.