Search code examples
javaandroidbaseadapter

Populate ImageView from BaseAdapter (Swipecards)


I am attempting to use the Swipecards library (https://github.com/Diolor/Swipecards) to build a tinder-esqe application. I am using a BaseAdapter to populate a layout with two text views and an image view that will be provided to the main SwipeFlingAdapterView. While both of the text fields are populated, I cannot get the image to appear on the cards. I have tried this implementation with both an ArrayAdapter and a BaseAdapter and the results are the same.

The activity layout (deal_page_layout)

<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent">
<com.lorentzos.flingswipe.SwipeFlingAdapterView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/swipe_fling_view"
    app:rotation_degrees="10"
    tools:context=".DealPage"
    android:alpha="1.0"
    app:max_visible="2"
    app:min_adapter_stack="5"/>
</FrameLayout>

The layout being populated by the BaseAdapter (deal_card)

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:id="@+id/deal_card_image">
</ImageView>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/deal_card_title"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_margin="15dp"
    android:gravity="center"
    android:textSize="20dp"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/deal_card_description"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_margin="15dp"
    android:gravity="center"
    android:textSize="20dp"/>
</RelativeLayout>

BaseAdapter class

public class DealBaseAdapter extends BaseAdapter {

private Context context;
private List<GrubbyDeal> dealList;
private LayoutInflater li;

public DealBaseAdapter(Context context, LayoutInflater li, ArrayList<GrubbyDeal> dealList){
    this.context = context;
    this.dealList = dealList;
    this.li = li;
}

@Override
public int getCount(){
    return dealList.size();
}

@Override
public Object getItem(int position){
    return dealList.get(position);
}

@Override
public long getItemId(int position){
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent){
    ViewHolder viewHolder;

    //resuse a view if possible
    if(convertView == null){
        convertView = li.inflate(R.layout.deal_card,parent,false);
        viewHolder = new ViewHolder();
        viewHolder.img = (ImageView) convertView.findViewById(R.id.deal_card_image);
        viewHolder.title = (TextView) convertView.findViewById(R.id.deal_card_title);
        viewHolder.desc = (TextView) convertView.findViewById(R.id.deal_card_description);

        convertView.setTag(viewHolder);
    }
    else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

    GrubbyDeal curDeal = dealList.get(position);

    viewHolder.img.setImageURI(curDeal.getImageUri());
    viewHolder.title.setText(curDeal.getTitle());
    viewHolder.desc.setText(curDeal.getDescription());

    return convertView;
}

//view holder class to hold cached findViewByID results
private static class ViewHolder {
    public ImageView img;
    public TextView title;
    public TextView desc;
}

And the main activity (DealPage)

public class DealPage extends Activity {
private ArrayList<GrubbyDeal> dealList;

private DealBaseAdapter dealAdapter;

SwipeFlingAdapterView flingContainer;

@Override
public void onCreate(Bundle sis){
    super.onCreate(sis);
    setContentView(R.layout.deal_page_layout);
    //add some awesome cat deals to the adapter
    dealList = new ArrayList<>();
    for(int i=0; i < 5; i++){
        GrubbyDeal tmp = new GrubbyDeal(i);
        dealList.add(tmp);
    }
    //add another type of cat deal to the list
    dealList.add(new GrubbyDeal());

   dealAdapter = new DealBaseAdapter(this, getLayoutInflater(), dealList);

    flingContainer = (SwipeFlingAdapterView) findViewById(R.id.swipe_fling_view);
    flingContainer.setAdapter(dealAdapter);
    flingContainer.setFlingListener(new SwipeFlingAdapterView.onFlingListener() {
        @Override
        public void removeFirstObjectInAdapter() {
            // this is the simplest way to delete an object from the Adapter (/AdapterView)
            Log.d("LIST", "removed object!");
            GrubbyDeal popped = dealList.remove(0);
            dealList.add(popped);
            dealAdapter.notifyDataSetChanged();
        }

        @Override
        public void onLeftCardExit(Object dataObject) {
            makeToast(DealPage.this, "Left!");
        }

        @Override
        public void onRightCardExit(Object dataObject) {
            makeToast(DealPage.this, "Right!");
        }

        @Override
        public void onAdapterAboutToEmpty(int itemsInAdapter) {
            dealList.add(new GrubbyDeal());
            dealAdapter.notifyDataSetChanged();
            Log.d("LIST", "notified");
        }

        @Override
        public void onScroll(float scrollProgressPercent) {
            View view = flingContainer.getSelectedView();
        }
    });

    flingContainer.setOnItemClickListener(new SwipeFlingAdapterView.OnItemClickListener() {
        @Override
        public void onItemClicked(int itemPosition, Object dataObject) {
            makeToast(DealPage.this, "Clicked!");
        }
    });
}

}

Am I missing something obvious? Is there some vastly superior library that I should be using? Thanks,

Ian


Solution

  • The problem was formatting. I was attempting to use

    Uri.parse("android.resource://com.thepackage.theapp/R.drawable.cat4.jpg");
    

    but wasn't getting a valid Uri back. So instead I am using resource ids with picasso and the card works great!