如何在 strings.xml 或资产中存储数组并在 ListViewAdapter 中使用

How to store arrays in strings.xml or assets and use in ListViewAdapter

我想在 ListViewAdapter 中创建超过 1500 个 LyricsFullDetail,但我的问题是我不想在 ListViewAdapter.java 中硬编码 Lyrics Full Detail。我想存储歌词的全部细节并从 Strings.xml 或资产文件夹中读取。音乐文件保存在原始文件夹中。 如果你帮我举个例子,我将不胜感激fix.thanks下面是我的代码。

这是我重复了 1500 次的 itemClick 代码,它给了我 'code too large error'

这就是为什么我努力将它保存在资产文件夹之类的地方并在 ListViewAdapter

中读取的原因

 //listview item clicks
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //code later
                if (modellist.get(i).getTitle().equals("Song 001 | This song lyrics 1")) {
                    //start NewActivity with title for actionbar and text for textview
                    Intent intent = new Intent(mContext, NewActivity.class);
                    intent.putExtra("actionBarTitle", "Song 001");
                    intent.putExtra("position", 1);
                    intent.putExtra("contentTv", "1. O THOU to whose all-searching sight\n" +
                            "The darkness shineth as the light,\n" +
                            "Search, prove my heart, it pants for thee;\n" +
                            "O burst these bonds and set it free!\n" +
                            "\n" +
                            "2. Wash out its stain, refine its dross,\n" +
                            "Nail my affections to the cross:\n" +
                            "Hallow each thought, let all within\n" +
                            "Be clean, as thou, my Lord, art clean.");
                    mContext.startActivity(intent);
                }
                if (modellist.get(i).getTitle().equals("Song 002 | This song lyrics 2")) {
                    //start NewActivity with title for actionbar and text for textview
                    Intent intent = new Intent(mContext, NewActivity.class);
                    intent.putExtra("position", 2);
                    intent.putExtra("actionBarTitle", "Song 002");
                    intent.putExtra("contentTv", "3 Saviour, where’er thy steps I see,\n" +
                            "Dauntless, untired, I’ll follow thee;\n" +
                            "O let thy hand support me still\n" +
                            "And lead me to thy holy hill!\n" +
                            "\n" +
                            "4 If rough and thorny be the way,\n" +
                            "My strength proportion to my day,\n" +
                            "Till toil and grief and pain shall cease,\n" +
                            "Where all is calm and joy and peace.");
                    mContext.startActivity(intent);
                }
                if (modellist.get(i).getTitle().equals("Song 003 | This song lyrics 3")) {
                    //start NewActivity with title for actionbar and text for textview
                    Intent intent = new Intent(mContext, NewActivity.class);
                    intent.putExtra("position", 3);
                    intent.putExtra("actionBarTitle", "Song 003");
                    intent.putExtra("contentTv", "1. O THOU to whose all-searching sight\n" +
                            "The darkness shineth as the light,\n" +
                            "Search, prove my heart, it pants for thee;\n" +
                            "O burst these bonds and set it free!\n" +
                            "\n" +
                            "2. Wash out its stain, refine its dross,\n" +
                            "Nail my affections to the cross:\n" +
                            "Hallow each thought, let all within\n" +
                            "Be clean, as thou, my Lord, art clean.");
                    mContext.startActivity(intent);
                }
                if (modellist.get(i).getTitle().equals("Song 004 | This song lyrics 4")) {
                    //start NewActivity with title for actionbar and text for textview
                    Intent intent = new Intent(mContext, NewActivity.class);
                    intent.putExtra("position", 4);
                    intent.putExtra("actionBarTitle", "Song 004");
                    intent.putExtra("contentTv", "This is song lyrics detail\n\n\n\n");
                    mContext.startActivity(intent);
                }
                if (modellist.get(i).getTitle().equals("Song 005 | This song lyrics 5")) {
                    //start NewActivity with title for actionbar and text for textview
                    Intent intent = new Intent(mContext, NewActivity.class);
                    intent.putExtra("position", 5);
                    intent.putExtra("actionBarTitle", "Song 005");
                    intent.putExtra("contentTv", "This is song lyrics detail");
                    mContext.startActivity(intent);
                }
                if (modellist.get(i).getTitle().equals("Song 006 | This song lyrics 6")) {
                    //start NewActivity with title for actionbar and text for textview
                    Intent intent = new Intent(mContext, NewActivity.class);
                    intent.putExtra("position", 6);
                    intent.putExtra("actionBarTitle", "Song 006");
                    intent.putExtra("contentTv", "This is song lyrics detail");
                    mContext.startActivity(intent);
                }
                if (modellist.get(i).getTitle().equals("Song 007 | This song lyrics 7")) {
                    //start NewActivity with title for actionbar and text for textview
                    Intent intent = new Intent(mContext, NewActivity.class);
                    intent.putExtra("position", 7);
                    intent.putExtra("actionBarTitle", "Song 007");
                    intent.putExtra("contentTv", "This is song lyrics detail");
                    mContext.startActivity(intent);
                }

            }
        });


        return view;
    }

