gpt4 book ai didi

java - 将 String[] 传递给另一个 Android 类(使用 Intent )

转载 作者:行者123 更新时间:2023-11-29 17:59:13 25 4
gpt4 key购买 nike

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

基本上,我显示了图像的 GridView ,图像链接从 MySQL 数据库获取并分配给“arr”,这很完美,但我想将该 String[] 传递给另一个类,但我收到错误和错误。这是一项小工作,但我是 Android 新手,请帮助我。主要 Activity

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

}

最佳答案

您无法将 Intent 从 Activity 发送到适配器

首先将 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
}

从示例 Activityiy 接收 Intent

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

关于java - 将 String[] 传递给另一个 Android 类(使用 Intent ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48540320/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com