如何在ParseQueryAdapter列表视图中添加2个文本视图

时间:2014-07-04 06:14:49

标签: android android-layout android-listview parse-platform

我希望创建一个包含2个文本视图的列表,其中包含从parse.com云获取的数据。 两个文本视图是: 1.普通文本(说A) 2.由两个格式化为字符串的日期对象组成的文本(说B)

我正在使用以下代码:

ADAPTER

public class TripListAdapter extends ParseQueryAdapter<Trips> {
    SimpleDateFormat formatter = new SimpleDateFormat("MMM dd, yyy");
    public TripListAdapter(Context context) {
        super(context, new ParseQueryAdapter.QueryFactory<Trips>() {
            public ParseQuery<Trips> create() {
                // Here we can configure a ParseQuery to display
                // only top-rated meals.
                ParseUser currentUser = ParseUser.getCurrentUser();
                ParseQuery query = new ParseQuery("Trips");
                query.whereEqualTo("user", currentUser);
                //query.whereContainedIn("rating", Arrays.asList("5", "4"));
                query.orderByDescending("createdAt");
                return query;
            }
        });
    }

    @Override
    public View getItemView(Trips trips, View v, ViewGroup parent) {

        if (v == null) {
            v = View.inflate(getContext(), R.layout.item_trip_list, null);
        }

        super.getItemView(trips, v, parent);

        //ParseImageView mealImage = (ParseImageView) v.findViewById(R.id.icon);
        //ParseFile photoFile = meal.getParseFile("photo");
        //if (photoFile != null) {
            //mealImage.setParseFile(photoFile);
            //mealImage.loadInBackground(new GetDataCallback() {
                //@Override
                //public void done(byte[] data, ParseException e) {
                    // nothing to do
            //  }
            //});
        //}

        TextView destinationTextView = (TextView) v.findViewById(R.id.trip_destination);
        destinationTextView.setText(trips.getDestination());
        TextView durationTextView = (TextView)v.findViewById(R.id.trip_duration);
        durationTextView.setText(formatter.format(trips.getStartDate())+" - "+formatter.format(trips.getEndDate()));
        return v;
    }

Trips.java

public class Trips extends ParseObject {

    public Trips() {
        // A default constructor is required.
    }

    public String getDestination() {
        return getString("to");
    }

    public void setDestination(String title) {
        put("to", title);
    }

    public String getStartDate() {
        return getString("start");
    }

    public void setStartDate(Date dateObj) {
        put("start", dateObj);
    }

    public String getEndDate() {
        return getString("end");
    }

    public void setEndDate(Date dateObj) {
        put("end", dateObj);
    }
}

带有列表视图代码的碎片

ListView mTripsList = (ListView)rootView.findViewById(R.id.trips_list);
        mainAdapter = new ParseQueryAdapter<Trips>(getActivity(), Trips.class);
        mainAdapter.setTextKey("to");
        mTripsList.setAdapter(mainAdapter);

我只能看到上面的第一个文本(A),而不是第二个带有格式化日期对象(B)的文本。我该如何解决 ? 谢谢

列出项目

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:background="@drawable/bg_card"
     >

    <TextView 
        android:id="@+id/trip_destination"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"        
        />

    <TextView 
        android:id="@+id/trip_duration"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="40dp"
        />


</LinearLayout>

1 个答案:

答案 0 :(得分:1)

尝试更换适配器代码

public class TripListAdapter extends ParseQueryAdapter<Trips> {
        SimpleDateFormat formatter = new SimpleDateFormat("MMM dd, yyyy");

        public TripListAdapter(Context context) {
            super(context, new ParseQueryAdapter.QueryFactory<Trips>() {
                public ParseQuery<Trips> create() {
                    // Here we can configure a ParseQuery to display
                    // only top-rated meals.
                    ParseUser currentUser = ParseUser.getCurrentUser();
                    ParseQuery query = new ParseQuery("Trips");
                    query.whereEqualTo("user", currentUser);
                    //query.whereContainedIn("rating", Arrays.asList("5", "4"));
                    query.orderByDescending("createdAt");
                    return query;
                }
            });
        }

        @Override
        public View getItemView(Trips trips, View v, ViewGroup parent) {
            super.getItemView(trips, v, parent);
            ViewHolder holder;
            if (v == null) {
                holder =new ViewHolder();
                v = View.inflate(getContext(), R.layout.item_trip_list, null);
                holder.destinationTextView = (TextView) v.findViewById(R.id.trip_destination);
                holder.durationTextView = (TextView) v.findViewById(R.id.trip_duration);
                v.setTag(holder);
            }else{
                holder = (ViewHolder) v.getTag();
            }


            //ParseImageView mealImage = (ParseImageView) v.findViewById(R.id.icon);
            //ParseFile photoFile = meal.getParseFile("photo");
            //if (photoFile != null) {
            //mealImage.setParseFile(photoFile);
            //mealImage.loadInBackground(new GetDataCallback() {
            //@Override
            //public void done(byte[] data, ParseException e) {
            // nothing to do
            //  }
            //});
            //}

            holder.destinationTextView.setText(trips.getDestination());
            holder.durationTextView.setText(formatter.format(trips.getStartDate()) + " - " + formatter.format(trips.getEndDate()));
            return v;
        }

        class ViewHolder{
            TextView destinationTextView;
            TextView durationTextView;

        }
    }