0

I have seen many post regarding this but cannot understand y my code doesn't work.

i have a gridview in SherlockFragment and i updating the gridview items in an asyncTask.

Here is my xml for gridView,

<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:horizontalSpacing="4dip"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="10dip"
android:padding="4dip"
android:background="#000000"
 />

and GridView items xml is.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="140dp"
android:layout_height="150dp"
android:paddingLeft="4dp"
android:paddingTop="4dp" >

<ImageView
    android:id="@+id/cat_thumbnail"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:contentDescription="@string/content_description"
    android:scaleType="fitXY"
    android:src="@drawable/logo" />

<TextView
    android:id="@+id/cat_title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/cat_thumbnail"
    android:background="#80a0a0a0"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="#ffffff" />

and the code that i am using is.,

extends SherlockFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    final View v = inflater.inflate(R.layout.grid_layout, container, false);
    gridView = (GridView) (v.findViewById(R.id.gridview));
    return v;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    imageLoader = ImageLoader.getInstance();
    setHasOptionsMenu(false);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    setRetainInstance(true);
    CategoryItems = new ArrayList<HashMap<String, String>>();
    new GridImages().execute();
    gridView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            HashMap<String, String> tempMap = new HashMap<String, String>();
            tempMap = CategoryItems.get(position);
            String Catid = tempMap.get(TAG_ID);
            Fragment videoFragment = new VideosFragment();
            Bundle bundle = new Bundle();
            bundle.putString(TAG_ID, Catid);
            videoFragment.setArguments(bundle);
            FragmentChangeActivity fca = (FragmentChangeActivity) getActivity();
            fca.switchContent(videoFragment);
        }
    });

}

public class ImageAdapter extends BaseAdapter {

    public class ViewHolder {
        public TextView title;
        public ImageView thumbnail;
    }

    private Context mContext;

    public ImageAdapter(Context ctx) {
        mContext = ctx;
    }

    @Override
    public int getCount() {
        return imageUrls.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        final ViewHolder holder;
        if (convertView == null) {
            view = LayoutInflater.from(mContext).inflate(
                    R.layout.category_list_row, parent, false);
            holder = new ViewHolder();
            holder.title = (TextView) view.findViewById(R.id.cat_title);
            holder.title.setSelected(false);
            holder.thumbnail = (ImageView) view
                    .findViewById(R.id.cat_thumbnail);
            holder.thumbnail.setSelected(false);

            view.setTag(holder);

        } else {
            holder = (ViewHolder) view.getTag();
            view=(View) convertView;
            view.setTag(holder);
        }
        HashMap<String, String> tempMap = new HashMap<String, String>();

        tempMap = CategoryItems.get(position);
        holder.title.setText(tempMap.get(TAG_NAME));
        imageLoader.displayImage(imageUrls[position], holder.thumbnail,
                defaultOptions);
                    holder.title.setOnClickListener(forItemClick);
        holder.thumbnail.setOnClickListener(forItemClick);
        return view;
    }
}
  private OnClickListener forItemClick=new OnClickListener() {

    @Override
    public void onClick(View v) {
        Toast.makeText(getActivity(), "kjsdhkfds", Toast.LENGTH_LONG).show();
    }
};

class GridImages extends AsyncTask<Void, Void, Void> {
    ProgressDialog dialog = new ProgressDialog(
            CategoryFragment.this.getActivity());

    @Override
    protected Void doInBackground(Void... params) {
        parsing();
        return null;
    }

    protected void onPreExecute() {
        dialog.setMessage("Please wait...");
        dialog.setIndeterminate(true);
        dialog.show();
    }

    protected void onPostExecute(Void unused) {
        gridView.setAdapter(new ImageAdapter(getActivity()));
        dialog.dismiss();
    }

}

private void parsing() {
    JSONParser jParser = new JSONParser();
    JSONObject json = jParser.getJSONFromUrl(url.trim());
    try {
        JSONObject Collection = json.getJSONObject(TAG_HEADER);
        Category = Collection.getJSONArray(TAG_CATEGORY);
        for (int i = 0; i < Category.length(); i++) {
            JSONObject temp = Category.getJSONObject(i);
            String id = temp.getString(TAG_ID);
            String name = temp.getString(TAG_NAME);
            String thumbnail = temp.getString(TAG_THUMBNAIL);

            HashMap<String, String> map = new HashMap<String, String>();

            map.put(TAG_ID, id);
            map.put(TAG_NAME, name);
            map.put(TAG_THUMBNAIL, thumbnail);

            CategoryItems.add(map);
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }
    int i = 0;
    imageUrls = new String[CategoryItems.size()];
    for (HashMap<String, String> map : CategoryItems) {
        imageUrls[i] = map.get(TAG_THUMBNAIL).trim();
        System.out.println(imageUrls[i]);
        i++;
    }
    defaultOptions = new DisplayImageOptions.Builder().cacheInMemory()
            .showStubImage(R.drawable.logo).resetViewBeforeLoading()
            .cacheOnDisc().build();

    config = new ImageLoaderConfiguration.Builder(getActivity()
            .getApplicationContext()).defaultDisplayImageOptions(
            defaultOptions).build();
    imageLoader.init(config);
}

please tell me what i am doing wrong., i cannot find it.,

i cannot perform onClick event for my gridView.

I I have updated my code now works., but this will not be the final solution i think because i didn't made the gridview clickable.valuable comments are expected always Thaks @Dhaval Sodha Parmar.,

Tamilselvan Kalimuthu
  • 1,534
  • 1
  • 13
  • 28

1 Answers1

0

write your onItemClickListener after you set the adapter in the onPostExecute method of the AsyncTask as shown below.

class GridImages extends AsyncTask<Void, Void, Void> {

@Override
protected Void doInBackground(Void... params) {
    //your background task code
    return null;
}

protected void onPostExecute(Void params) {

    getActivity().runOnUiThread(new Runnable() {

    public void run() {
        //add the below line
       gridView = (GridView)getView().findViewById(R.id.gridView);

       gridView.setAdapter(new ImageAdapter(getActivity()));

       /*gridView.setOnItemClickListener(new OnItemClickListener() {

       @Override
       public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {

        }
      });*/

     gridView.setOnItemSelectedListener(new OnItemSelectedListener() {

     @Override
     public void onItemSelected(AdapterView<?> arg0,View arg1, int arg2, long arg3) {
     // TODO Auto-generated method stub

     }

     @Override
     public void onNothingSelected(AdapterView<?> arg0) {
     // TODO Auto-generated method stub

     }
    });
  }
}
SKK
  • 5,261
  • 3
  • 27
  • 39
  • i have tried that too., but the onitemClick event not working – Tamilselvan Kalimuthu Mar 20 '13 at 05:00
  • Check now. I would have not posted it as answer if it was not working for me. – SKK Mar 20 '13 at 05:06
  • Add a Log Statement and check whether it prints something. The same approach works for both listView and GridView and I have used ABS itself. The only line which i haven't written in my apps is setRetainInstance(true); but i don't know if that is a problem. also, instead of having the GridView itself as the root element i have placed in inside a Relative layout as i have other Controls also. – SKK Mar 20 '13 at 05:22
  • then y my code doesn't work. is there anything wrong in my xml code ah? please review my full code.@Santhosh – Tamilselvan Kalimuthu Mar 20 '13 at 05:26
  • Let me know, If nothing what i have suggested has worked for you. i will be happy to delete my answer. – SKK Mar 20 '13 at 05:28
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/26511/discussion-between-tamilselvan-and-santhosh) – Tamilselvan Kalimuthu Mar 20 '13 at 06:15