Android GridView重复项Parse SDK

时间:2016-06-08 07:39:14

标签: android gridview parse-platform cursor

我正在调用一个查询来使用Parse SDK在我的Android应用程序中获取一些对象,一切正常,除了GridView在向下滚动以获取第二个之后的对象之后复制第一个项目而不是甚至从第二项开始获得正确的图像,如下所示:

enter image description here

enter image description here

这是我的代码:

public class Home extends AppCompatActivity {


/* Variables */
List<ParseObject> eventsArray = null;
Cursor cursor;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home);
    super.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    // Set Title on the ActionBar
    getSupportActionBar().setTitle("Events");

    // Call query
    queryEvents();
}



// MARK: - QUERY EVENTS
public  void queryEvents() {
    ParseQuery<ParseObject> query = ParseQuery.getQuery(Configs.EVENTS_CLASS_NAME);
    query.whereEqualTo(Configs.EVENTS_IS_PENDING, false);
    query.orderByDescending(Configs.EVENTS_END_DATE);
    query.findInBackground(new FindCallback<ParseObject>() {
        public void done(List<ParseObject> objects, ParseException error) {
            if (error == null) {
                eventsArray = objects;


                // CUSTOM GRID ADAPTER
                class GridAdapter extends BaseAdapter {
                    private Cursor cursor;
                    private Context context;

                    public GridAdapter(Context context, List<ParseObject> objects, Cursor cursor) {
                        super();
                        this.context = context;
                        if (cursor != null)  {
                            cursor.moveToFirst();
                            this.cursor = cursor;
                        }
                    }


                    // CONFIGURE CELL
                    @Override
                    public View getView(int position, View convertView, ViewGroup parent) {

                        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                        View gridView;

                        if (convertView == null) {
                            gridView = new View(context);
                            // Inflate event_cell.xml
                            gridView = inflater.inflate(R.layout.event_cell, null);

                            // Get Parse object
                            ParseObject eventObj = eventsArray.get(position);



                            // Get Title
                            TextView titleTxt = (TextView) gridView.findViewById(R.id.titleTxt);
                            titleTxt.setText(eventObj.getString(Configs.EVENTS_TITLE).toString().toUpperCase());

                            // Get Address
                            TextView addTxt = (TextView) gridView.findViewById(R.id.addressTxt);
                            addTxt.setText(eventObj.getString(Configs.EVENTS_LOCATION).toString());

                            // Get Start & End dates
                            TextView dateTxt = (TextView) gridView.findViewById(R.id.dateTxt);
                            SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd yyyy | hh:mm a");
                            dateTxt.setText(dateFormat.format(startDate).toUpperCase() + " - " + dateFormat.format(endDate).toUpperCase());


                            // Get Image
                            ParseFile fileObject = (ParseFile)eventObj.get(Configs.EVENTS_IMAGE);
                            fileObject.getDataInBackground(new GetDataCallback() {
                                public void done(byte[] data, ParseException error) {
                                    if (error == null) {
                                        Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
                                        if (bmp != null) {
                                            ImageView eventImage = (ImageView) findViewById(R.id.eventImage);
                                            eventImage.setImageBitmap(bmp);
                                        }
                                    }}});



                        } else { gridView = convertView; }

                        return gridView;
                    }

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

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

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

                }



                // Init GridView and set its adapter
                GridView eventsGrid = (GridView) findViewById(R.id.eventsGridView);
                eventsGrid.setAdapter(new GridAdapter(Home.this, eventsArray, cursor));

                eventsGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                        ParseObject eventObj = eventsArray.get(position);
                        Toast.makeText(getApplicationContext(), eventObj.getString(Configs.EVENTS_TITLE), Toast.LENGTH_SHORT).show();
                    // GO TO EventDetails activity
                    }
                });


                // Error in query
            } else {
                Toast.makeText(getApplicationContext(), error.getMessage().toString(), Toast.LENGTH_LONG).show();
            }

        }
    });

}

我是否对光标做错了,或者我不需要它? 我应该尝试解决这个问题?

谢谢!

1 个答案:

答案 0 :(得分:1)

您的getView错了。

如果是新视图convertView == null,您只能格式化视图(带标题等...),而不是仅在convertView == null时对其进行充气并始终进行设置。 此外,不需要gridView。 试试这个:

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

                    if (convertView == null) {
                        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                        // Inflate event_cell.xml
                        convertView= inflater.inflate(R.layout.event_cell, null);
                    }
                    // Get Parse object
                    ParseObject eventObj = eventsArray.get(position);



                    // Get Title
                    TextView titleTxt = (TextView) convertView.findViewById(R.id.titleTxt);
                    titleTxt.setText(eventObj.getString(Configs.EVENTS_TITLE).toString().toUpperCase());

                    // Get Address
                    TextView addTxt = (TextView) convertView.findViewById(R.id.addressTxt);
                    addTxt.setText(eventObj.getString(Configs.EVENTS_LOCATION).toString());

                    // Get Start & End dates
                    TextView dateTxt = (TextView) convertView.findViewById(R.id.dateTxt);
                    SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd yyyy | hh:mm a");
                    dateTxt.setText(dateFormat.format(startDate).toUpperCase() + " - " + dateFormat.format(endDate).toUpperCase());

                     final ImageView eventImage = (ImageView) convertView.findViewById(R.id.eventImage);
                    // Get Image
                    ParseFile fileObject = (ParseFile)eventObj.get(Configs.EVENTS_IMAGE);
                    fileObject.getDataInBackground(new GetDataCallback() {
                        public void done(byte[] data, ParseException error) {
                            if (error == null) {
                                Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
                                if (bmp != null) {
                                    eventImage.setImageBitmap(bmp);
                                }
                            }}});
                    return convertView;
                }

顺便说一句,你应该将getView划分为方法,例如setViewTitlesetViewImage ......

很难像这样阅读,对于你或其他可能正在使用此代码的人来说,维护它是很难的。