[英]How to load Android strings array into ListView?
I'm trying to build my very first ListView in Android, and for this I'm building a custom ListAdapter. 我正在尝试在Android中构建我的第一个ListView,为此我正在构建一个自定义的ListAdapter。 So I created a class called NotificationsAdapter which extends the BaseAdapter. 所以我创建了一个名为NotificationsAdapter的类,它扩展了BaseAdapter。 Since I want to be able to translate the strings into other languages, I created a string-array in strings.xml: 由于我希望能够将字符串翻译成其他语言,因此我在strings.xml中创建了一个字符串数组:
<string-array name="notifications_string_array">
<item>The first item</item>
<item>The second item</item>
<item>Number three</item>
<item>Numero four</item>
</string-array>
Next to dislaying the items in this list, I also want to be able to click every item. 在放弃此列表中的项目旁边,我还希望能够单击每个项目。 This should create an intent to get the user to the next screen (always the same) with which I have to give a putextra so that I know in the next activity which item the user clicked. 这应该创建一个意图让用户进入下一个屏幕(总是相同的),我必须给它一个putextra,以便我在下一个活动中知道用户点击了哪个项目。 Furthermore, I want to put a custom icon next to the list item. 此外,我想在列表项旁边放置一个自定义图标。
In my Adapter I've got a constructor in which I load the notifications_string_array as follows: 在我的适配器中,我有一个构造函数,我在其中加载notifications_string_array,如下所示:
int theStringArray = R.array.notifications_string_array;
This is however, just the id of the string array. 但是,这只是字符串数组的id。 So first question; 所以第一个问题; how do I load the whole array? 如何加载整个数组?
I then need to load the array in the view, which I try to do like so: 然后我需要在视图中加载数组,我尝试这样做:
@Override
public View getView(final int position, View convertView, ViewGroup viewGroup) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_item_notifications, viewGroup, false);
TextView textView = (TextView) rowView.findViewById(R.id.text_notification);
textView.setText(theStringArray[position]);
}
This goes horribly wrong, because "theStringArray" is not a string array at all, but just an int referring to a string array. 这非常糟糕,因为“theStringArray”根本不是一个字符串数组,而只是一个引用字符串数组的int。 I guess this is solved with an answer to my first question. 我想这是通过回答我的第一个问题来解决的。
From this point on however, I wouldn't know how to add an intent which I can give a .putextra dependent on the item on which the user clicks. 但是从这一点开始,我不知道如何添加一个我可以根据用户点击的项目提供.putextra的意图。 And lastly, how to give every item his own icon? 最后,如何给每个项目提供自己的图标? Where should I store this list of icons and how do I map it to the correct item? 我应该在哪里存储这个图标列表,如何将其映射到正确的项目?
If anybody could enlighten me I would be very grateful! 如果有人能够启发我,我将非常感激!
这是加载String数组的方法:
final String[] theStringArray = getResources().getStringArray(R.array.notifications_string_array);
String[] sarray = getResources().getStringArray(R.array.notifications_string_array);
Now you could pass this array to the constructor of adapter class and use it there 现在您可以将此数组传递给适配器类的构造函数并在那里使用它
I want to put a custom icon next to the list item.
You need a custom layout with a textview and imageview. 您需要具有textview和imageview的自定义布局。 Inlflate the custom layout in getView
and update textview and imageview appropriately. 在getView
隐藏自定义布局并适当更新textview和imageview。
I also want to be able to click every item
You can set listeners for textview and imageview. 您可以为textview和imageview设置侦听器。 If you want to click listener for rows then use setOnItemClickListener
. 如果要单击行的侦听器,请使用setOnItemClickListener
。
listView.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
TextView tv = (TextView)arg1.findViewById(R.id.text_notification);
String text = tv.getText().toString();
Intent i = new Intent(ActivityName.this,SecondActivity.class);
i.putExtra("key",text);
startActivity(i);
}
});
For the drawables 对于drawables
int [] mydrawable ={R.drawable.icon1,R.drawable.icon2}; // pass this to the constructor of you adapter class
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.