Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I made two listviews. I need to know how to add the first number in the list and the number next to it (meaning in the second list) and so on.
Then toasting the number in the text view.

What I have tried:

package com.example.unknowm.adding_list_views;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
ListView listView1, listView2;
String[] names = new String[]{"1" , "2","3", "4", "5", "6", "7", "8", "9", "10"};
GridView lv; //dynamic size , array of string, elements of list view


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView1 = (ListView)findViewById(R.id.list1);
listView2 = (ListView)findViewById(R.id.list2);// listview is always vertical

ArrayAdapter<string> obj = new ArrayAdapter<string>(this, R.layout.list_items, names);
listView1.setAdapter(obj);

listView1.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String s = (String)listView1.getItemAtPosition(i);
Toast.makeText(MainActivity.this, s,Toast.LENGTH_SHORT).show();

}
}
);

ArrayAdapter<string> obj2 = new ArrayAdapter<string>(this, R.layout.list_items, names);
listView2.setAdapter(obj2);

listView2.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String s = (String)listView2.getItemAtPosition(i);
Toast.makeText(MainActivity.this, s,Toast.LENGTH_SHORT).show();

}
}
);



}
}
Posted
Comments
David Crow 16-Oct-17 10:17am    
Okay, so what part(s) of your requirement are you having trouble with? Do the two lists display correctly? Do they both respond to an item being clicked? Does the Toast object show the correct value?
Member 13390616 18-Oct-17 13:46pm    
I want to toast the result after adding the numbers. Yes, the toast object is showing the correct value. But I am little confused how to add the values.
David Crow 18-Oct-17 13:52pm    
Something like:

public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
{
    String s1 = (String) listView1.getItemAtPosition(i);
    String s2 = (String) listView2.getItemAtPosition(i);
    int sum = Integer.parseInt(s1) + Integer.parseInt(s2);
}
Member 13390616 17-Oct-17 15:40pm    
I want to toast the result after adding the numbers. Yes, the toast object is showing the correct value. But I am little confused how to add the values.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900