单击某个项目时,图像不会显示在详细活动上

时间:2017-04-25 23:25:19

标签: android listview android-intent android-volley imageloader

我正在使用凌空库来进行网络连接。当我点击列表中的项目时,我试图将图像加载到下一个屏幕上。在XML文件中,我使用“NetworkImageView”来保存图像。我正在使用意图在描述页面上传递视图。

先谢谢

以下是代码:

列出活动 此活动显示存储在数据库中的项目列表

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 HallDescriptionActivity extends AppCompatActivity implements View.OnClickListener {

private static final String TAG = HallDescriptionActivity.class.getSimpleName();

TextView tvTitle, tvDesc, tvLocation, tvPrice;
NetworkImageView ivImage;
Button btnReserve;
ImageLoader imageLoader;
Hall h;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_hall_description);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    imageLoader = MySingleton.getInstance().getImageLoader();
    // Initialize the views
    tvTitle = (TextView) findViewById(R.id.tvTitle);
    ivImage = (NetworkImageView) findViewById(R.id.ivImage);
    tvDesc = (TextView) findViewById(R.id.tvDesc);
    tvLocation = (TextView) findViewById(R.id.tvLocation);
    tvPrice = (TextView) findViewById(R.id.tvPrice);

    btnReserve = (Button) findViewById(R.id.btnReserve);
    btnReserve.setOnClickListener(this);

    try {
        Intent iDescription = getIntent();

        h = new Hall();
        Log.d(TAG, "onCreate: hall " + h);
        // The text and image from the list page is passed onto this page using intents
        h.setTitle(iDescription.getStringExtra("title"));
        //Get the result of image
        h.setThumbnailUrl(iDescription.getStringExtra("image"));
        //Get the result of description
        h.setDescription(iDescription.getStringExtra("description"));
        //Get the result of location
        h.setLocation(iDescription.getStringExtra("location"));
        //Get the result of price
        h.setPrice(iDescription.getStringExtra("price"));

        // The views then set to their appropriate views
        tvTitle.setText(h.getTitle());
        ivImage.setImageUrl(h.getThumbnailUrl(), imageLoader);
        tvDesc.setText(h.getDescription());
        tvLocation.setText(h.getLocation());
        tvPrice.setText("£" + h.getPrice());

    } catch (Exception e) {
        Log.e(TAG, e.getMessage());
    }
}

@Override
public void onClick(View v) {
    if(v == btnReserve) {
        startActivity(new Intent(getApplicationContext(), OrderActivity.class));
    }
}
}

适配器类

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

1 个答案:

答案 0 :(得分:0)

https://www.simplifiedcoding.net/picasso-android-tutorial-picasso-image-loader-library/

试试这个 - 我认为你将图像作为网址,如果是这样的话,请将它保存在一个arraylist中。 - 使arraylist静止,这样你就可以在任何活动中调用相同的arraylist - 在你喜欢的活动中使用毕加索图像加载器。