gpt4 book ai didi

java - 如何将 ListView 的远程图像转移到另一个布局,只需单击该项目? ...在 Android Studio 中

转载 作者:行者123 更新时间:2023-12-01 09:37:37 25 4
gpt4 key购买 nike

我制作了服务器的 ListView ,将图像发送到智能手机,并且我在那里运行的代码,结果是这样的:

enter image description here

现在我想做到这一点,当您单击列表中的某个项目时,可以在另一个布局中查看完整的图像,并且只能通过使用 Intent 来获取此图像。

我不明白如何在onItemClick方法中传递参数。请有人告诉我解决方案。代码是这样的:

public class MainActivity extends AppCompatActivity {

private ListView listView;
private ProgressDialog progressDialog;
ArrayList asuntos=new ArrayList();
ArrayList imagen=new ArrayList();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolBar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolBar);
listView=(ListView) findViewById(R.id.list);
progressDialog = new ProgressDialog(this);
progressDialog.setCancelable(false);
lista o=new lista();
o.obtenerAvisos();

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

Intent intent = new Intent(MainActivity.this, Visor.class);
startActivity(intent);
}
});
}

public class lista {

public void obtenerAvisos() {

asuntos.clear();
imagen.clear();

String tag_string_req = "req_data";

progressDialog.setMessage("Conectando...");
showDialog();

StringRequest strReq = new StringRequest(Request.Method.POST, AppURLs.URL, new Response.Listener<String>() {

@Override
public void onResponse(String response) {

hideDialog();

try {

JSONArray jsonArray = new JSONArray(response);

for (int i = 0; i < jsonArray.length(); i++) {
asuntos.add(jsonArray.getJSONObject(i).getString("asunto"));
imagen.add(jsonArray.getJSONObject(i).getString("publicacion"));
}

listView.setAdapter(new ImagenAdaptador(getApplicationContext()));
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {

Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
hideDialog();
}
}) {

@Override
protected Map<String, String> getParams() {

Map<String, String> params = new HashMap<String, String>();
params.put("tag", "data");
return params;
}

};
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}
}

private void showDialog() {

if (!progressDialog.isShowing())
progressDialog.show();
}

private void hideDialog() {

if (progressDialog.isShowing())
progressDialog.dismiss();
}

public class ImagenAdaptador extends BaseAdapter {

Context ctx;
LayoutInflater layoutInflater;
SmartImageView smartImageView;
TextView tvasunto;

public ImagenAdaptador(Context applicationContext) {

this.ctx=applicationContext;
layoutInflater=(LayoutInflater)ctx.getSystemService(LAYOUT_INFLATER_SERVICE);
}

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

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

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

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

ViewGroup viewGroup=(ViewGroup) layoutInflater.inflate(R.layout.activity_main_items,null);
smartImageView=(SmartImageView)viewGroup.findViewById(R.id.imagen1);
tvasunto=(TextView) viewGroup.findViewById(R.id.tvAsunto);
String urlfinal="http://192.168.43.45/xxxx/xxxxx/"+imagen.get(position).toString();
Rect rect=new Rect(smartImageView.getLeft(), smartImageView.getTop(), smartImageView.getRight(), smartImageView.getBottom());
smartImageView.setImageUrl(urlfinal, rect);
tvasunto.setText(asuntos.get(position).toString());
return viewGroup;
}
}
}

注意:使用这些库:
com.mcxiaoke.volley:library:1.0.19

com.github.snowdream.android:smartimageview:0.0.2

最佳答案

在这种情况下,您不需要,您已经可以访问“imagen arrayList”:

public static final string ARG_IMAGE_URL = "URL";

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

Intent intent = new Intent(MainActivity.this, Visor.class);
intent.putExtras(ARG_IMAGE_URL, imagen.get(position))
startActivity(intent);
}
});

但是您必须考虑一种策略来避免从网络重新加载图像,这会浪费资源。

关于java - 如何将 ListView 的远程图像转移到另一个布局,只需单击该项目? ...在 Android Studio 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38727815/

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