- 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/
我需要将文本放在 中在一个 Div 中,在另一个 Div 中,在另一个 Div 中。所以这是它的样子: #document Change PIN
奇怪的事情发生了。 我有一个基本的 html 代码。 html,头部, body 。(因为我收到了一些反对票,这里是完整的代码) 这是我的CSS: html { backgroun
我正在尝试将 Assets 中的一组图像加载到 UICollectionview 中存在的 ImageView 中,但每当我运行应用程序时它都会显示错误。而且也没有显示图像。 我在ViewDidLoa
我需要根据带参数的 perl 脚本的输出更改一些环境变量。在 tcsh 中,我可以使用别名命令来评估 perl 脚本的输出。 tcsh: alias setsdk 'eval `/localhome/
我使用 Windows 身份验证创建了一个新的 Blazor(服务器端)应用程序,并使用 IIS Express 运行它。它将显示一条消息“Hello Domain\User!”来自右上方的以下 Ra
这是我的方法 void login(Event event);我想知道 Kotlin 中应该如何 最佳答案 在 Kotlin 中通配符运算符是 * 。它指示编译器它是未知的,但一旦知道,就不会有其他类
看下面的代码 for story in book if story.title.length < 140 - var story
我正在尝试用 C 语言学习字符串处理。我写了一个程序,它存储了一些音乐轨道,并帮助用户检查他/她想到的歌曲是否存在于存储的轨道中。这是通过要求用户输入一串字符来完成的。然后程序使用 strstr()
我正在学习 sscanf 并遇到如下格式字符串: sscanf("%[^:]:%[^*=]%*[*=]%n",a,b,&c); 我理解 %[^:] 部分意味着扫描直到遇到 ':' 并将其分配给 a。:
def char_check(x,y): if (str(x) in y or x.find(y) > -1) or (str(y) in x or y.find(x) > -1):
我有一种情况,我想将文本文件中的现有行包含到一个新 block 中。 line 1 line 2 line in block line 3 line 4 应该变成 line 1 line 2 line
我有一个新项目,我正在尝试设置 Django 调试工具栏。首先,我尝试了快速设置,它只涉及将 'debug_toolbar' 添加到我的已安装应用程序列表中。有了这个,当我转到我的根 URL 时,调试
在 Matlab 中,如果我有一个函数 f,例如签名是 f(a,b,c),我可以创建一个只有一个变量 b 的函数,它将使用固定的 a=a1 和 c=c1 调用 f: g = @(b) f(a1, b,
我不明白为什么 ForEach 中的元素之间有多余的垂直间距在 VStack 里面在 ScrollView 里面使用 GeometryReader 时渲染自定义水平分隔线。 Scrol
我想知道,是否有关于何时使用 session 和 cookie 的指南或最佳实践? 什么应该和什么不应该存储在其中?谢谢! 最佳答案 这些文档很好地了解了 session cookie 的安全问题以及
我在 scipy/numpy 中有一个 Nx3 矩阵,我想用它制作一个 3 维条形图,其中 X 轴和 Y 轴由矩阵的第一列和第二列的值、高度确定每个条形的 是矩阵中的第三列,条形的数量由 N 确定。
假设我用两种不同的方式初始化信号量 sem_init(&randomsem,0,1) sem_init(&randomsem,0,0) 现在, sem_wait(&randomsem) 在这两种情况下
我怀疑该值如何存储在“WORD”中,因为 PStr 包含实际输出。? 既然Pstr中存储的是小写到大写的字母,那么在printf中如何将其给出为“WORD”。有人可以吗?解释一下? #include
我有一个 3x3 数组: var my_array = [[0,1,2], [3,4,5], [6,7,8]]; 并想获得它的第一个 2
我意识到您可以使用如下方式轻松检查焦点: var hasFocus = true; $(window).blur(function(){ hasFocus = false; }); $(win
我是一名优秀的程序员,十分优秀!