user3023973
user3023973

Reputation: 1

How to add a variable/value to a listview and send it to a new activity

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!

Upvotes: 0

Views: 170

Answers (1)

Coderji
Coderji

Reputation: 7745

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);
    }
});

Upvotes: 2

Related Questions