这是ListViewAdapter.java

public class ListViewAdapter extends BaseAdapter {

    //Variables
    Context mContext;
    LayoutInflater inflater;
    List<Model> modellist;
    ArrayList<Model> arrayList;
    int[] soundfile;

    //Constructor
    public ListViewAdapter(Context context, List<Model> modellist) {
        mContext = context;
        this.modellist = modellist;
        inflater = LayoutInflater.from(mContext);
        this.arrayList = new ArrayList<Model>();
        this.arrayList.addAll(modellist);
    }

    public class ViewHolder {
        TextView mTitleTv, mDescTv;
        ImageView mIconTv, favorite;
    }

    @Override
    public int getCount() {
        return modellist.size();
    }

    @Override
    public Object getItem(int i) {
        return modellist.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup parent) {
        final ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = inflater.inflate(R.layout.row, null);

            //locate the views in row.xml
            holder.mTitleTv = (TextView) view.findViewById(R.id.mainTitle);
            holder.mDescTv = (TextView) view.findViewById(R.id.mainDesc);
            holder.mIconTv = view.findViewById(R.id.mainIcon);
            //initialize the favorite image view
            holder.favorite = view.findViewById(R.id.favorite);

            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        //set the result into textview
        holder.mTitleTv.setText(modellist.get(i).getTitle());
        holder.mDescTv.setText(modellist.get(i).getDesc());
        //Set the result in imagview
        holder.mIconTv.setImageResource(modellist.get(i).getIcon());

        if (modellist.get(i).isFavorite())
            holder.favorite.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_heart));
        else
            holder.favorite.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_heart_o));

        //listview soundfile file for songs in position
        soundfile = new int[]{R.raw.song_1, R.raw.song_2, R.raw.song_3, R.raw.song_4, R.raw.song_5, R.raw.song_6, R.raw.song_7, R.raw.song_8,};

        //fovarite/unfavorite

        holder.favorite.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (modellist.get(i).isFavorite())
                    holder.favorite.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_heart_o));
                else
                    holder.favorite.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_heart));

                modellist.get(i).setFavorite();
            }
        });

        //listview item clicks
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //code later
                if (modellist.get(i).getTitle().equals("Song 001 | This song lyrics 1")) {
                    //start NewActivity with title for actionbar and text for textview
                    Intent intent = new Intent(mContext, NewActivity.class);
                    intent.putExtra("actionBarTitle", "Song 001");
                    intent.putExtra("position", 1);
                    intent.putExtra("contentTv", "1. O THOU to whose all-searching sight\n" +
                            "The darkness shineth as the light,\n" +
                            "Search, prove my heart, it pants for thee;\n" +
                            "O burst these bonds and set it free!\n" +
                            "\n" +
                            "2. Wash out its stain, refine its dross,\n" +
                            "Nail my affections to the cross:\n" +
                            "Hallow each thought, let all within\n" +
                            "Be clean, as thou, my Lord, art clean.\n\n\n\n");
                    mContext.startActivity(intent);
                }
                if (modellist.get(i).getTitle().equals("Song 002 | This song lyrics 2")) {
                    //start NewActivity with title for actionbar and text for textview
                    Intent intent = new Intent(mContext, NewActivity.class);
                    intent.putExtra("position", 2);
                    intent.putExtra("actionBarTitle", "Song 002");
                    intent.putExtra("contentTv", "3 Saviour, where’er thy steps I see,\n" +
                            "Dauntless, untired, I’ll follow thee;\n" +
                            "O let thy hand support me still\n" +
                            "And lead me to thy holy hill!\n" +
                            "\n" +
                            "4 If rough and thorny be the way,\n" +
                            "My strength proportion to my day,\n" +
                            "Till toil and grief and pain shall cease,\n" +
                            "Where all is calm and joy and peace.\n\n\n\n");
                    mContext.startActivity(intent);
                }
                if (modellist.get(i).getTitle().equals("Song 003 | This song lyrics 3")) {
                    //start NewActivity with title for actionbar and text for textview
                    Intent intent = new Intent(mContext, NewActivity.class);
                    intent.putExtra("position", 3);
                    intent.putExtra("actionBarTitle", "Song 003");
                    intent.putExtra("contentTv", "1. O THOU to whose all-searching sight\n" +
                            "The darkness shineth as the light,\n" +
                            "Search, prove my heart, it pants for thee;\n" +
                            "O burst these bonds and set it free!\n" +
                            "\n" +
                            "2. Wash out its stain, refine its dross,\n" +
                            "Nail my affections to the cross:\n" +
                            "Hallow each thought, let all within\n" +
                            "Be clean, as thou, my Lord, art clean.\n\n\n\n");
                    mContext.startActivity(intent);
                }
                if (modellist.get(i).getTitle().equals("Song 004 | This song lyrics 4")) {
                    //start NewActivity with title for actionbar and text for textview
                    Intent intent = new Intent(mContext, NewActivity.class);
                    intent.putExtra("position", 4);
                    intent.putExtra("actionBarTitle", "Song 004");
                    intent.putExtra("contentTv", "This is song lyrics detail\n\n\n\n");
                    mContext.startActivity(intent);
                }
                if (modellist.get(i).getTitle().equals("Song 005 | This song lyrics 5")) {
                    //start NewActivity with title for actionbar and text for textview
                    Intent intent = new Intent(mContext, NewActivity.class);
                    intent.putExtra("position", 5);
                    intent.putExtra("actionBarTitle", "Song 005");
                    intent.putExtra("contentTv", "This is song lyrics detail\n\n\n\n");
                    mContext.startActivity(intent);
                }
                if (modellist.get(i).getTitle().equals("Song 006 | This song lyrics 6")) {
                    //start NewActivity with title for actionbar and text for textview
                    Intent intent = new Intent(mContext, NewActivity.class);
                    intent.putExtra("position", 6);
                    intent.putExtra("actionBarTitle", "Song 006");
                    intent.putExtra("contentTv", "This is song lyrics detail\n\n\n\n");
                    mContext.startActivity(intent);
                }
                if (modellist.get(i).getTitle().equals("Song 007 | This song lyrics 7")) {
                    //start NewActivity with title for actionbar and text for textview
                    Intent intent = new Intent(mContext, NewActivity.class);
                    intent.putExtra("position", 7);
                    intent.putExtra("actionBarTitle", "Song 007");
                    intent.putExtra("contentTv", "This is song lyrics detail\n\n\n\n");
                    mContext.startActivity(intent);
                }

            }
        });


        return view;
    }

    //filter
    public void filter(String charText) {
        charText = charText.toLowerCase(Locale.getDefault());
        modellist.clear();
        if (charText.length() == 0) {
            modellist.addAll(arrayList);
        } else {
            for (Model model : arrayList) {
                if (model.getTitle().toLowerCase(Locale.getDefault()).contains(charText)) {
                    modellist.add(model);
                }
            }
        }
        notifyDataSetChanged();
    }


}

