Diolor / Swipecards

A Tinder-like Android library to create the swipe cards effect. You can swipe left or right to like or dislike the content.
Apache License 2.0
2.34k stars 583 forks source link

Custom Adapter Not Displaying Anything? #149

Closed thofnar closed 8 years ago

thofnar commented 8 years ago

Hi,

I had a custom adapter working fine for a while, but now it seems to have stopped. I don't get any errors, the cards just don't show up in the app at all.

This is my custom ArrayAdapter:

public class JobAdapter extends ArrayAdapter<Job> {
private final Context context;
private final ArrayList<Job> jobs;
private final int layoutResourceId;

public JobAdapter(Context context, int layoutResourceId, ArrayList<Job> jobs) {
    super(context, layoutResourceId, jobs);
    this.context = context;
    this.jobs = jobs;
    this.layoutResourceId = layoutResourceId;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    ViewHolder holder = null;

    LayoutInflater inflater = (LayoutInflater) context.getSystemService((Activity.LAYOUT_INFLATER_SERVICE));

    if (view == null) {
        view = inflater.inflate(layoutResourceId, parent, false);

        holder = new ViewHolder();
        holder.title = (TextView)view.findViewById(R.id.tv_jobTitle);
        holder.payrate = (TextView)view.findViewById(R.id.tv_payRate);
        holder.startdate = (TextView)view.findViewById(R.id.tv_startDate);
        holder.workinghrs = (TextView)view.findViewById(R.id.tv_duration);
        holder.location = (TextView)view.findViewById(R.id.tv_location);
        holder.companyname = (TextView)view.findViewById(R.id.tv_companyName);
        holder.description = (TextView)view.findViewById(R.id.tv_JobDesc);
        holder.experience = (TextView)view.findViewById(R.id.tv_experienceReq);
        holder.equipment = (TextView)view.findViewById(R.id.tv_equipmentReq);

        //TODO: make equipment a linear layout with three graphics hardhat, boots or + for other

        view.setTag(holder);
    } else {
        holder = (ViewHolder)view.getTag();
    }

    Job j = jobs.get(0);

    holder.title.setText(j.getJobTitle());
    holder.payrate.setText(j.getPayrate());
    holder.startdate.setText(j.getDurationStart());
    holder.workinghrs.setText(j.getWorkingHrs());
    holder.location.setText(j.getLocation());
    holder.companyname.setText("ABC Company");
    holder.description.setText(j.getDescription());
    holder.experience.setText("3-5 years");
    holder.equipment.setText("Hardhat");

    return view;
}

static class ViewHolder
{
    TextView title;
    TextView payrate;
    TextView startdate;
    TextView workinghrs;
    TextView location;
    TextView companyname;
    TextView description;
    TextView experience;
    TextView equipment;
}
}

And this is how I am setting the custom adapter from an AsyncTask in my activity:

        arrayAdapter = new JobAdapter(getApplicationContext(), R.layout.jobcard, result);
        arrayAdapter.notifyDataSetChanged();
        flingContainer.setAdapter(arrayAdapter);

The jobcard layout xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_gravity="top"
android:layout_width="match_parent"
android:layout_height="415dp"
android:padding="5dp"
android:layout_marginTop="20dp">

