Java 如何将Android字符串数组加载到ListView?

Java 如何将Android字符串数组加载到ListView?,java,android,arrays,string,listview,Java,Android,Arrays,String,Listview,我正在尝试用Android构建我的第一个ListView,为此我构建了一个自定义ListAdapter。因此,我创建了一个名为NotificationsAdapter的类,它扩展了BaseAdapter。因为我希望能够将字符串翻译成其他语言,所以我在strings.xml中创建了一个字符串数组: <string-array name="notifications_string_array"> <item>The first item</item>

我正在尝试用Android构建我的第一个ListView,为此我构建了一个自定义ListAdapter。因此,我创建了一个名为NotificationsAdapter的类,它扩展了BaseAdapter。因为我希望能够将字符串翻译成其他语言,所以我在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>
但是,这只是字符串数组的idSo第一个问题;如何加载整个阵列?

然后我需要在视图中加载数组,我尝试这样做:

@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]);
}
这是非常错误的,因为“字符串数组”根本不是一个字符串数组,而只是一个指向字符串数组的int。我想这是解决了我的第一个问题的答案

然而,从现在开始,我不知道如何添加一个意图,我可以根据用户单击的项目给出一个.putextra。最后,如何让每一件物品都有自己的标志?我应该将此图标列表存储在哪里,以及如何将其映射到正确的项目


如果有人能启发我,我将不胜感激

这是加载字符串数组的方式:

final String[] theStringArray = getResources().getStringArray(R.array.notifications_string_array);
现在您可以将这个数组传递给适配器类的构造函数并在那里使用它

I want to put a custom icon next to the list item.
您需要一个带有textview和imageview的自定义布局。在
getView
中插入自定义布局,并适当更新textview和imageview

I also want to be able to click every item
您可以为textview和imageview设置侦听器。如果要单击行的侦听器,请使用
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);              

}
});

检查这个啊,谢谢!字符串数组现在可以很好地加载。至于图标。我只是不知道应该将图标列表存储在哪里,以及如何使.putextra依赖于单击的项目。欢迎使用其中的任何指针(或示例代码)。@kramer65图标在哪里?与所有其他图标一样,在我的res/drawable/文件夹中。但我想我需要在某个地方列个清单。除了在一个中心位置定义字符串、图标和putextra之外,还有没有更符合逻辑的方法来完成这项工作?@kramer65检查我的编辑。我不知道你所说的中心位置是什么意思。@kramer65还建议使用一种视窗夹模式
I also want to be able to click every item
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);              

}
});
int [] mydrawable ={R.drawable.icon1,R.drawable.icon2}; // pass this to the constructor of you adapter class