【问题标题】:Pass String[] to another class Android (using intent) [duplicate]将 String[] 传递给另一个 Android 类(使用意图)[重复]
【发布时间】:2018-07-10 10:54:00
【问题描述】:

我正在尝试通过意图传递字符串 [],但出现错误。 我想将“arr”发送到其他类“ImageAdapter”并分配“String[] mThumbIds”和“imageTotal”。

基本上,我正在显示图像的网格视图,并且图像链接正在从 MySQL 数据库中获取并分配给“arr”,这将是完美的,但我想将该 String[] 传递给另一个类,但我收到了错误和错误。 这是一项小工作,但我是 android 新手,请帮帮我。 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);
}

}

图像适配器

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;
}

}

【问题讨论】:

  • 您正在使用适配器来创建意图。您必须使用活动 new Intent(MainActivity.this, ImageAdapter.class);
  • 你可以使用arraylist代替字符串数组,然后使用intent.putStringArrayListExtra()
  • ImageAdapter,不是活动。您需要传递数据不活动,然后从此活动调用适配器
  • 据我所知,您不能将意图从活动发送到适配器,您可以将其发送到使用此适配器的活动,并将数组放入适配器构造函数中,就像我使用它的方式一样

标签: java android mysql json


【解决方案1】:

您不能将意图从活动发送到适配器

首先将intent发送给Activity

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

在您的适配器中

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
    }

从示例 Activiy 接收意图

Bundle bundle = getIntent().getExtras();
String [] mThumbIds= bundle.getStringArray("arr");
ImageAdapter imageAdapter = new ImageAdapter (example.this,mThumbIds);

【讨论】:

  • 我做了,但 getIntent 给出了一个错误
  • 是的,因为您是从适配器接收它,所以您应该从您正在使用此适配器的活动中收到意图,请仔细阅读我的回答
  • 它有效,但我删除了这样的意图和参数
  • gridview.setAdapter(new ImageAdapter(this,arr,arr.length));
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-07
相关资源
最近更新 更多