Android GridView显示详细项目

时间:2016-02-09 16:40:53

标签: android gridview detail

我有一个关于GridView的源代码。 但是,我很困惑为什么有些代码是错误的。 这些是代码。我在MainActivity中的OnClick方法上出错了。在这有人帮助我吗?感谢

public void onItemClick(AdapterView <? > AdapterView, View View, int Int, long Long) {

    AdapterView = ((ImageItem) imageItems.get(Int)).getTitle();
                toDetail(Int, AdapterView);

        }
    });

我希望每个项目在点击时显示详细的图像和文字。我也得到这样的代码但是当我点击“GingerBread”项目时,它会转到第一项(“Astro”)

 public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        ImageItem item = (ImageItem) parent.getItemAtPosition(position);

            //Create intent
        Intent intent = new Intent(getApplicationContext(), DetailActivity.class);
     //  intent.putExtra("android.intent.extra.TEXT", Integer.parseInt(getIntent().getStringExtra("android.intent.extra.TEXT")));
      intent.putExtra("title", item.getTitle());
            //Start details activity
        startActivity(intent);
         overridePendingTransition(R.anim.home_detail_entry, R.anim.home_detail_exit);
       }
    });

MainActivity.java

package com.acer.gridview;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends Activity {

  public static String[] bitmapName = {
    "Astro", "Bender", "Cupcake", "Donut", "Eclair",
    "Froyo", "Gingerbread", "HoneyComb"
  };
  public static int[] bitmapId = {
    R.drawable.Astro, R.drawable.Bender, R.drawable.Cupcake, R.drawable.Eclair,
      R.drawable.Froyo, R.drawable.Gingerbread, R.drawable.HoneyComb
  };

  private GridViewAdapter gridAdapter;
  private GridView gridView;
  ArrayList < ImageItem > imageItems;

  private ArrayList < ImageItem > getData() {
    imageItems = new ArrayList();
    BitmapFactory.Options localOptions = new BitmapFactory.Options();
    localOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
    int i = 0;
    for (;;) {
      if (i > 7) {
        return imageItems;
      }
      Bitmap localBitmap = BitmapFactory.decodeResource(getResources(), bitmapId[i], localOptions);
      imageItems.add(new ImageItem(localBitmap, bitmapName[i]));
      i += 1;
    }
  }

  @
  Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_image_grid);

    gridView = (GridView) findViewById(R.id.gridView);
    gridAdapter = new GridViewAdapter(this, R.layout.row_grid, getData());
    gridView.setAdapter(gridAdapter);
    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

      public void onItemClick(AdapterView <? > AdapterView, View View, int Int, long Long) {

////////////This code shown error///////////////////

    AdapterView = ((ImageItem) imageItems.get(Int)).getTitle();
                toDetail(Int, AdapterView);

////////////This code shown error///////////////////

      }
    });
  }

  public void toDetail(int Int, String String) {
    Intent localIntent = new Intent(this, DetailActivity.class);
    localIntent.putExtra("android.intent.extra.TEXT", Integer.toString(Int));
    localIntent.putExtra("title", String);
    startActivity(localIntent);

  }

  /**
   * A placeholder fragment containing a simple view.
   */
  private boolean ActionBar(final Intent aIntent) {
    try {
      startActivity(aIntent);
      return true;
    } catch (ActivityNotFoundException e) {
      return false;
    }
  }

  public void showDialog() {
    LayoutInflater inflater = getLayoutInflater();
    View localView = inflater.inflate(R.layout.help_dialog, null);
    ((ImageView) localView.findViewById(R.id.imageView1)).setImageResource(R.drawable.makhrojulhuruf);
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setView(localView);
    builder.show();
  }



  @
  Override
  public boolean onCreateOptionsMenu(Menu menu) {}

  @
  Override
  public boolean onOptionsItemSelected(MenuItem item) {


  }

}

GridViewAdapter.java

    package com.acer.gridview;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * Created by Acer on 08/02/2016.
 */
public class GridViewAdapter extends ArrayAdapter<ImageItem> {
    private Context context;
    private int layoutResourceId;
    private ArrayList<ImageItem> data = new ArrayList();

    public GridViewAdapter(Context context, int layoutResourceId, ArrayList<ImageItem> data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        ViewHolder holder = null;

        if (row == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);
            holder = new ViewHolder();
            holder.imageTitle = (TextView) row.findViewById(R.id.text);
            holder.image = (ImageView) row.findViewById(R.id.image);
            row.setTag(holder);
        } else {
            holder = (ViewHolder) row.getTag();
        }

        ImageItem item = (ImageItem) data.get(position);
        holder.imageTitle.setText(item.getTitle());
        holder.image.setImageBitmap(item.getImage());
        return row;
    }

    static class ViewHolder {
        TextView imageTitle;
        ImageView image;

    }
}

ImageItem.java

package com.acer.gridview;

import android.graphics.Bitmap;

/**
 * Created by Acer on 08/02/2016.
 */
public class ImageItem {
    private Bitmap image;
    private String title;

    public ImageItem(Bitmap Bitmap, String String)
    {
        setImage(Bitmap);
        setTitle(String);
    }

    public Bitmap getImage()
    {
        return this.image;
    }

    public String getTitle()
    {
        return this.title;
    }

    public void setImage(Bitmap Bitmap)
    {
        this.image = Bitmap;
    }

    public void setTitle(String String)
    {
        this.title = String;
    }
}

0 个答案:

没有答案