【问题标题】:Different Intents for Each List Item Android每个列表项的不同意图 Android
【发布时间】:2017-02-25 02:52:36
【问题描述】:

我正在尝试为 Coins.java 中的每个列表项赋予意图。当我添加 Intent 时,它会将我带到同一页面。我在 StackOverflow 上搜索可能的答案。许多答案说使用 switch 语句来给出不同的意图。但是,在我的应用程序中,列表中的项目数不是一个常数。它可能是 10 或 5 甚至 100。我在 StackOverflow 中搜索了答案,但我可以找到任何适用于我的应用程序的答案。谁能告诉我该怎么做?提前致谢。

  1. Coins.java

    package com.example.android.cotescol;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AdapterView;
    import android.widget.ListView;
    import java.util.ArrayList;
    
    public class Coins extends android.support.v4.app.Fragment{    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.currency_list, container, false);
            final ArrayList<CoinObject> coins = new ArrayList<CoinObject>();
            coins.add(new CoinObject(1, "India", 1947));
            coins.add(new CoinObject(1, "India", 1947));
    
            CoinAdapter itemsAdapter = new CoinAdapter(getActivity(), coins);
            ListView listView = (ListView) rootView.findViewById(R.id.list);
            listView.setAdapter(itemsAdapter);
    
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    CoinObject coinObject = coins.get(position);
                    Intent intent = new Intent(Coins.this.getActivity(), DetailsActivity.class);
                    startActivity(intent);
                }
            });
    
            return rootView;
        }
    }
    
  2. CoinObject.java

    package com.example.android.cotescol;
    
    import java.util.HashMap;
    import java.util.Locale;
    import java.util.Map;
    
    public class CoinObject {
    
        private final static int NO_IMAGE_AVAILABLE = R.drawable.nia;
        private final static String NOT_AVAILABLE = "Material Not Specified";
        private final static double NOT_KNOWN = 0.00;
    
        private int denomination;
        private String country;
        private String countryCode;
        private int year;
        private int obverseImageResourceId = NO_IMAGE_AVAILABLE;
        private int reverseImageResourceId = NO_IMAGE_AVAILABLE;
        private String material = NOT_AVAILABLE;
        private double diameter = NOT_KNOWN;
        private double weight = NOT_KNOWN;
        private double thickness = NOT_KNOWN;
        private double value = NOT_KNOWN;
    
        public CoinObject(int denomination, String country, int year, int obverseImageResourceId, int reverseImageResourceId , double diameter, double thickness, String material, double weight, double value) {
            this.denomination = denomination;
            this.country = country;
            this.year = year;
            this.obverseImageResourceId = obverseImageResourceId;
            this.reverseImageResourceId = reverseImageResourceId;
            this.diameter = diameter;
            this.thickness = thickness;
            this.material = material;
            this.weight = weight;
            this.value = value;
            this.countryCode = getCountryCode(country);
        }
    
        public CoinObject(int denomination, String country, int year, double diameter, double thickness, String material, double weight, double value) {
            this.denomination = denomination;
            this.country = country;
            this.year = year;
            this.diameter = diameter;
            this.thickness = thickness;
            this.material = material;
            this.weight = weight;
            this.value = value;
            this.countryCode = getCountryCode(country);
        }
    
        public CoinObject(int denomination, String country, int year) {
            this.denomination = denomination;
            this.country = country;
            this.year = year;
            this.countryCode = getCountryCode(country);
        }
    
        public String getCountryCode(String countryName) {
            // Get all country codes in a string array.
            String[] isoCountryCodes = Locale.getISOCountries();
            Map<String, String> countryMap = new HashMap<>();
            // Iterate through all country codes:
            for (String code : isoCountryCodes) {
                // Create a locale using each country code
                Locale locale = new Locale("", code);
                // Get country name for each code.
                String name = locale.getDisplayCountry();
                // Map all country names and codes in key - value pairs.
                countryMap.put(name, code);
            }
            // Get the country code for the given country name using the map.
            // Here you will need some validation or better yet
            // a list of countries to give to user to choose from.
            String countryCode = countryMap.get(countryName); // "NL" for Netherlands.  
    
            return countryCode;
        }
    
        public int getDenomination() { return denomination; }
        public int getYear() { return year; }
        public int getObverseImageResourceId() { return obverseImageResourceId; }
        public int getReverseImageResourceId() { return reverseImageResourceId; }
        public double getDiameter() { return diameter; }
        public double getWeight() { return weight; }
        public double getThickness() { return thickness; }
        public double getValue() { return value; }
        public String getCountry() { return country; }
        public String getCountryCode() { return countryCode; }
        public String getMaterial() { return material; }
    }
    

【问题讨论】:

    标签: android listview android-intent


    【解决方案1】:

    您应该将您的 Coin 对象设为 Parcelable,然后您可以将该对象作为额外的 Intent 传递,

    intent.putExtra("coin", coinObject);
    

    然后你在你的Activity中提取硬币对象,

    Coin coin = intent.getParcelableExtra("coin");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-21
      相关资源
      最近更新 更多