私はこれに関する多くの投稿を見てきましたが、私のコードが機能しないことを理解できません。
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イベントを実行できません。
コードを更新しましたが、グリッドビューをクリック可能にしなかったため、これは最終的な解決策ではないと思います。貴重なコメントは常にThaks @DhavalSodhaParmarに期待されます。