这是我保存列表数组的地方MainActivity.java

public class MainActivity extends AppCompatActivity implements SearchView.OnQueryTextListener {
    private String TAG = "MainActivity ----- ; " ;
    // Store instance variables

    private int page;
    private ConsentForm form;

    ListView listView;
    ListViewAdapter adapter;
    String[] title;
    String[] description;
    int[] icon;
    ArrayList<Model> arrayList = new ArrayList<Model>();


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

        //initialize preferences
        PreferenceUtils.initialize(getApplicationContext());

        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle("Redeemed Songs");

        title = new String[]{"Song 001 | This song lyrics 1","Song 002 | This song lyrics 2","Song 003 | This song lyrics 3","Song 004 | This song lyrics 4","Song 005 | This song lyrics 5","Song 006 | This song lyrics 6","Song 007 | This song lyrics 7"};

        description = new String[]{"MORNING song", "MORNING song", "MORNING song", "MORNING song", "MORNING song", "MORNING song", "MORNING song",};

        icon = new int[]{ R.drawable.song, R.drawable.song, R.drawable.song, R.drawable.song,R.drawable.song,R.drawable.song, R.drawable.song,};

        listView = findViewById(R.id.list);

        for (int i =0; i<title.length; i++){
            Model model =new Model(title[i], description[i], icon[i]);
            //let's simply say, the song identifier is just the order of the song in the list
            model.id = i;
            //bind all strings in an array
            arrayList.add(model);
        }

