如何在使用Json时使用parcable?

时间:2015-11-22 12:42:33

标签: android

我有这个从服务器获取nametry { JSONObject jsonObj = new JSONObject(myJSON); peoples = jsonObj.getJSONArray("result"); System.out.println(peoples.length()); Listitem = new ArrayList<Listitem>(); for(int i=0;i<peoples.length();i++){ JSONObject c = peoples.getJSONObject(i); // String id ="2"; String id= c.getString("ID"); String url = c.getString("URL"); Log.d("Id: ", id); int intid = 0; Student student = new Student(c.getString("ID"), "hhe", c.getString("URL") ); Intent intent = new Intent(this,); // Passing data as a parecelable object to StudentViewActivity intent.putExtra("student",student); // Opening the activity startActivity(intent); try { intid = Integer.parseInt(id.toString()); } catch(NumberFormatException nfe) { System.out.println("Could not parse " + nfe); } 的json代码,但是我不能在json中使用startActivity(intent);实际上我可以只在parcable类中插入数据而不启动意图吗?

public class Student implements Parcelable{
    String mSid;
    String mSName;
    String mSUrl;


    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }

    /**
     * Storing the Student data to Parcel object
     **/
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(mSid);
        dest.writeString(mSName);
        dest.writeString(mSUrl);
    }

    /**
     * A constructor that initializes the Student object
     **/
    public Student(String mSid,String sName, String mSUrl ){
        this.mSid = mSid;
        this.mSName = sName;
        this.mSUrl = mSUrl;
    }

    /**
     * Retrieving Student data from Parcel object
     * This constructor is invoked by the method createFromParcel(Parcel source) of
     * the object CREATOR
     **/
    private Student(Parcel in){
        this.mSid = in.readString();
        this.mSName = in.readString();
        this.mSUrl = in.readString();
    }

    public static final Parcelable.Creator<Student> CREATOR = new Parcelable.Creator<Student>() {

        @Override
        public Student createFromParcel(Parcel source) {
            return new Student(source);
        }

        @Override
        public Student[] newArray(int size) {
            return new Student[size];
        }
    };
}

这是parcable class

startactivity

我在result

上遇到错误

image

修改

这是主要的活动,我收到了arraylist public void downloadImage() { // OkHttpHandler handler = new OkHttpHandler(); OkHttpHandler handler = new OkHttpHandler(MainActivity.this,new OkHttpHandler.MyInterface() { @Override public void myMethod(ArrayList result) { Toast.makeText(MainActivity.this, "Connection Succesful", Toast.LENGTH_LONG).show(); GridViewAdapter adapter = new GridViewAdapter(getApplicationContext(), R.layout.grid_item_layout, result); adapter.notifyDataSetChanged(); mGridView.setAdapter(adapter); } }); handler.execute(); String image = null; try { image = handler.execute().get(); Log.d("e", "hhhh handler"); System.out.print(image); //if (image != null && image.length > 0){ // Log.isLoggable("e",image.length); //Log.d("e", "ddddddd entered the try"); // Bitmap bitmap = BitmapFactory.decodeByteArray(image, 0, image.length); // imageView.setImageBitmap(bitmap); // txtBytes.setText("Total bytes downloaded: " + image.length); } catch (Exception e) { Log.d("e", "rrrrrr error"); e.printStackTrace(); // txtBytes.setText("Hmm sorry, something went wrong!"); } } ...我正在传递给Gridview适配器

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    System.out.println("entering adapter1");

    View row = convertView;
  final  ViewHolder holder;


    if (row == null) {
        LayoutInflater inflater = LayoutInflater.from(mcontext);
        row = inflater.inflate(layoutResourceId, parent, false);
        holder = new ViewHolder();
        holder.imageTitle = (TextView) row.findViewById(R.id.text);
        holder.imageView = (ImageView) row.findViewById(R.id.imageView);
        row.setTag(holder);
    } else {
        holder = (ViewHolder) row.getTag();
    }
    final Listitem item = getItem(position);
    System.out.println("item.getUrl() ");
    System.out.println(item.getUrl());
    Picasso.with(mcontext).setIndicatorsEnabled(true);
    holder.imageTitle.setText(item.getId());
    Picasso.
            with(mcontext).
            load(item.getUrl())
            .placeholder(R.drawable.logo)
            .fit()
            .noFade()
            .into(holder.imageView);

    holder.imageView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Log.d("OnImageButton", "Clicked");


            Intent intnt  =new Intent(mcontext, SingleViewActivity.class);
         //   intnt.putExtra("Contact_list", item);
            intnt.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);


            mcontext.startActivity(intnt)  ; //This line raises error


            Toast.makeText(mcontext, "intent",
                    Toast.LENGTH_LONG).show();
        }
    });


    return row;
}

这是gridview适配器,如何将arraylist数据传递给下一个sibleviewactivity以发布名称和id?

ExpressionModel

0 个答案:

没有答案