Skip to content

Instantly share code, notes, and snippets.

Created March 1, 2016 01:08
Show Gist options
  • Star 21 You must be signed in to star a gist
  • Fork 18 You must be signed in to fork a gist
  • 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);
}
}
@abdullah-alialdin
Copy link

Finally, I got it

package com.example.android.miwok;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.ArrayList;

/**

  • Created by Abdullah on 14/03/2018.

  • {@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 WordAdapter} objects.

  • */
    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 words A List of words objects to display in a list
      */
      WordAdapter(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, 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, View convertView, @nonnull 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 in the list_item.xml layout with the ID miwok_text
      TextView miwokTextView = (TextView) listItemView.findViewById(R.id.miwok_text);
      // Get the miwok word from the current Word object and
      // set this text on the miwok TextView
      assert currentWord != null;
      miwokTextView.setText(currentWord.getMiwokTranslation());

      // Find the TextView in the list_item.xml layout with the ID default_text
      TextView defaultTextView = (TextView) listItemView.findViewById(R.id.default_text);
      // Get the default word from the current Word object and
      // set this text on the default TextView
      defaultTextView.setText(currentWord.getDefaultTranslation());

      // Return the whole list item layout (containing 2 TextViews)
      // so that it can be shown in the ListView
      return listItemView;
      }

}

@MohammedAttar24
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 ???

@sherifhisham
Copy link

it is very hard but i got it in 2 hours

@sherifhisham
Copy link

@MohammedAttar24 ; must be make this in the WordAdapter.Jave ....... Word currentWord = getItem(postion);

   TextView nameTextView = (TextView) listItemView.findViewById(R.id.miwok_text_view);
   nameTextView.setText(currentWord.getMiwokTranslation());

it should be working with you . i spend hour because to work

@louayeldin
Copy link

Done, with little modification of the code

@AarafAwady
Copy link

so i did everything like the lessons and there is no error messeges but when i run the app and click on numberActivity the app crash
please help
WORDADAPTER
`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 java.util.ArrayList;

/**

  • Created by moj on 24/03/2018.
    */

public class WordAdapter extends ArrayAdapter {

public WordAdapter(Context context, ArrayList<Word> Words) {
    super(context,0, Words);
}





@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 local_word = getItem(position);
    TextView miwokTextView = (TextView) listItemView.findViewById(R.id.miwokNum);
    miwokTextView.setText(local_word.getMiwakTranslation());
    TextView defaultTextView = (TextView) listItemView.findViewById(R.id.engNum);
    defaultTextView.setText(local_word.getDefultTranslation());

    return listItemView;
}

}
NUMBER ACTIVITY 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 Numbers 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);
}

}`

@YoucefAndroid
Copy link

Finally it's work for me
should modified Word Adapter.java

`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 WordAdapter extends ArrayAdapter {

public WordAdapter(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 miwokTextView = (TextView) listItemView.findViewById(R.id.miwok_text_view);
    miwokTextView.setText(my_word.getMiwokTranslation());
    TextView defaultTextView = (TextView) listItemView.findViewById(R.id.default_text_view);
    defaultTextView.setText(my_word.getDefaultTranslation());

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

}`

@mrzaima
Copy link

mrzaima commented Mar 30, 2018

it works now after your help, but when i run it on the emulator it displays the miwok language alone!!!
any clew why?

@ulkuulker
Copy link

ulkuulker commented Apr 3, 2018

I have added these lines of codes. Project works, but when I click the Numbers, project has broken down. I can not see any error on message window. How can I do?

@Hoab
Copy link

Hoab commented Apr 30, 2018

It worked! I barely understand what I did though

@badassboy
Copy link

Please, i need explanation to this.
When am calling the setText() method on the TextView,it does not work.It rather,calls the setmText from the TextView class.What is wrong with my code?
TextView english = (TextView) convertView.findViewById(R.id.english);
english.setmText(currentWord.getmDefaultTranslation).
I WANT THIS RATHER.
english.setmText(currentWord.getmDefaultTranslation);

@AbdallahShaqrah
Copy link

LayoutInflater doesn't work with me , it's colored red and says cannot resolve symbol LayoutInflater , how can i fix it

@hemanth9999
Copy link

Cannot symbol "R" guys it was showing error on my java code.
what can i do?

@kareemofthecrop
Copy link

kareemofthecrop commented Jul 5, 2018

How come pointed brackets aren't used in:
WordAdapter adapter = new WordAdapter(this, words);

compared to when we used:
ArrayAdapter< String > words = new ArrayAdapter<>;

@Shubham6059
Copy link

@kareemofthecrop because WordAdapter is itself a class which we created as WordAdpater.java, not WordAdapter<Word>.java instead, we specified a super class ArrayAdapter<Word> by extends keyword as:

public class WordAdapter extends ArrayAdapter<Word> {...}

This extends WordAdapter functionality but still as we're creating the instance for WordAdapter we use:
WordAdapter adapter = new WordAdapter(this, words);

Hope this solves your query.

@AHMED7GHONEIM
Copy link

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

}

@trustidkid
Copy link

The code under WordAdapter.java should like this. It works.

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 java.util.ArrayList;

public class WordAdapter extends ArrayAdapter {

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_view);
    // 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;
}

}

@anirudh135
Copy link

i have a doubt ,
the getview method in WordAdapter.java is never called how does that method get executed.

@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