2017-02-24 60 views
-1

我想創建一個包含列表的選項卡式活動。我創建了Tabbed Activity,我想在片段中創建列表。每個列表項目包含2個ImageViews和4個TextViews。因此,我創建了一個自定義類和一個自定義ArrayAdapter。這是代碼 -ArrayAdapter顯示錯誤:選項卡式活動和ListView

  1. Coins.java - 這是選項卡式活動的片段。

    package com.example.android.cotescol; 
    import android.os.Bundle; 
    import android.view.LayoutInflater; 
    import android.view.View; 
    import android.view.ViewGroup; 
    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<Currency>(); 
    
         coins.add(new CoinObject(1, "India", 1947)); 
         coins.add(new CoinObject(1, "India", 1947)); 
    
         CoinAdapter itemsAdapter = new CoinAdapter(this, coins); 
         ListView listView = (ListView) findViewById(R.id.list); 
         listView.setAdapter(itemsAdapter); 
    
         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; } 
    } 
    
  3. CoinAdapter.java - 這是ArrayAdapter

    package com.example.android.cotescol; 
    
    import android.content.Context; 
    import android.support.annotation.NonNull; 
    import android.view.LayoutInflater; 
    import android.view.View; 
    import android.view.ViewGroup; 
    import android.widget.ArrayAdapter; 
    import android.widget.ImageView; 
    import android.widget.TextView; 
    
    import java.util.ArrayList; 
    
    public class CoinAdapter extends ArrayAdapter<CoinObject>{ 
        public CoinAdapter(Context context, ArrayList<CoinObject> currencies) { 
         super(context, 0, currencies); 
        } 
    
        @NonNull 
        @Override 
        public View getView(int position, View convertView, ViewGroup parent) { 
         View listItemView = convertView; 
         if (listItemView == null) { 
          listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false); 
         } 
    
         CoinObject currentCoinObject = getItem(position); 
    
         TextView denominationTextView = (TextView) listItemView.findViewById(R.id.denomination_text_view); 
         denominationTextView.setText(currentCoinObject.getDenomination()); 
    
         TextView countryTextView = (TextView) listItemView.findViewById(R.id.country_text_view); 
         countryTextView.setText(currentCoinObject.getCountry() + "(" + currentCoinObject.getCountryCode() + ")"); 
    
         TextView yearTextView = (TextView) listItemView.findViewById(R.id.year_text_view); 
         yearTextView.setText(currentCoinObject.getYear()); 
    
         ImageView obverseImageView = (ImageView) listItemView.findViewById(R.id.obverse_image_view); 
         obverseImageView.setImageResource(currentCoinObject.getObverseImageResourceId()); 
    
         ImageView reverseImageView = (ImageView) listItemView.findViewById(R.id.reverse_image_view); 
         reverseImageView.setImageResource(currentCoinObject.getReverseImageResourceId()); 
    
         return listItemView; 
        } 
    } 
    

在Coins.java文件的自定義,我在創建CoinAdapter類的實例得到一個錯誤。此外,findViewById()以紅色顯示。 Error Picture

如果有人指導我以正確的方式做,這將是非常有益的。提前致謝。

回答

0

替換:

CoinAdapter itemsAdapter = new CoinAdapter(this, coins); 

有了:

CoinAdapter itemsAdapter = new CoinAdapter(getActivity(), coins); 

替換:

ListView listView = (ListView) findViewById(R.id.list); 

有了:

ListView listView = (ListView) rootView.findViewById(R.id.list); 
相關問題