Android 单击项目时,图像不会显示在详细活动上
我正在使用截击库进行网络连接。当我点击列表中的一个项目时,我试图将图像加载到下一个屏幕上。在XML文件中,我使用“NetworkImageView”保存图像。我使用intent在描述页面中传递视图 提前谢谢 代码如下: 列出活动 此活动显示存储在数据库中的项目列表Android 单击项目时,图像不会显示在详细活动上,android,listview,android-intent,android-volley,imageloader,Android,Listview,Android Intent,Android Volley,Imageloader,我正在使用截击库进行网络连接。当我点击列表中的一个项目时,我试图将图像加载到下一个屏幕上。在XML文件中,我使用“NetworkImageView”保存图像。我使用intent在描述页面中传递视图 提前谢谢 代码如下: 列出活动 此活动显示存储在数据库中的项目列表 public class HallListActivity extends AppCompatActivity implements SearchView.OnQueryTextListener, AdapterView.On
public class HallListActivity extends AppCompatActivity
implements SearchView.OnQueryTextListener, AdapterView.OnItemClickListener {
// Log tag
private static final String TAG = HallListActivity.class.getSimpleName();
// Movies json url
private static final String url = "http://10.0.2.2/wedding1/halls.php";
private ProgressDialog progressDialog;
private List<Hall> hallList = new ArrayList<Hall>();
private ListView listView;
private HallAdapter adapter;
private Hall h;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hall_list);
getSupportActionBar().setTitle("List of Halls");
listView = (ListView) findViewById(R.id.list);
adapter = new HallAdapter(this, hallList);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
// Create a new progress dialog
progressDialog = new ProgressDialog(this);
// Showing progress dialog before making http request
progressDialog.setMessage("Loading...");
progressDialog.show();
// Creating volley request obj
//String url = "http://10.0.2.2/wedding1/";
JsonArrayRequest hallReq = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
Hall hall = new Hall();
hall.setThumbnailUrl(obj.getString("image"));
hall.setTitle(obj.getString("name"));
hall.setDescription(obj.getString("description"));
hall.setLocation(obj.getString("location"));
hall.setPrice(obj.getString("price"));
// adding hall to movies array
hallList.add(hall);
} catch (JSONException e) {
e.printStackTrace();
}
}
progressDialog.dismiss(); // progress bar disappears once the halls are loaded on screen
/* notifying list adapter about data changes so that it renders the list view with updated data */
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hideProgressDialog();
}
});
// Adding request to request queue
MySingleton.getInstance().addToRequestQueue(hallReq);
}
@Override
public void onDestroy() {
super.onDestroy();
hideProgressDialog();
}
private void hideProgressDialog() {
if (progressDialog != null) {
progressDialog.dismiss();
progressDialog = null;
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.refresh:
startActivity(new Intent(getApplicationContext(), HallListActivity.class));
break;
}
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
MenuItem menuItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(menuItem);
searchView.setOnQueryTextListener(this); // The onQueryTextChange method will invoke
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
newText = newText.toLowerCase();
ArrayList<Hall> newList = new ArrayList<>();
for (Hall hall : hallList) {
String name = hall.getTitle().toLowerCase();
if (name.contains(newText))
newList.add(hall);
}
return true;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
h = hallList.get(position);
// shows the hall name at the bottom once the hall is clicked
Toast.makeText(HallListActivity.this, h.getTitle(), Toast.LENGTH_SHORT).show();
Intent intentDescription = new Intent(HallListActivity.this, HallDescriptionActivity.class);
Log.d(TAG, "onItemClick: title " + h.getTitle());
intentDescription.putExtra("title", h.getTitle());
intentDescription.putExtra("image", h.getThumbnailUrl());
intentDescription.putExtra("description", h.getDescription());
intentDescription.putExtra("location", h.getLocation());
intentDescription.putExtra("price", String.valueOf(h.getPrice()));
intentDescription.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intentDescription);
}
}
适配器类
public class HallAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<Hall> hallItems;
ImageLoader imageLoader = MySingleton.getInstance().getImageLoader();
public HallAdapter(Activity activity, List<Hall> hallItems) {
this.activity = activity;
this.hallItems = hallItems;
}
// Lets tge ListView know how many items to display (returns the size of the data source
@Override
public int getCount() {
return hallItems.size();
}
// returns an item to be placed in a given position of the data source
@Override
public Object getItem(int position) {
return hallItems.get(position);
}
// defines a unique ID for each row in the list
// Use the position of the item as its ID for simplicity
@Override
public long getItemId(int position) {
return position;
}
// Creates the view to be used as a row in the list.
// Defining the information and where it is placed in the ListView.
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.list_item, null);
// image loader is used to check if the permissions are granted or not
if (imageLoader == null)
imageLoader = MySingleton.getInstance().getImageLoader();
// Thumbnail
NetworkImageView thumbnail = (NetworkImageView) convertView.findViewById(R.id.thumbnail);
// Hall name
TextView title = (TextView) convertView.findViewById(R.id.title);
// Rating
TextView description = (TextView) convertView.findViewById(R.id.description);
// genre
TextView location = (TextView) convertView.findViewById(R.id.location);
// year
TextView price = (TextView) convertView.findViewById(R.id.price);
// getting hall data for each row
//Hall h = hallItems.get(position);
Hall h = (Hall) getItem(position);
// Thumbnail image
String url = "http://10.0.2.2/wedding1/";
thumbnail.setImageUrl(url + h.getThumbnailUrl(), imageLoader);
// Title
title.setText(h.getTitle());
// Description
description.setText(String.valueOf(h.getDescription()));
// Location
location.setText(String.valueOf(h.getLocation()));
// Price
price.setText("£" + String.valueOf(h.getPrice()));
return convertView;
}
}
公共类HallAdapter扩展了BaseAdapter{
私人活动;
私人充气机;
私人物品清单;
ImageLoader ImageLoader=MySingleton.getInstance().getImageLoader();
公共HallAdapter(活动、列表项){
这个。活动=活动;
this.hallItems=hallItems;
}
//让tge ListView知道要显示多少项(返回数据源的大小
@凌驾
public int getCount(){
返回hallItems.size();
}
//返回要放置在数据源给定位置的项
@凌驾
公共对象getItem(int位置){
返回项目。获取(位置);
}
//为列表中的每一行定义唯一的ID
//为简单起见,请使用项目的位置作为其ID
@凌驾
公共长getItemId(int位置){
返回位置;
}
//创建要用作列表中的行的视图。
//定义信息及其在ListView中的位置。
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
如果(充气器==null)
充气器=(LayoutInflater)activity.getSystemService(Context.LAYOUT\u充气器\u SERVICE);
if(convertView==null)
convertView=充气机。充气(R.layout.list_项,空);
//图像加载器用于检查是否授予了权限
如果(imageLoader==null)
imageLoader=MySingleton.getInstance().getImageLoader();
//缩略图
NetworkImageView缩略图=(NetworkImageView)convertView.findViewById(R.id.缩略图);
//厅名
TextView title=(TextView)convertView.findViewById(R.id.title);
//评级
TextView description=(TextView)convertView.findViewById(R.id.description);
//体裁
TextView位置=(TextView)convertView.findViewById(R.id.location);
//年
TextView价格=(TextView)convertView.findViewById(R.id.price);
//获取每行的霍尔数据
//霍尔h=霍尔项目。获取(位置);
大厅h=(大厅)获取项目(位置);
//缩略图像
字符串url=”http://10.0.2.2/wedding1/";
setImageUrl(url+h.getThumbnailUrl(),imageLoader);
//头衔
title.setText(h.getTitle());
//描述
description.setText(String.valueOf(h.getDescription());
//位置
location.setText(String.valueOf(h.getLocation());
//价格
price.setText(“£”+String.valueOf(h.getPrice());
返回视图;
}
}
试试这个
-我认为您正在将图像作为url获取,如果是这样,请将其保存在arraylist中。
-使arraylist为静态,以便可以在任何活动中调用相同的arraylist
-在activity u like中使用毕加索图像加载程序。我怀疑
onCreate()的try…catch块中引发了异常
详细信息活动的方法。由于您手动打印stacktrace,您的应用程序不会崩溃。在执行此操作时,您需要小心,因为异常可能会使您的程序处于不稳定状态。在这种情况下,崩溃要比继续运行代码并产生不良结果要好得多。您需要更仔细地考虑您的异常n处理。话虽如此,请在此处张贴stacktrace,以便我们能进一步帮助您。
public class HallAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<Hall> hallItems;
ImageLoader imageLoader = MySingleton.getInstance().getImageLoader();
public HallAdapter(Activity activity, List<Hall> hallItems) {
this.activity = activity;
this.hallItems = hallItems;
}
// Lets tge ListView know how many items to display (returns the size of the data source
@Override
public int getCount() {
return hallItems.size();
}
// returns an item to be placed in a given position of the data source
@Override
public Object getItem(int position) {
return hallItems.get(position);
}
// defines a unique ID for each row in the list
// Use the position of the item as its ID for simplicity
@Override
public long getItemId(int position) {
return position;
}
// Creates the view to be used as a row in the list.
// Defining the information and where it is placed in the ListView.
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.list_item, null);
// image loader is used to check if the permissions are granted or not
if (imageLoader == null)
imageLoader = MySingleton.getInstance().getImageLoader();
// Thumbnail
NetworkImageView thumbnail = (NetworkImageView) convertView.findViewById(R.id.thumbnail);
// Hall name
TextView title = (TextView) convertView.findViewById(R.id.title);
// Rating
TextView description = (TextView) convertView.findViewById(R.id.description);
// genre
TextView location = (TextView) convertView.findViewById(R.id.location);
// year
TextView price = (TextView) convertView.findViewById(R.id.price);
// getting hall data for each row
//Hall h = hallItems.get(position);
Hall h = (Hall) getItem(position);
// Thumbnail image
String url = "http://10.0.2.2/wedding1/";
thumbnail.setImageUrl(url + h.getThumbnailUrl(), imageLoader);
// Title
title.setText(h.getTitle());
// Description
description.setText(String.valueOf(h.getDescription()));
// Location
location.setText(String.valueOf(h.getLocation()));
// Price
price.setText("£" + String.valueOf(h.getPrice()));
return convertView;
}
}