Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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!

share|improve this question
    
check this thread –  El_Mochiq Jan 14 at 16:58
add comment

2 Answers

This is how you load a String-array:

final String[] theStringArray = getResources().getStringArray(R.array.notifications_string_array);
share|improve this answer
add comment
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
share|improve this answer
    
Ah, thanks! The string array now loads perfectly well. As for the icon. I just don't know where I should store the list of icons and how I can make the .putextra dependent on the item which is clicked. Any pointers (or example code) in this would be very welcome. –  kramer65 Jan 14 at 17:04
    
@kramer65 where do you have the icons? –  Raghunandan Jan 14 at 17:05
    
in my res/drawable/ folder, like all the other icons. But I guess I need to make a list somewhere. Isn't there a more logical way of doing this, instead of defining the strings, icons, and putextras in one central place? –  kramer65 Jan 14 at 17:07
    
@kramer65 check my edit. I don't know what you mean by central place. –  Raghunandan Jan 14 at 17:08
    
@kramer65 also recommend using a viewholder pattern developer.android.com/training/improving-layouts/… –  Raghunandan Jan 14 at 17:12
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.