- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在 ListViewAdapter 中创建超过 1500 个 LyricsFullDetail,但我的问题是我不想在 ListViewAdapter.java 中硬编码 Lyrics Full Detail。我想存储歌词的完整详细信息并从 Strings.xml 或 Assets 文件夹中读取。音乐文件保存在 raw 文件夹中。如果您帮助我修复示例,我将非常感激。谢谢以下是我的代码。
这是我重复了 1500 次的 itemClick 代码,这给了我“代码太大错误”
这就是为什么我努力将其保存在 Assets 文件夹之类的地方并在 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));
字符串.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>
关于java - 如何在 strings.xml 或 asset 中存储数组并在 ListViewAdapter 中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60234808/
我正在尝试创建一个包含 int[][] 项的数组 即 int version0Indexes[][4] = { {1,2,3,4}, {5,6,7,8} }; int version1Indexes[
我有一个整数数组: private int array[]; 如果我还有一个名为 add 的方法,那么以下有什么区别: public void add(int value) { array[va
当您尝试在 JavaScript 中将一个数组添加到另一个数组时,它会将其转换为一个字符串。通常,当以另一种语言执行此操作时,列表会合并。 JavaScript [1, 2] + [3, 4] = "
根据我正在阅读的教程,如果您想创建一个包含 5 列和 3 行的表格来表示这样的数据... 45 4 34 99 56 3 23 99 43 2 1 1 0 43 67 ...它说你可以使用下
我通常使用 python 编写脚本/程序,但最近开始使用 JavaScript 进行编程,并且在使用数组时遇到了一些问题。 在 python 中,当我创建一个数组并使用 for x in y 时,我得
我有一个这样的数组: temp = [ 'data1', ['data1_a','data1_b'], ['data2_a','data2_b','data2_c'] ]; // 我想使用 toStr
rent_property (table name) id fullName propertyName 1 A House Name1 2 B
这个问题在这里已经有了答案: 关闭13年前。 Possible Duplicate: In C arrays why is this true? a[5] == 5[a] array[index] 和
使用 Excel 2013。经过多年的寻找和适应,我的第一篇文章。 我正在尝试将当前 App 用户(即“John Smith”)与他的电子邮件地址“jsmith@work.com”进行匹配。 使用两个
当仅在一个边距上操作时,apply 似乎不会重新组装 3D 数组。考虑: arr 1),但对我来说仍然很奇怪,如果一个函数返回一个具有尺寸的对象,那么它们基本上会被忽略。 最佳答案 这是一个不太理
我有一个包含 GPS 坐标的 MySQL 数据库。这是我检索坐标的部分 PHP 代码; $sql = "SELECT lat, lon FROM gps_data"; $stmt=$db->query
我需要找到一种方法来执行这个操作,我有一个形状数组 [批量大小, 150, 1] 代表 batch_size 整数序列,每个序列有 150 个元素长,但在每个序列中都有很多添加的零,以使所有序列具有相
我必须通过 url 中的 json 获取文本。 层次结构如下: 对象>数组>对象>数组>对象。 我想用这段代码获取文本。但是我收到错误 :org.json.JSONException: No valu
enter code here- (void)viewDidLoad { NSMutableArray *imageViewArray= [[NSMutableArray alloc] init];
知道如何对二维字符串数组执行修剪操作,例如使用 Java 流 API 进行 3x3 并将其收集回相同维度的 3x3 数组? 重点是避免使用显式的 for 循环。 当前的解决方案只是简单地执行一个 fo
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我有来自 ASP.NET Web 服务的以下 XML 输出: 1710 1711 1712 1713
如果我有一个对象todo作为您状态的一部分,并且该对象包含数组列表,则列表内部有对象,在这些对象内部还有另一个数组listItems。如何更新数组 listItems 中 id 为“poi098”的对
我想将最大长度为 8 的 bool 数组打包成一个字节,通过网络发送它,然后将其解压回 bool 数组。已经在这里尝试了一些解决方案,但没有用。我正在使用单声道。 我制作了 BitArray,然后尝试
我们的数据库中有这个字段指示一周中的每一天的真/假标志,如下所示:'1111110' 我需要将此值转换为 boolean 数组。 为此,我编写了以下代码: char[] freqs = weekday
我是一名优秀的程序员,十分优秀!