listview in android studio code example
Example 1: listview in android
Android ListView is a view which groups several items and display them in vertical scrollable list. The list items are automatically inserted to the list using an Adapter that pulls content from a source such as an array or database.
Example 2: how to create list in android '
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView list = findViewById(R.id.list);
ArrayList arrayList = new ArrayList<>();
arrayList.add("JAVA");
arrayList.add("ANDROID");
arrayList.add("C Language");
arrayList.add("CPP Language");
arrayList.add("Go Language");
arrayList.add("AVN SYSTEMS");
ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, arrayList);
list.setAdapter(arrayAdapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
String clickedItem=(String) list.getItemAtPosition(position);
Toast.makeText(MainActivity.this,clickedItem,Toast.LENGTH_LONG).show();
}
});
}
}
Example 3: android java list view
ListView friendsListView = findViewById(R.id.friendListView);
final ArrayList myFriends = new ArrayList(asList("Mark","Jane","Sussy","Jan"));
ArrayAdapter arrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, myFriends);
friendsListView.setAdapter(arrayAdapter); // Set an adapter
friendsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> adapterView, View view, int i, long l) {
Toast.makeText(getApplicationContext(), "Hello " + myFriends.get(i), Toast.LENGTH_LONG).show();