kramer65
kramer65

Reputation: 54013

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. So I created a class called NotificationsAdapter which extends the BaseAdapter. Since I want to be able to translate the strings into other languages, I created a string-array in 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. 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:

int theStringArray = R.array.notifications_string_array;

This is however, just the id of the string array. 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. 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. 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!

Upvotes: 0

Views: 1133

Answers (2)

Raghunandan
Raghunandan

Reputation: 133570

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. Inlflate the custom layout in getView and update textview and imageview appropriately.

I also want to be able to click every item

You can set listeners for textview and imageview. If you want to click listener for rows then use 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

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

Upvotes: 1

Blacklight
Blacklight

Reputation: 3827

This is how you load a String-array:

final String[] theStringArray = getResources().getStringArray(R.array.notifications_string_array);

Upvotes: 1

Related Questions