Sure, here's an example Android code to create an ArrayList of integers and strings, and display it in a ListView in the main activity:
First, create a new project in Android Studio and add the following code to the MainActivity.java file:
code
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
// Declare variables
private ListView listView;
private Button addButton;
private ArrayList<Integer> intList = new ArrayList<Integer>();
private ArrayList<String> stringList = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find UI elements
listView = findViewById(R.id.list_view);
addButton = findViewById(R.id.add_button);
// Set up ListView adapter
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, stringList);
listView.setAdapter(adapter);
// Add items to ArrayList when button is clicked
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
intList.add(1);
stringList.add("Item " + intList.size());
adapter.notifyDataSetChanged();
}
});
}
}
In this code, we first import the necessary classes and define our variables. We have a ListView for displaying the list, a Button for adding new items, and two ArrayLists: one for integers and one for strings.
In the onCreate method, we set the content view and find the ListView and Button UI elements. We then set up the ArrayAdapter for the ListView using the stringList ArrayList. We also set up an OnClickListener for the addButton that adds a new integer and string to the respective ArrayLists, updates the adapter, and increments the integer by 1.
In the activity_main.xml layout file, we can add the following ListView and Button elements:
This will create a ListView with a divider and a button centered below it.
When you run the app, you can click the button to add new items to the list, which will be displayed in the ListView.
code
import android.os.Bundle
import android.view.View
import android.widget.ArrayAdapter
import android.widget.Button
import android.widget.ListView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
// Declare variables
private lateinit var listView: ListView
private lateinit var addButton: Button
private val intList = ArrayList<Int>()
private val stringList = ArrayList<String>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Find UI elements
listView = findViewById(R.id.list_view)
addButton = findViewById(R.id.add_button)
// Set up ListView adapter
val adapter = ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, stringList)
listView.adapter = adapter
// Add items to ArrayList when button is clicked
addButton.setOnClickListener {
intList.add(1)
stringList.add("Item ${intList.size}")
adapter.notifyDataSetChanged()
}
}
}
In this Kotlin code, we have the same variables as before, but we use the lateinit keyword to declare them without initializing them. We also use the val keyword to declare our ArrayLists since we don't need to reassign them.
The onCreate function is similar to the Java version, except we use the findViewById function to find our UI elements and use the adapter property of the listView to set the adapter. We also use a lambda expression instead of an anonymous inner class for the OnClickListener.
Finally, in the activity_main.xml layout file, we can use the same ListView and Button elements as before:
This will create the same UI as before. When you run the app, you can click the button to add new items to the list, which will be displayed in the ListView.