kaveer
11/23/2016 - 4:53 PM

ListView

ListView

//xml layout
<ListView
  android:layout_width="match_parent"
  android:layout_height="255dp"
  android:id="@+id/listviewtest" />
  
//java class
ListView listView;

public class tool {
    String drill;
    String screwDriver;
}

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
      View view = inflater.inflate(R.layout.fragment_tools, container, false);

     ArrayList<tool> toolList = new ArrayList<>();
      tool tools = new tool();
      
      tools.drill ="hungry";
      tools.screwDriver = "makita";
      toolList.add(tools);
      
      tools.drill ="simple";
      tools.screwDriver = "duraco";
      toolList.add(tools);

      String[] listItems = new String[toolList.size()];
      for(int i = 0; i < toolList.size(); i++){
          listItems[i] = toolList.drill + " " + toolList.screwDriver
      }

      listView = (ListView)view.findViewById(R.id.listviewtest);
      ArrayAdapter adapter = new ArrayAdapter(this.getContext(), android.R.layout.simple_list_item_1, listItems);

      listView.setAdapter(adapter);

      listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

          @Override
          public void onItemClick(AdapterView<?> parent, View view,
                                  int position, long id) {

              // ListView Clicked item index
              int itemPosition     = position;

              // ListView Clicked item value
              String  itemValue    = (String) listView.getItemAtPosition(position);

              // Show Alert

              Toast messageBox = Toast.makeText(getActivity() , +itemPosition+ "Insurance removed" +itemValue  , Toast.LENGTH_SHORT);
              messageBox.show();


          }

      });


      return view;
  }
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <ListView
                android:id="@+id/list1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />

            <View
                android:layout_width="fill_parent"
                android:layout_height="5dp"
                android:background="#ff0000" />

            <ListView
                android:id="@+id/list2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />

            <View
                android:layout_width="fill_parent"
                android:layout_height="5dp"
                android:background="#ff0000" />

            <ListView
                android:id="@+id/list3"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />

            <View
                android:layout_width="fill_parent"
                android:layout_height="5dp"
                android:background="#ff0000" />
        </LinearLayout>
    </ScrollView>

</RelativeLayout>
//xml layout
<ListView
  android:layout_width="match_parent"
  android:layout_height="255dp"
  android:id="@+id/listviewtest" />
  
//java class
ListView listView;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_tools, container, false);

        String[] values = new String[] { "Android List View",
                "Adapter implementation",
                "Simple List View In Android",
                "Create List View Android",
                "Android Example",
                "List View Source Code",
                "List View Array Adapter",
                "Android Example List View"
        };

        listView = (ListView)view.findViewById(R.id.listviewtest);

        ArrayAdapter adapter = new ArrayAdapter(this.getContext(), android.R.layout.simple_list_item_1, values);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {

                // ListView Clicked item index
                int itemPosition     = position;

                // ListView Clicked item value
                String  itemValue    = (String) listView.getItemAtPosition(position);

                // Show Alert

                Toast messageBox = Toast.makeText(getActivity() , +itemPosition+ "Insurance removed" +itemValue  , Toast.LENGTH_SHORT);
                messageBox.show();


            }

        });


        return view;
    }