Ronny Westwood
Ronny Westwood

Reputation: 43

Alphabetical order does not work for custom ListView

Maybe this topic has already been discussed but I did not find anything that can help me.... I've a custom ListView with Text and Image.

this is myAdapter..

public class MyAdapter extends ArrayAdapter<String>
{
private String [] country;
private int [] flag;
private Context mContext;

MyAdapter(@NonNull Context context, String[] countryNames, int[] flagImages)
{
    super(context, R.layout.listview_item);
    this.country = countryNames;
    this.flag = flagImages;
    this.mContext = context;
}

@Override
public int getCount()
{
    return names.length;
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent)
{
    ViewHolder mViewHolder = new ViewHolder();

    if(convertView == null)
    {
        LayoutInflater mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        assert mInflater != null;
        convertView = mInflater.inflate(R.layout.listview_item, parent, false);

        mViewHolder.mImage = convertView.findViewById(R.id.imageView);
        mViewHolder.mText = convertView.findViewById(R.id.textView);
        convertView.setTag(mViewHolder);
    } else {
        mViewHolder = (ViewHolder)convertView.getTag();
    }
        mViewHolder.mImage.setImageResource(flag[position]);
        mViewHolder.mText.setText(country[position]);

        return convertView;
}

static class ViewHolder
{
    ImageView mImage;
    TextView mText;
}
}

and this the activity..

public class MainActivity extends AppCompatActivity
{
ListView mListView;

int [] flagImages = {R.drawable.a1, R.drawable.a2, R.drawable.a3, R.drawable.a4, R.drawable.a5, R.drawable.a6, R.drawable.a7, R.drawable.a8, R.drawable.a9, R.drawable.a10, R.drawable.a11, R.drawable.a12};

String [] countryNames;

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

    countryNames = getResources().getStringArray(R.array.cList);

    mListView = findViewById(R.id.listview);

    final MyAdapter myAdapter = new MyAdapter(MainActivity.this, countryNames, flagImages);

    mListView.setAdapter(myAdapter);

    Array.sort(countryNames);

    mListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
        {
            Intent mIntent = new Intent(MainActivity.this, SecondActivity.class);
            mIntent.putExtra("countryName", countryNames[i]);
            mIntent.putExtra("flagImage", flagImages[i]);
            startActivity(mIntent);
        }
    });
}
}

all works fine in the standard language. but when change the language system the text change the order (rightly) but the images no. How to set the images list to text list? thanks...

this is the MainActivity layout..

<?xml version="1.0" encoding="utf-8"?> 

xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="ronnydev.testapp.MainActivity">

<ListView
    android:id="@+id/listview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/black"
    android:divider="@android:drawable/screen_background_light_transparent"
    android:scrollbars="none" />

and this the strings.xml layout..

<?xml version="1.0" encoding="utf-8"?>

<string-array name="cList">
    <item>Denmark</item>
    <item>Finland</item>
    <item>France</item>
    <item>Germany</item>
    <item>Italy</item>
    <item>Poland</item>
    <item>Portugal</item>
    <item>Romania</item>
    <item>Slovenia</item>
    <item>Spain</item>
    <item>Sweden</item>
    <item>United Kingdom</item>
</string-array>

Upvotes: 0

Views: 80

Answers (2)

user8959091
user8959091

Reputation:

You must name the list of drawables imageList and not cList
cList is the name of the string-array of the names.

Upvotes: 0

user8959091
user8959091

Reputation:

You have created string-arrays for the names of the countries for each language, which may or may not be initially sorted but you are using only 1 coded array for the images.
So the position of a country name in another language than English is not equal to the position of the flag.
One solution would be to alter your string-arrays like this:

<string-array name="cList">
    <item>Denmark;a1</item>
    <item>Finland;a2</item>
    <item>France;a3</item>
    <item>Germany;a4</item>
    <item>Italy;a5</item>
    <item>Poland;a6</item>
    <item>Portugal;a7</item>
    <item>Romania;a8</item>
    <item>Slovenia;a9</item>
    <item>Spain;a10</item>
    <item>Sweden;a11</item>
    <item>United Kingdom;a12</item>
</string-array>

For each language you must have the country name and the corresponding flag.
In your MainActivity class you retrieve this array and split each item to get the name and the flag:

public class MainActivity extends AppCompatActivity {
    ListView mListView;

    String [] countries;
    String [] countryNames;
    int [] flagImages;

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

        countries = getResources().getStringArray(R.array.cList);
        Arrays.sort(countries);
        countryNames = new String[countries.length];
        flagImages = new int[countries.length];

        for (int i = 0; i < countries.length; i++) {
            String[] array = countries[i].split(";");
            countryNames[i] = array[0];
            flagImages[i] = getResources().getIdentifier(array[1], "drawable", getPackageName());
        }

        mListView = findViewById(R.id.listview);

        final MyAdapter myAdapter = new MyAdapter(MainActivity.this, countryNames, flagImages);

        mListView.setAdapter(myAdapter);

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
            {
                Intent mIntent = new Intent(MainActivity.this, SecondActivity.class);
                mIntent.putExtra("countryName", countryNames[i]);
                mIntent.putExtra("flagImage", flagImages[i]);
                startActivity(mIntent);
            }
        });
    }
}

I hope I have no typos

Upvotes: 1

Related Questions