Example 2: ListFragment
File res/layout/list_fragment.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="match_parent" /> <TextView android:id="@android:id/empty" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Empty Listview" /> </LinearLayout>
File ListFragment.java
package com.example.fragment; import android.app.AlertDialog; import android.app.ListFragment; import android.content.DialogInterface; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Toast; import androidx.annotation.Nullable; import java.util.List; import java.util.ArrayList; public class ListFragment1 extends ListFragment implements AdapterView.OnItemClickListener{ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.list_fragment, container, false); return view; } ArrayAdapter<String> adapter; List<String> cities = new ArrayList<String>(); @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); cities.add("BangKok"); cities.add("HaNoi"); cities.add("Jakarta"); adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, cities); setListAdapter(adapter); getListView().setOnItemClickListener(this); } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(getActivity(), "Item: " + position, Toast.LENGTH_LONG).show(); AlertDialog.Builder adb= new AlertDialog.Builder(getActivity()); adb.setTitle("Delete?"); adb.setMessage("Are you sure you want to delete " + cities.get(position)); adb.setNegativeButton("Cancel", null); adb.setPositiveButton("Ok", new AlertDialog.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { cities.remove(position); adapter.notifyDataSetChanged(); }}); adb.show(); } }
File res/layout/acivity_main.xml
<?xml version="1.0" encoding="utf-8"?> <android.widget.RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:text="TUTORIALSPOTS.COM" /> <fragment android:name="com.example.fragment.ListFragment1" android:id="@+id/fragment" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/text" /> </android.widget.RelativeLayout>
File MainActivity.java
package com.example.fragment; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
1 Comment
Android Lesson 14: Alert Dialog | Free Online Tutorials
(December 14, 2020 - 3:03 am)[…] Android Lesson 12 part 2: ListFragment […]