Android自定义适配器无法在Activity中显示任何数据?

时间:2016-05-29 00:06:21

标签: java android android-layout android-arrayadapter

我为我的列表(JobAdapter)创建了一个自定义适配器,但是,每当我设置适配器时,即使有值(jobs arraylist参数的大小为2),我的活动也不会显示任何内容。

这是我的适配器:

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);

            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");

        //TODO: add hardhat, boots and other field to objects mapped

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

这就是我在AsyncTask OnPostExecute中设置适配器的方法:

protected void onPostExecute(ArrayList<Job> result) {

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

            flingContainer.setAdapter(arrayAdapter);
        }

这可能是什么问题? Logcat中没有错误消息,应用程序仍然正常运行,只显示没有数据。数据应显示在为JobAdapter定义的jobcard.xml布局中。

提前感谢您对此的任何帮助!

编辑:

看起来JobAdapter中的getView()没有被调用,我试图覆盖JobAdapter中的getCount()方法返回1,这样我就可以看到一个元素但是也没有用。

<?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" />

0 个答案:

没有答案