android list 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();
         }
      });
   }
}

Tags:

Misc Example