        //pass result to listview class
        adapter = new ListViewAdapter(this, arrayList);

        //bind the adapter to the listview class
        listView.setAdapter(adapter);



    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);

        MenuItem myActionMenuItem = menu.findItem(R.id.action_search);
        SearchView searchView = (SearchView)myActionMenuItem.getActionView();
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String s) {
                if (TextUtils.isEmpty(s)){
                    adapter.filter("");
                    listView.clearTextFilter();
                }
                else {
                    adapter.filter(s);
                }
                return true;
            }
        });

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id==R.id.action_settings){
            Toast.makeText(this, "Settings Selected", Toast.LENGTH_SHORT).show();
            startActivity(new Intent(this, Main2Activity.class));
            return true;
            //do your funtionality here

        }
        else if (id==R.id.action_howtouse){
                Toast.makeText(this, "How-To", Toast.LENGTH_SHORT).show();
            startActivity(new Intent(this, FavoriteListActivity.class));
            return true;
                //do your funtionality here


        }else if (id==R.id.action_favorites){
                Toast.makeText(this, "Favorites Selected", Toast.LENGTH_SHORT).show();
            startActivity(new Intent(this, FavoriteListActivity.class));
            return true;
                //do your funtionality here


        }
        else if (id==R.id.action_developers){
            Toast.makeText(this, "About", Toast.LENGTH_SHORT).show();
            startActivity(new Intent(this, Main4Activity.class));
            return true;
            //do your funtionality here

        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onQueryTextSubmit(String query) {
        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        return false;
    }
}

您可以从 strings.xml 中的字符串数组创建一个列表,然后可以在适配器中使用它,您可以有一个并行数组来将歌曲标题与歌词匹配

但您最好直接使用 SQLite 数据库或使用 Android Rooms 处理此类内容。

房间训练material(SQLite 之上的一层)https://developer.android.com/training/data-storage/room/

或更直接https://developer.android.com/training/data-storage/sqlite.html

List<String> myLyricList = Arrays.asList(getResources().getStringArray(R.array.array_lyrics));

List<String> myTitleList = Arrays.asList(getResources().getStringArray(R.array.array_titles));

strings.xml

<string-array name="array_lyrics">
        <item>This song lyrics 1</item>
        <item>This song lyrics 2</item>
</string-array>

<string-array name="array_titles">
        <item>Song 001</item>
        <item>Song 002</item>
</string-array>