0

I am trying to pass through intent a string[] but I am getting an error. I want to send the "arr" to other class "ImageAdapter" and assign the 'String[] mThumbIds' and 'imageTotal'.

Basically, I am showing a grid view of images and images link are fetching from the MySQL database and assign to the "arr" which is going perfect but I want to pass that String[] to another class and I am getting error and error. It's a small work but I am new in android please help me out. MainActivity

public class MainActivity extends AppCompatActivity {
public static final String URL_LOGIN= "http://10.0.2.2/e-stitch/fatchimg.php?apicall=shirt";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    userLogin();

    final GridView gridview = (GridView) findViewById(R.id.gridview);
   // gridview.setAdapter(new ImageAdapter(this));
   // gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
   //     public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
   //     }
    //       // Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
            //i.putExtra("id", position);
            //startActivity(i);
  //      }
   // });
}

private void userLogin() {
    //if everything is fine
    StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_LOGIN,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    //progressBar.setVisibility(View.GONE);

                    try {
                        JSONObject obj = new JSONObject(response);

                        if (!obj.getBoolean("error")) {
                            //Toast.makeText(getApplicationContext(), obj.getString("message"), Toast.LENGTH_SHORT).show();
                            JSONArray arrJson = obj.getJSONArray("user");
                            String[] arr = new String[arrJson.length()];
                            for(int i = 0; i < arrJson.length(); i++) {
                                arr[i] = arrJson.getString(i);
                            }

                            Intent intent = new Intent(MainActivity.this, ImageAdapter.class);
                            intent.putExtra("arr", arr);
                            startActivity(intent);

                            //finish();
                        } else {
                            //Toast.makeText(getApplicationContext(), obj.getString("message"), Toast.LENGTH_SHORT).show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    //Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
                }
            }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<>();
            params.put("shirt", "shirt");
            return params;
        }

    };

    VolleySingleton.getInstance(this).addToRequestQueue(stringRequest);
}

}

ImageAdapter

public class ImageAdapter extends BaseAdapter {

private Context mContext;
public static String[] mThumbIds;
int imageTotal;

Bundle bundle = getIntent().getExtras();
mThumbIds= bundle.getString("arr");


public ImageAdapter(Context c) {
    mContext = c;
}
public int getCount() {
    return imageTotal;
}


public void setitem() {


    this.mThumbIds=mThumbIds;
    imageTotal = mThumbIds.length;
}

@Override
public String getItem(int position) {

    return mThumbIds[position];
}

public long getItemId(int position) {
    return 0;
}

public View getView(int position, View convertView, ViewGroup parent) {

    ImageView imageView;
    if (convertView == null) {
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(480, 480));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
    } else {
        imageView = (ImageView) convertView;
    }
    String url = getItem(position);
    Picasso.with(mContext)
            .load(url)
            .placeholder(R.drawable.loader)
            .fit()
            .centerCrop().into(imageView);
    return imageView;
}

}

  • Your are using adapter to create intent. You must use activity **new Intent(MainActivity.this, ImageAdapter.class);** – Kenany Jan 31 '18 at 10:56
  • instead of string array you can use arraylist and then use intent.putStringArrayListExtra() – Ezio Jan 31 '18 at 10:59
  • ImageAdapter, not activity. you need to Pass data inactivity then call adapter from this activity – Shohel Rana Jan 31 '18 at 11:07
  • from what i know you can not send intent from activity to adapter you can send it to the activity that uses this adapter and put the array in adapter constructor that the way i would use it – pola alper Jan 31 '18 at 11:09

1 Answers1

0

you can not send intent from activity to adapter

first send the intent to Activity

Intent intent = new Intent(MainActivity.this, exmaple.class);
Bundle extras = new Bundle();
extras.putStringArray("arr", arr);
intent.putExtras(extras);
startActivity(intent);

in your adapter

public class ImageAdapter extends BaseAdapter {



private Context mContext;
public static String[] mThumbIds;
int imageTotal;




 public ImageAdapter(Context c,String[] mThumbIds) {
        mContext = c;
this.mThumbIds= mThumbIds
    }

receive the intent from the example Activiy

Bundle bundle = getIntent().getExtras();
String [] mThumbIds= bundle.getStringArray("arr");
ImageAdapter imageAdapter = new ImageAdapter (example.this,mThumbIds);
pola alper
  • 194
  • 1
  • 10