0

我看过很多关于此的帖子,但无法理解我的代码不起作用。

我在 SherlockFragment 中有一个 gridview,我在 asyncTask 中更新了 gridview 项目。

这是我的gridView xml,

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

和 GridView 项目 xml 是。

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

我正在使用的代码是。

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

请告诉我我做错了什么。,我找不到它。,

我无法为我的 gridView 执行 onClick 事件。

已经更新了我的代码现在可以工作了。但这不会是我认为的最终解决方案,因为我没有使 gridview 可点击。预计总是会有有价值的评论 Thaks @Dhaval Sodha Parmar。,

4

1 回答 1

0

在 AsyncTask 的 onPostExecute 方法中设置适配器后,编写您的 onItemClickListener,如下所示。

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

     }
    });
  }
}
于 2013-03-20T04:54:42.790 回答