0

I am new to java and android. I have a 2 dimensional array that contains a number and a name like this:

String[][] array = new String[2][2];

    array[0][0] = "1";
    array[0][1] = "Mickey"";
    array[1][0] = "2";
    array[1][1] = "Mouse";

The array is changing by incoming data via bluetooth - so I can not "hardcode" it. I want to make a ListView which shows just the names but if you click on it, it will give the number (matching to this name) to the to a new activity.

So far i have this which caused me almost more than a day:

mLstModes = (ListView) findViewById(R.id.LstModes);                      
ArrayList<String> arrayList = new ArrayList<String>();

//here I want to "add" also the number... 
arrayList.add(array[0][1]);
arrayList.add(array[1][1]);

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, arrayList);
mLstModes.setAdapter(arrayAdapter);                             
mLstModes.setOnItemClickListener(new OnItemClickListener()
{
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int pos, long id)
    {
        Intent intent = new Intent();
        intent.setClassName(getPackageName(), getPackageName()+ ".NextActivity");
        intent.putExtra("selected", "I WANT THE NUMBER HERE");
        startActivity(intent);
    }
});

The problem is, that I dont know how to add also the number to the arrayList, so that I can easily let the onItemClick method hand over the number of the name.

Sorry if the question is noobish but I hope someone can help me out. Thanks!

Venkatesh S
  • 5,466
  • 1
  • 25
  • 30
  • 2
    Check out custom adapter for ListView. Also, consider using a HashMap instead of ArrayList. See this question http://stackoverflow.com/questions/5234576/what-adapter-shall-i-use-to-use-hashmap-in-a-listview – ono Nov 23 '13 at 03:48
  • you need to create Adapter by extending BaseAdapter class then you can get The Selected item's data by position parameter in onItemClick() – Kirtan Nov 23 '13 at 03:55
  • I searched for custom adapters but didnt find anything that is matching to my problem. I just found solutions for showing a determinated array to a list and create text edits on the next activity. The problem is, that my array is changing on runtime and the numbers dont show like 1,2,3... – user3023973 Nov 25 '13 at 11:05
  • .. they are not chronological and neither continuous. I would be very happy if maybe someone has a more specific help for my problem. – user3023973 Nov 25 '13 at 11:13

1 Answers1

2

I agree with @ono I prefer having customAdapter but there is always another way if you dont want to do the hard, long though better step.

one you can do is since the arrayList is String, you can add the number by doing:

arrayList.add(array[0][1] + " " + array[0][0]); //output will be "Mickey 1"
arrayList.add(array[1][0] + " " + array[1][1]);

and when you add this to the ListView, it will be the name and the number in the same line.

now for onItemClick you can either do a subString to the text that whenever it reaches a space, start cutting the needed number and send it in through. like:

mLstModes.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int pos, long id)
{
    TextView tv = (TextView) v;
    String number = tv.substring(" "); //will substring starting from the space till the end of the string.
    Intent intent = new Intent();
    intent.setClassName(getPackageName(), getPackageName()+ ".NextActivity");
    intent.putExtra("selected", number.trim()); // the trim is remove any whitespaces between the number
    startActivity(intent);
}
});

Be aware that this method wont work if you have spaces between the name, you can work it around by change the space to * then it would look like this Mickey * 1

please give me a feedback if this worked for you or not to give you an alternate method. cheers

Edit

another solution is to use hashMap , so basically use the name as key to get the value of the array..

first you need to declare a hashMap as global variable

HashMap<String, String> map = new HashMap<String, String>();

then use the following code: P.S: I didnt implement the code hope it works but I hope you get the Idea

mLstModes = (ListView) findViewById(R.id.LstModes);                      
ArrayList<String> arrayList = new ArrayList<String>();

arrayList.add(array[0][1]);
arrayList.add(array[1][1]);


map.add(array[0][1], array[0][0]);
map.add(array[1][1], array[1][0]);
// and so on.

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, arrayList);
mLstModes.setAdapter(arrayAdapter);                             
mLstModes.setOnItemClickListener(new OnItemClickListener()
{
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int pos, long id)
    {
        TextView tv = new view.TextView(getApplicationContext()); //to get the value of the name from the list to use it as a key
        Intent intent = new Intent();
        intent.setClassName(getPackageName(), getPackageName()+ ".NextActivity");
        intent.putExtra("selected", map.get(tv.getText().toString())); // use textView to get the ID.
        startActivity(intent);
    }
});
Coderji
  • 7,655
  • 5
  • 37
  • 51
  • I forgot the + in `arrayList.add(array[0][1] + " " + array[0][0]);` – Coderji Nov 23 '13 at 04:13
  • 1
    I think the best way is to map the name to the number. This works as as long as there is one space. It depends on what he's trying to do. – ono Nov 23 '13 at 04:31
  • yeah i know i could do this as walkaround, but the number is not important for the user - plus they are not sequential (like 3,5,8,2,4) - so I dont want to show it – user3023973 Nov 25 '13 at 10:07
  • you said you maybe have a alternate method... could you show it to me? Its importatnt that the number isnt shown at the list. the problem is I dont understand the way threw custom Adapters as I am beginner. Thanks a lot! – user3023973 Nov 28 '13 at 11:25
  • Please check the edited part, unfortunately I couldn't implement the code because Im short in time. but this is a great concept that will work with you. so give me a feedback if it worked! best luck – Coderji Nov 28 '13 at 17:21