将数据从Gridview传递到Android中的新活动

时间:2018-12-19 02:57:03

标签: android android-intent parameter-passing

我在将ID号从SQLite数据库传递到新的第二个活动页面时遇到问题。我认为我的传递ID为空。但是我不知道我需要纠正哪一部分。而且我认为它也与String和ArrayList有关。第一次编码是主要活动。第二个编码是第二个活动(ImageList)。第三个编码是CustomGridAdapter到第一页。第四个编码是CustomGridAdapter1到第二页。任何人都可以纠正我的编码。

gridView = (GridView) findViewById(R.id.gridView1);
        imageList = new ArrayList<>();
        adapter = new CustomGridAdapter(this, R.layout.image_item, imageList);
        gridView.setAdapter(adapter);

        // get all data from sqlite
        Cursor cursor = databaseAccess.getData("SELECT * FROM IMAGE");
        imageList.clear();
        while (cursor.moveToNext()) {
            String id = cursor.getString(0);
            String name = cursor.getString(1);
            byte[] image = cursor.getBlob(2);

            imageList.add(new Things(id, name, image));
        }
        adapter.notifyDataSetChanged();

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
                view.setSelected(true);
                final Things labels=imageList.get(position);
                Intent passIntent = new Intent(MainActivity.this, ImageList.class);
                passIntent.putExtra("keyid", String.valueOf(labels));
                startActivity(passIntent);

            }
        });

gridView = (GridView) findViewById(R.id.gridView2);
       imageList = new ArrayList<>();
        adapter = new CustomGridAdapter1(this, R.layout.image_general);
        gridView.setAdapter(adapter);

        Bundle showData = getIntent().getExtras();
        rowId = showData.getString("keyid");
        databaseAccess = new DatabaseAccess(getApplicationContext());
        cursor = databaseAccess.getImage(rowId);
        if(cursor.moveToFirst()) {
            do{
            String id = cursor.getString(1);
            String name = cursor.getString(2);
            byte[] image = cursor.getBlob(3);
            String imgid = cursor.getString(4);

            General general =new General(id, name, image,imgid);
            adapter.add(general);
            } while(cursor.moveToNext());
        }
        adapter.notifyDataSetChanged();

    }

public class CustomGridAdapter  extends BaseAdapter {

    private Context context;
    private int layout;
//    private ArrayList<Things> ImageList;
    List ImageList= new ArrayList();

    public CustomGridAdapter(Context aContext,  int layout, List ImageList) {
        this.context = aContext;
        this.ImageList =ImageList;
        this.layout=layout;
    }
    @Override
    public int getCount() {
        return ImageList.size();
    }

    @Override
    public Object getItem(int position) {
        return ImageList.get(position);
    }

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

    private class ViewHolder {
        TextView Thing;
        ImageView ThingImage;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = new ViewHolder();
        if (convertView == null) {
//            convertView = layoutInflater.inflate(R.layout.gridgeneral, null);
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.image_item, parent, false);
            holder.ThingImage = (ImageView) convertView.findViewById(R.id.imageView_flag);
            holder.Thing= (TextView) convertView.findViewById(R.id.thing);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Things things= (Things) this.getItem(position);
        holder.Thing.setText(things.getName());

        byte[] thingName = things.getImage();
        Bitmap bitmap = BitmapFactory.decodeByteArray(thingName, 0, thingName.length);
        holder.ThingImage.setImageBitmap(bitmap);

        return convertView;
    }

}

public class CustomGridAdapter1  extends BaseAdapter {

    private Context context;
    private int layout;
//    private ArrayList<General> ImageList;
    List ImageList= new ArrayList();

    public CustomGridAdapter1(Context aContext, int layout) {
        this.context = aContext;
        this.ImageList = ImageList;
        this.layout = layout;
    }

    @Override
    public int getCount() {
        return ImageList.size();
    }

    @Override
    public Object getItem(int position) {
        return ImageList.get(position);
    }

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

    public void add(General general) {
        ImageList.add(general);
    }

    private class ViewHolder {
        TextView Thing;
        ImageView ThingImage;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = new ViewHolder();
        if (convertView == null) {
//            convertView = layoutInflater.inflate(R.layout.gridgeneral, null);
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.image_general, null);
            holder.ThingImage = (ImageView) convertView.findViewById(R.id.imageView_flag1);
            holder.Thing = (TextView) convertView.findViewById(R.id.thing1);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }

//        General general = ImageList.get(position);
        General general=(General) this.getItem(position);

        holder.Thing.setText(general.getName());
        byte[] thingName = general.getImage();
        Bitmap bitmap = BitmapFactory.decodeByteArray(thingName, 0, thingName.length);
        holder.ThingImage.setImageBitmap(bitmap);

        return convertView;
    }

}

1 个答案:

答案 0 :(得分:0)

如果要通过POJO类对象或模型对象中的数据将数据从一个活动发送到另一个活动,则可以使用GSON转换要意图转移的模型。

Things labels=imageList.get(position);

您正在尝试将标签发送到您的下一个活动。因此,您想在不同的活动之间传递数据,但是您传递的是一个自定义对象列表,该列表无法使用putExtra函数进行解析。

您可以使用Gson + Type令牌发送数据。

通过下面的示例:

在名为事物的模型内部。向每个私有数据添加注释。

import com.google.gson.annotations.SerializedName;
public class Student{

   @SerializedName("student")
    private String student;
   @SerializedName("id")
    private int studentID;

      public Student(String student, int studentID){
         this.student = student;
         this.studentID = studentID;
      }
}
  

将数据传递给Intent时,您需要首先初始化一个gson对象,然后键入以将您自己的对象转换为json字符串。

Things labels=imageList.get(position);
Gson gson = new Gson();
Type type = new TypeToken<Things>() {}.getType();
String json = gson.toJson(labels, type);

 Intent passIntent = new Intent(MainActivity.this, ImageList.class);
 passIntent.putExtra("keyid", json);
 startActivity(passIntent);
  
    

在您下一个活动或目标活动的onCreate方法内部,即在您的     情况是ImageList.java

  
Gson gson = new Gson();
String stringLocation = data.getStringExtra("keyid");
if(stringLocation != null) {
  Type type = new TypeToken<Things>>() {
   }.getType();
   Things label = gson.fromJson(stringLocation, type);
   Log.d("Data Recieved", label.getId());
}
else{
Log.d("Location Count","failed");
}