Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
2.4 kB
0
Indexable
Never
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        start();
    }

    void start() {
        String[] valutes = {"RUB", "ZAR", "SGD", "TRY", "KRW", "RMB", "JPY"};
        String[] descriptions = {"Russian Ruble", "South African Rand", "Singapore Dollar", "Turkish Lira", "South Korean Won", "Chinese Yuan", "Japanese Yen"};
        String[] capitals = {"Moscow", "Pretoria", "Singapore", "Ankara", "Seoul", "Beijing", "Tokyo"};
        int[] images = {R.drawable.rub, R.drawable.zar, R.drawable.sgd, R.drawable.try, R.drawable.krw, R.drawable.rmb, R.drawable.jpy};

        ListView lvMain = findViewById(R.id.lvMain);
        CustomAdapter adapter = new CustomAdapter(this, valutes, descriptions, capitals, images);
        lvMain.setAdapter(adapter);
    }

    class CustomAdapter extends ArrayAdapter<String> {
        private final MainActivity context;
        private final String[] valutes;
        private final String[] descriptions;
        private final String[] capitals;
        private final int[] images;

        public CustomAdapter(MainActivity context, String[] valutes, String[] descriptions, String[] capitals, int[] images) {
            super(context, R.layout.list_item_custom, valutes);
            this.context = context;
            this.valutes = valutes;
            this.descriptions = descriptions;
            this.capitals = capitals;
            this.images = images;
        }

        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            LayoutInflater inflater = context.getLayoutInflater();
            View rowView = inflater.inflate(R.layout.list_item_custom, null, true);

            TextView currencyName = rowView.findViewById(R.id.currencyName);
            TextView description = rowView.findViewById(R.id.description);
            TextView capital = rowView.findViewById(R.id.capital);
            ImageView imageView = rowView.findViewById(R.id.imageView);

            currencyName.setText(valutes[position]);
            description.setText(descriptions[position]);
            capital.setText(capitals[position]);
            imageView.setImageResource(images[position]);

            return rowView;
        }
    }
}
Leave a Comment