Skip to content

Instantly share code, notes, and snippets.

Created March 1, 2016 01:08
Show Gist options
  • Save anonymous/e19e1c844cd428eb312f to your computer and use it in GitHub Desktop.
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);
}
}
@driss65
Copy link

driss65 commented Jan 17, 2019

it is work

package com.example.android.miwok;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.example.android.miwok.word;

import java.util.ArrayList;

public class wordsAdapter extends ArrayAdapter {

public wordsAdapter(Context context, ArrayList<word> pWords) {
    super(context,0, pWords);
}

@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);
    }
    word my_word= (word) getItem(position);
    TextView textView = (TextView) listItemView.findViewById(R.id.textView);
    textView.setText(my_word.getMtranslationMiwok());
    TextView textView2 = (TextView) listItemView.findViewById(R.id.textView2);
    textView2.setText(my_word.getMtranslationDefault());

    return listItemView;
    //return super.getView(position, convertView, parent);
}

}

Its working fine,thank you.

@MasterSami1985
Copy link

package com.example.android.miwok;

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

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

public class WordAdapter extends ArrayAdapter {

private Context context;



public WordAdapter(Context context, List<Word> words) {
    super(context, 0, words);

}



@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Word current = getItem(position);
    LayoutInflater layoutInflater = LayoutInflater.from(getContext());
    convertView = layoutInflater.inflate(R.layout.list_item, parent, false);

    ((TextView) convertView.findViewById(R.id.miwok_text_view)).setText(current.getmMiwokTranslation());
    ((TextView) convertView.findViewById(R.id.default_text_view)).setText(current.getmDefaultTranslation());
    return convertView;}

}

its word :)

@LaszloLajosT
Copy link

Hi Guys,

For the people above whose code is not working. This is not the completed code, it's the part of quiz and we need to modify the wordAdapter after which the code will work.

Hi guys,
Read the quote above, please!
This task is just a part of the whole project.

If you already worked ahead and want to compare your code then here is a link. All the files are uploaded where we needed to change our code.

@spurthi-dantu
Copy link

spurthi-dantu commented Jun 10, 2020

I have copied the same code but the app is being crashed whenever I click on numbers. Can u pls help me out ?

@LaszloLajosT
Copy link

I have copied the same code but the app is being crashed whenever I click on numbers. Can u pls help me out ?

Hi,

As I mentioned above, it will not work perfectly yet(Crash) because this step is just one step from two. Next step is in the next chapter. With that code it will run your app.

@Ravinarayanmishra
Copy link

This code can't install the App again because of this error message :--

Error:(59, 32) error: constructor WordAdapter in class WordAdapter cannot be applied to given types;
required: no arguments
found: NumbersActivity,ArrayList
reason: actual and formal argument lists differ in length

What is the wrong with the WordAdapter class created ???!!!!!!!

It may be because the constructor created in the WordAdapter class should be a parameterized constructor with the parameters being context and the ArrayList

@Ravinarayanmishra
Copy link

I have copied the same code but the app is being crashed whenever I click on numbers. Can u pls help me out ?

It's because in WordAdapter class, the overridden method getView() should return convertView only insted of super.getView(position, convertView, parent);

@Islam3li
Copy link

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;
}

}`

@qaisHere
Copy link

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);

@simarjeet30
Copy link

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'

@YazanMunirA
Copy link

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

@JuniiiSays
Copy link

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;
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment