字母顺序不适用于自定义ListView

时间:2018-08-16 12:41:00

标签: java android android-studio listview android-arrayadapter

也许这个话题已经讨论过了,但是我没有找到任何可以帮助我的...。 我有一个带有文本和图像的自定义ListView。

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

,这是活动

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

所有语言都可以正常使用。但是当改变语言系统时,文本改变了顺序(正确),但是图像改变了。 如何将图像列表设置为文本列表? 谢谢...

这是MainActivity布局。

<?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" />

这是strings.xml布局。

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

2 个答案:

答案 0 :(得分:1)

您已经为每种语言的国家/地区名称创建了字符串数组,这些字符串数组最初可能会也可能不会排序,但是图像仅使用一个编码数组。
因此,用英语以外的其他语言表示的国家/地区名称的位置不等于国旗的位置。
一种解决方案是像这样更改您的字符串数组:

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

对于每种语言,您必须具有国家/地区名称和相应的标志。
在您的MainActivity类中,检索此数组并拆分每个项目以获取名称和标志:

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

我希望我没有错字

答案 1 :(得分:0)

您必须命名绘图对象列表imageList,而不是cList
cList是名称的字符串数组的名称。