<LinearLayout
    android:id="@+id/outerMainCardLayout"
    android:layout_width="match_parent"
    android:layout_height="400dp"
    android:layout_gravity="center_horizontal|top"
    android:orientation="vertical"
    android:background="@drawable/swipecard_shadow"
    android:gravity="top"
    android:layout_marginLeft="5dp">

    <LinearLayout
        android:id="@+id/mainCardLayout"
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:layout_gravity="center_horizontal|top"
        android:orientation="vertical"
        android:weightSum="1"
        android:background="#FFFFFF"
        android:gravity="top">

        <TextView
            android:id="@+id/tv_jobTitle"
            android:background="#ffffff"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:textStyle="bold"
            android:textSize="16sp"
            android:textColor="#505353"
            android:textAlignment="center"
            tools:text="Cement Pouring Guy"
            android:layout_gravity="center_horizontal"/>

        <TextView
            android:id="@+id/tv_companyName"
            android:background="#ffffff"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="14sp"
            android:textColor="#505353"
            android:textAlignment="center"
            tools:text="ABC Company"
            android:layout_gravity="center_horizontal" />

        <LinearLayout
            android:id="@+id/cardHeader"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center" >

            <TextView
                android:id="@+id/tv_experienceReq"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="15dp"
                android:layout_marginLeft="10dp"
                android:textSize="14sp"
                android:textColor="#505353"
                android:drawableTop="@drawable/ic_experience"
                tools:text="3-5 years" />

            <View
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_weight="1" />

            <TextView
                android:id="@+id/tv_location"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="15dp"
                android:textSize="14sp"
                android:textColor="#505353"
                android:drawableTop="@drawable/ic_location"
                tools:text="Langley, BC" />

            <View
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_weight="1" />

            <TextView
                android:id="@+id/tv_payRate"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="15dp"
                android:layout_marginRight="10dp"
                android:textSize="14sp"
                android:textColor="#505353"
                android:drawableTop="@drawable/ic_payrate"
                tools:text="125/day" />

        </LinearLayout>

        <TextView
            android:id="@+id/tv_JobDesc"
            android:textSize="14sp"
            android:textColor="#505353"
            android:background="#e6e7e8"
            tools:text="Pour Cement, Mix Cement, Level Cement and go pick up cement bags."
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:padding="15dp" />

        <LinearLayout
            android:id="@+id/additionalInfo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center" >

            <TextView
                android:id="@+id/tv_startDate"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="15dp"
                android:textSize="14sp"
                android:textColor="#505353"
                android:layout_marginLeft="10dp"
                android:drawableTop="@drawable/ic_date"
                tools:text="June 1, 2016" />

            <View
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_weight="1"
                />

            <TextView
                android:id="@+id/tv_duration"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="15dp"
                android:textSize="14sp"
                android:textColor="#505353"
                android:drawableTop="@drawable/ic_clock"
                tools:text="  8h" />

            <View
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_weight="1"
                />

            <TextView
                android:id="@+id/tv_equipmentReq"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="15dp"
                android:textSize="14sp"
                android:textColor="#505353"
                android:layout_marginRight="10dp"
                android:drawableTop="@drawable/ic_hardhat"
                tools:text="Hardhat" />

        </LinearLayout>

    </LinearLayout>
</LinearLayout>

<View
    android:id="@+id/item_swipe_left_indicator"
    android:alpha="0"
    android:layout_width="20dp"
    android:layout_height="20dp"
    android:layout_margin="20dp"
    android:background="#FFFFFF" />

<View
    android:id="@+id/item_swipe_right_indicator"
    android:alpha="0"
    android:layout_width="20dp"
    android:layout_height="20dp"
    android:layout_margin="20dp"
    android:layout_gravity="right"
    android:background="#FFFFFF" />
</FrameLayout>

Any ideas what the issue could be?

Thanks in advance!

thofnar commented 8 years ago

@alexwalterbos I'm back at it with this custom adapter but it seems I've really broken it this time. The cards are no longer displayed, any ideas what it could be? I've spent the better part of today on this and I am completely out of ideas :(

This time it looks like getView, inside the JobAdapter is not being called at all even thought there are 2 records in the jobs arraylist that is passed to it.

leonardoarnaud commented 8 years ago

Collect cards data before bind layout.

alexwalterbos commented 8 years ago

You're not using position but you're getting Job 0 because you use Job j = jobs.get(0). Should probably be jobs.get(position).

Hope this helps! Also:

`LayoutInflater inflater = (LayoutInflater) context.getSystemService((Activity.LAYOUT_INFLATER_SERVICE));`

Protip: Instead of calling the SystemService, you can get a LayoutInflater more cleanly with LayoutInflater.from(Context context)