-
-
Save anonymous/e19e1c844cd428eb312f to your computer and use it in GitHub Desktop.
package com.example.android.miwok; | |
import android.os.Bundle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.widget.ListView; | |
import java.util.ArrayList; | |
public class NumbersActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_numbers); | |
// Create a list of words | |
ArrayList<Word> words = new ArrayList<Word>(); | |
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 WordAdapter}, whose data source is a list of {@link Word}s. The | |
// adapter knows how to create list items for each item in the list. | |
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 = (ListView) findViewById(R.id.list); | |
// Make the {@link ListView} use the {@link WordAdapter} we created above, so that the | |
// {@link ListView} will display list items for each {@link Word} in the list. | |
listView.setAdapter(adapter); | |
} | |
} |
I have copied the same code but the app is being crashed whenever I click on numbers. Can u pls help me out ?
It is because you might not have made following changes to NumberActivity.java
WordAdapter wordsAdapter = new WordAdapter(this, words);
ListView numberView = (ListView) findViewById(R.id.number_activity);
numberView.setAdapter(wordsAdapter);
its works so fine
`package com.example.android.miwok;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;import androidx.annotation.NonNull;
import androidx.annotation.Nullable;import java.util.ArrayList;
/*
- {@link WordAdapter} is an {@link ArrayAdapter} that can provide the layout for each list
- based on a data source, which is a list of {@link words} objects.
- */
public class WordAdapter extends ArrayAdapter {
/** * This is our own custom constructor (it doesn't mirror a superclass constructor). * The context is used to inflate the layout file, and the list is the data we want * to populate into the lists. * * @param context The current context. Used to inflate the layout file. * @param words A List of AndroidFlavor objects to display in a list */ public WordAdapter(@NonNull Activity context, ArrayList<Word> words) { // Here, we initialize the ArrayAdapter's internal storage for the context and the list. // the second argument is used when the ArrayAdapter is populating a single TextView. // Because this is a custom adapter for two TextViews and an ImageView, the adapter is not // going to use this second argument, so it can be any value. Here, we used 0. super(context, 0, words); } /** * 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. */ @NonNull @Override public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { // Check if the existing view is being reused, otherwise inflate the view View listWordsView = convertView; if (listWordsView == null) { listWordsView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false); } // Get the {@link AndroidFlavor} object located at this position in the list Word currentWord = getItem(position); // Find the TextView in the list_item.xml layout with the ID default_text_view TextView englishWord = listWordsView.findViewById(R.id.default_text_view); // Get the english default word from the current Word object and // set this text on the englishWord TextView englishWord.setText(currentWord.getDefaultTranslation()); // Find the TextView in the list_item.xml layout with the ID version_number TextView miwokWord = listWordsView.findViewById(R.id.miwok_text_view); // Get the version number from the current AndroidFlavor object and // set this text on the number TextView miwokWord.setText(currentWord.getMiwokTranslation());
// we will need it in the future in sha allah ;)
// Find the ImageView in the list_item.xml layout with the ID list_item_icon
// ImageView iconView = (ImageView) listItemView.findViewById(R.id.list_item_icon);
//Get the image resource ID from the current AndroidFlavor object and
//set the image to iconView
// iconView.setImageResource(currentAndroidFlavor.getImageResourceId());return listWordsView; }
}`
This code works fine but with one error in "Word currentWord = (Word) getItem(position); " which is :Incompatible types. Found: 'java.lang.Object', required: 'com.example.miwok.Word' . which can be solved by casting currentWord to 'com.example.miwok.Word'
guys please help here, as im having a problem when implementing the methods "getMiwokTranslation()" and "getDefaultTranslation()", as the program says that it couldn't recognize these two methods ... any idea here ???
WordAdabter.java
package com.example.android.miwok;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.ArrayList;
public class WordAdabter extends ArrayAdapter {
private static final String Number_TAG = WordAdabter.class.getSimpleName();
/**
- This is our own custom constructor (it doesn't mirror a superclass constructor).
- The context is used to inflate the layout file, and the list is the data we want
- to populate into the lists.
*/
public WordAdabter(Activity context, ArrayList words) {
// Here, we initialize the ArrayAdapter's internal storage for the context and the list.
// the second argument is used when the ArrayAdapter is populating a single TextView.
// Because this is a custom adapter for two TextViews and an ImageView, the adapter is not
// going to use this second argument, so it can be any value. Here, we used 0.
super(context, 0, words);
}
@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);
}
// Get the {@link AndroidFlavor} object located at this position in the list
Word currentWordNumber = getItem(position);
// Find the TextView in the list_item.xml layout with the ID version_name
TextView nameTextViewMiwokTranslation = listItemView.findViewById(R.id.miwok_text_view);
// Get the miwok word from the current Word object and
// set this text on the miwok TextView
nameTextViewMiwokTranslation.setText(currentWordNumber.getMiwokTranslation());
// Find the TextView in the list_item.xml layout with the ID version_name
TextView nameTextViewMiwoDefault = listItemView.findViewById(R.id.default_text_view);
// Get the default word from the current Word object and
// set this text on the default TextView
nameTextViewMiwoDefault.setText(currentWordNumber.getDefaultTranslation());
return listItemView;
}
}
Word.Java
package com.example.android.miwok;
import android.content.Context;
import android.widget.TextView;
public class Word{
private String mDefaultTranslation;
private String mMiwokTranslation;
public Word(String defaultTranslation,String miwokTranslation){
mDefaultTranslation = defaultTranslation;
mMiwokTranslation= miwokTranslation;
}
/**
* Get the default Translation of the word
* @return
*/
public String getDefaultTranslation(){
return mDefaultTranslation;
}
/**
* Get miwok translation of the word
* @return
*/
public String getMiwokTranslation(){
return mMiwokTranslation;
}
}
XML list_item
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/default_text_view"
tools:text="One"
/>
NumberActivtiy.Java
package com.example.android.miwok;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.ArrayList;
public class WordAdabter extends ArrayAdapter {
private static final String Number_TAG = WordAdabter.class.getSimpleName();
/**
- This is our own custom constructor (it doesn't mirror a superclass constructor).
- The context is used to inflate the layout file, and the list is the data we want
- to populate into the lists.
*/
public WordAdabter(Activity context, ArrayList words) {
// Here, we initialize the ArrayAdapter's internal storage for the context and the list.
// the second argument is used when the ArrayAdapter is populating a single TextView.
// Because this is a custom adapter for two TextViews and an ImageView, the adapter is not
// going to use this second argument, so it can be any value. Here, we used 0.
super(context, 0, words);
}
@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);
}
// Get the {@link AndroidFlavor} object located at this position in the list
Word currentWordNumber = getItem(position);
// Find the TextView in the list_item.xml layout with the ID version_name
TextView nameTextViewMiwokTranslation = listItemView.findViewById(R.id.miwok_text_view);
// Get the miwok word from the current Word object and
// set this text on the miwok TextView
nameTextViewMiwokTranslation.setText(currentWordNumber.getMiwokTranslation());
// Find the TextView in the list_item.xml layout with the ID version_name
TextView nameTextViewMiwoDefault = listItemView.findViewById(R.id.default_text_view);
// Get the default word from the current Word object and
// set this text on the default TextView
nameTextViewMiwoDefault.setText(currentWordNumber.getDefaultTranslation());
return listItemView;
}
}
So here you can find you Mistake as you know before the code as threaded chain
package com.example.android.miwok;
import android.app.Activity;
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 androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.ArrayList;
public class WordAdapter extends ArrayAdapter {
private static final String LOG_TAG = WordAdapter.class.getSimpleName();
/**
* This is our own custom constructor (it doesn't mirror a superclass constructor).
* The context is used to inflate the layout file, and the list is the data we want
* to populate into the lists.
*
* @param context The current context. Used to inflate the layout file.
* @param Word A List of AndroidFlavor objects to display in a list
*/
public WordAdapter(Activity context, ArrayList<Word> Word) {
// Here, we initialize the ArrayAdapter's internal storage for the context and the list.
// the second argument is used when the ArrayAdapter is populating a single TextView.
// Because this is a custom adapter for two TextViews and an ImageView, the adapter is not
// going to use this second argument, so it can be any value. Here, we used 0.
super(context, 0, Word);
}
/**
* 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 currentWord = getItem(position);
// Find the TextView Miwok Translation in the list_item.xml layout with the ID miwok_text_view
TextView miwokTextView = (TextView) listItemView.findViewById(R.id.miwok_text_view);
// Get the Miwok Word from the current Word object and
// set this text on the Miwok TextView
miwokTextView.setText(currentWord.getMiwokTranslation());
// Find the TextView of Default Translation in the list_item.xml layout with the ID default_text_view
TextView defaultTextView = (TextView) listItemView.findViewById(R.id.default_text_view);
// Get the version number from the current Word object and
// set this text on the Default TextView
defaultTextView.setText(currentWord.getDefaultTranslation());
return listItemView;
}
}
its works so fine
`package com.example.android.miwok;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.ArrayList;
/*
public class WordAdapter extends ArrayAdapter {
// we will need it in the future in sha allah ;)
// Find the ImageView in the list_item.xml layout with the ID list_item_icon
// ImageView iconView = (ImageView) listItemView.findViewById(R.id.list_item_icon);
//Get the image resource ID from the current AndroidFlavor object and
//set the image to iconView
// iconView.setImageResource(currentAndroidFlavor.getImageResourceId());
}`