[source code] Android Development Tutorial - 47 & 48

+11 Bucky Roberts · January 3, 2015
MainActivity.java
package com.thenewboston.listexample;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {

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

        String[] foods = {"Bacon", "Ham", "Tuna", "Candy", "Meatball", "Potato"};
        ListAdapter buckysAdapter = new CustomAdapter(this, foods);
        ListView buckysListView = (ListView) findViewById(R.id.buckysListView);
        buckysListView.setAdapter(buckysAdapter);

        buckysListView.setOnItemClickListener(
                new AdapterView.OnItemClickListener(){
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        String food = String.valueOf(parent.getItemAtPosition(position));
                        Toast.makeText(MainActivity.this, food, Toast.LENGTH_LONG).show();
                    }
                }
        );
    }

}


CustomAdapter.java
package com.thenewboston.listexample;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

class CustomAdapter extends ArrayAdapter<String> {

    CustomAdapter(Context context, String[] foods) {
        super(context, R.layout.custom_row ,foods);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater buckysInflater = LayoutInflater.from(getContext());
        View customView = buckysInflater.inflate(R.layout.custom_row, parent, false);

        String singleFoodItem = getItem(position);
        TextView buckysText = (TextView) customView.findViewById(R.id.buckysText);
        ImageView buckysImage = (ImageView) customView.findViewById(R.id.buckysImage);

        buckysText.setText(singleFoodItem);
        buckysImage.setImageResource(R.drawable.chunky);
        return customView;
    }
}


activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/buckysListView"></ListView>

</RelativeLayout>


custom_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:id="@+id/buckysImage"
        android:src="@drawable/chunky"
        android:layout_margin="5dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/buckysText"
        android:layout_margin="5dp" />
</LinearLayout>

Post a Reply

Replies

Oldest  Newest  Rating
+2 Sam Foster · March 6, 2015
How do you do the same but for the Image?
+1 Emad Ahmed · March 7, 2015
Bucky I need Source code for 27,28,29 (Danke Meme Bro!)
+4 Roman Smirnov · March 8, 2015
Bucky can you please use github for the source? That would be much more organized as well as teach people something useful
+1 nash floros · August 3, 2015
I have the same question with Sam Foster. How can we have different images? thx.
0 Juan C Galvez · October 14, 2015
Hi Bucky.

I have learned a lot with your excellent video tutorials.

When testing with this tutorial I noticed than when I scroll up and down and repeat it several times the memory usage increases endlessly until the app fails

Could you please check it and explain why this is happening.
0 Maki Lew · March 23, 2016
How do you change view for multiple images so that it doesn't display only bucky. Do I need to create array that will go through images like food? How do I achieve the same thing but pulling images and names from xml file.
0 Hanna Hoffmann · March 30, 2016
Hi,
Is there a thread for all those wonderful tutorials`source codes or do I have to really code along? ;-)
Tx Hanna
  • 1

Java / Android Development

107,173 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator