Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Here we are again. I hope this will make it easier how to compare the task to the result. Do you want to know which lesson is that? -> Custom ArrayAdapter Exercise from Udacity, Android Basics Nanodegree Program. Multi Screen Apps -> LESSON 3 Arrays, Lists, Loops, & Custom Classes Use Arrays, Lists, and Loops
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/miwok_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="16dp"
tools:text="luffi"
android:textAppearance="?android:textAppearanceLarge"
android:textColor="#4689C8"
android:textStyle="bold" />
<TextView
android:id="@+id/default_text_view2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="16dp"
tools:text="one"
android:textAppearance="?android:textAppearanceLarge"
android:textColor="#4689C8"
android:textStyle="bold" />
</LinearLayout>
package com.example.android.miwok;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
import androidx.appcompat.app.AppCompatActivity;
public class NumbersActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_numbers);
//create an ArrayList of words
ArrayList<Word> words = new ArrayList<Word>();
//words.add("one");
//Word w = new Word("one","lutti");
//words.add(w);
//or you can add this way word to the list
words.add(new Word("one", "lutti"));
words.add(new Word("two", "otiiko"));
words.add(new Word("three", "tolookosu"));
words.add(new Word("four", "oyyisa"));
words.add(new Word("five", "massokka"));
words.add(new Word("six", "temmokka"));
words.add(new Word("seven", "kenekaku"));
words.add(new Word("eight", "kawinta"));
words.add(new Word("nine", "wo’e"));
words.add(new Word("ten", "na’aacha"));
// Create an {@link ArrayAdapter}, whose data source is a list of Strings. The
// adapter knows how to create layouts for each item in the list, using the
// simple_list_item_1.xml layout resource defined in the Android framework.
// This list item layout contains a single {@link TextView}, which the adapter will set to
// display a single word.
WordAdapter adapter = new WordAdapter(this, words);
// Find the {@link ListView} object in the view hierarchy of the {@link Activity}.
// There should be a {@link ListView} with the view ID called list, which is declared in the
// activity_numbers.xml layout file.
ListView listView = findViewById(R.id.list);
// Make the {@link ListView} use the {@link ArrayAdapter} we created above, so that the
// {@link ListView} will display list items for each word in the list of words.
// Do this by calling the setAdapter method on the {@link ListView} object and pass in
// 1 argument, which is the {@link ArrayAdapter} with the variable name itemsAdapter.
listView.setAdapter(adapter);
}
}
package com.example.android.miwok;
/**
* {@link Word} represents a vocabulary word that the user wants to learn.
* It contains a default translation and a Miwok translation for that word.
*/
public class Word {
/** Default translation for the word */
private String mDefaultTranslation;
/** Miwok translation for the word */
private String mMiwokTranslation;
/**
* Create a new Word object.
*
* @param defaultTranslation is the word in a language that the user is already familiar with
* (such as English)
* @param miwokTranslation is the word in the Miwok language
*/
public Word(String defaultTranslation, String miwokTranslation) {
mDefaultTranslation = defaultTranslation;
mMiwokTranslation = miwokTranslation;
}
/**
* Get the default translation of the word.
*/
public String getDefaultTranslation() {
return mDefaultTranslation;
}
/**
* Get the Miwok translation of the word.
*/
public String getMiwokTranslation() {
return mMiwokTranslation;
}
}
package com.example.android.miwok;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
public class WordAdapter extends ArrayAdapter<Word> {
private static final String LOG_TAG = WordAdapter.class.getSimpleName();
public WordAdapter(Context context, ArrayList<Word> pwords) {
super(context, 0, pwords);
}
/**
* Provides a view for an AdapterView (ListView, GridView, etc.)
*
* @param position The position in the list of data that should be displayed in the
* list item view.
* @param convertView The recycled view to populate.
* @param parent The parent ViewGroup that is used for inflation.
* @return The View for the position in the AdapterView.
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Check if the existing view is being reused, otherwise inflate the view
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
// Get the {@link Word} object located at this position in the list
Word local_word = getItem(position);
// Find the TextView in the list_item.xml layout with the ID version_name
TextView miwokTextView = (TextView) listItemView.findViewById(R.id.miwok_text_view);
// Get the version name from the current Word object and
// set this text on the name TextView
miwokTextView.setText(local_word.getMiwokTranslation());
// Find the TextView in the list_item.xml layout with the ID version_number
TextView defaultTextView = (TextView) listItemView.findViewById(R.id.default_text_view2);
// Get the version name from the current Word object and
// set this text on the name TextView
defaultTextView.setText(local_word.getDefaultTranslation());
// Return the whole list item layout (containing 2 TextViews and an ImageView)
// so that it can be shown in the ListView
return listItemView;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.