How to set the first item in GridView with default value and populate the rest of the items based on JSON result

一个人想着一个人 提交于 2019-12-13 05:53:56

问题


Hi i would like to set the first item of my GridView with a default value, and populate the rest of the item accordingly (based on JSON result)

My code consist of 2 classes 1. MainActivity 2. GridView Adapter

Here is the code for my GridAdapter

public class ContactGridAdapter extends ArrayAdapter<Person> {
*** SOME INITIALIZATION ***

public ContactGridAdapter(Context context, ArrayList<Person> values){
    super(context, R.layout.grid_item_text);
    mContext = context;
    mList = values;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return mList.size();
}

@Override
public Person getItem(int position) {
    // TODO Auto-generated method stub
    return mList.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    LayoutInflater inflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View view;

    if(convertView == null){
        view = new View(mContext);
        view = inflater.inflate(R.layout.grid_item_text, null);

        TextView personName = (TextView) view.findViewById(R.id.textItem);
        ImageView personThumb = (ImageView) view.findViewById(R.id.iconItem);

        // IF HAVE RESULT
        if(mList.size() != 0){
            /* INITIALIZES PHOTO */
            BitmapFactory.Options bmOptions;
            bmOptions = new BitmapFactory.Options();
            bmOptions.inSampleSize = 1;

            /*
            Bitmap bmpThumb = loadBitmap(mList.get(position).accId, bmOptions, IMAGE_THUMB);

            if(bmpThumb != null){
                ImageHelper ih = new ImageHelper();
                bmpThumb = ih.getRoundedCornerBitmap(bmpThumb, 10);
                personThumb.setImageBitmap(bmpThumb);
            }else{
                personThumb.setImageResource(mThumbs);
            }
            */

            personThumb.setImageResource(mThumbs);
            personName.setText(mList.get(position).fullName.toString());
        }else{
            personThumb.setImageResource(mThumbs);
            personName.setText("Add Friends");
        }



    }else{
        view = (View) convertView;
    }
    return view;
}

Excerpt from my MainActivity

protected void onCreate(Bundle savedInstanceState) {
private GridView gridView;
gridView = (GridView) findViewById(R.id.thumb_grid_view);
getFriends();
}

private void getFriends(){
    RetrieveFriendList task = new RetrieveFriendList();
    task.execute();
}

private class RetrieveFriendList extends AsyncTask<String, Void, ArrayList<Person>> {

        @Override
        protected ArrayList<Person> doInBackground(String... params) {
blah...

            return mFriendList;
        }

        @Override
        protected void onPostExecute(final ArrayList<Person> result) {          
            if(result != null){
                gridView.setAdapter(new ContactGridAdapter(getApplicationContext(), result));
            }
        }
    }

This however does not work as i intended. Should i populate the default value from MainActivity or from within the Adapter?


回答1:


Try doing this:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

LayoutInflater inflater = (LayoutInflater) mContext
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View view;

if(position == 0)
// Populate default item
else
// Populate from json
if(convertView == null){
    view = new View(mContext);
    view = inflater.inflate(R.layout.grid_item_text, null);

    TextView personName = (TextView) view.findViewById(R.id.textItem);
    ImageView personThumb = (ImageView) view.findViewById(R.id.iconItem);

    // IF HAVE RESULT
    if(mList.size() != 0){
        /* INITIALIZES PHOTO */
        BitmapFactory.Options bmOptions;
        bmOptions = new BitmapFactory.Options();
        bmOptions.inSampleSize = 1;

        /*
        Bitmap bmpThumb = loadBitmap(mList.get(position).accId, bmOptions, IMAGE_THUMB);

        if(bmpThumb != null){
            ImageHelper ih = new ImageHelper();
            bmpThumb = ih.getRoundedCornerBitmap(bmpThumb, 10);
            personThumb.setImageBitmap(bmpThumb);
        }else{
            personThumb.setImageResource(mThumbs);
        }
        */

        personThumb.setImageResource(mThumbs);
        personName.setText(mList.get(position).fullName.toString());
    }else{
        personThumb.setImageResource(mThumbs);
        personName.setText("Add Friends");
    }



}else{
    view = (View) convertView;
}
return view;

}




回答2:


if you are using array or arraylist to bind data in gridview than add data at first position than after add data of JSON result. so you can have your data at first position of array or arraylist. and than bind it in to gridview.



来源:https://stackoverflow.com/questions/20651946/how-to-set-the-first-item-in-gridview-with-default-value-and-populate-the-rest-o

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!