Unable to add items to ListView - android

I have a dialog where I need to add name and If I click Ok button that name entered in the edittext should be added to my ListView which is in fragment
This is my homefragment:
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new HomeFragment();
break;
Here is my input-dialog:
<LinearLayout 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:padding="#dimen/activity_vertical_margin"
android:orientation="vertical" >
<TextView
android:id="#+id/list_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter name for shopping list" />
<EditText
android:id="#+id/list_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" />
This is my fragment_home:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView android:id="#+id/itemslistView"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_marginTop="75dp"/>
</RelativeLayout>
Here is my dialog code:
protected void showInputDialog() {
// get prompts.xml view
LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
View promptView = layoutInflater.inflate(R.layout.input_dialog, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
MainActivity.this);
alertDialogBuilder.setView(promptView);
final EditText editText = (EditText) promptView
.findViewById(R.id.list_text);
// setup a dialog window
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
LayoutInflater layoutInflater = LayoutInflater
.from(MainActivity.this);
View lstview = layoutInflater.inflate(
R.layout.fragment_home, null);
lv = (ListView) lstview
.findViewById(R.id.itemslistView);
List<String> MyList = new ArrayList<String>();
String NewListname=editText.getText().toString();
MyList.add(NewListname);
Toast.makeText(MainActivity.this, NewListname, Toast.LENGTH_LONG).show();
ArrayAdapter<String> adp = new ArrayAdapter<String>(
MainActivity.this, R.layout.list, MyList);
lv.setAdapter(adp);
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// create an alert dialog
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}
This is my HomeFragment:
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.Fragment;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
public class HomeFragment extends Fragment {
public HomeFragment() {
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container,
false);
return rootView;
}
}
My problem is I'm able to get the text from edittext as shown with toast but unable to add it to listview.
Can anyone say me where am I going wrong?

After adding text in your MyList you just called adapter.notifyDataSetChanged();
No need to setAdapter(adp); again

Please try with follwing code (i never tried in devices)
protected void showInputDialog() {
// get prompts.xml view
LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
View promptView = layoutInflater.inflate(R.layout.input_dialog, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
MainActivity.this);
alertDialogBuilder.setView(promptView);
final EditText editText = (EditText) promptView
.findViewById(R.id.list_text);
LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
View lstview = layoutInflater.inflate(R.layout.fragment_home, null);
lv = (ListView) lstview.findViewById(R.id.itemslistView);
List<String> MyList = new ArrayList<String>();
final ArrayAdapter<String> adp = new ArrayAdapter<String>(MainActivity.this,
R.layout.list, MyList);
lv.setAdapter(adp);
// setup a dialog window
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
String NewListname = editText.getText().toString();
MyList.add(NewListname);
Toast.makeText(MainActivity.this, NewListname,
Toast.LENGTH_LONG).show();
adp.notifyDataSetChanged();
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// create an alert dialog
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}

Try This:
In your HomeFragment:
List<String> MyList = new ArrayList<String>();
ListView lv=(ListView)findViewById(R.id.itemslistView);
protected void showInputDialog()
{
// get prompts.xml view
LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
View promptView = layoutInflater.inflate(R.layout.input_dialog, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
MainActivity.this);
alertDialogBuilder.setView(promptView);
final EditText editText = (EditText) promptView
.findViewById(R.id.list_text);
// setup a dialog window
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
String NewListname=editText.getText().toString();
MyList.add(NewListname);
Toast.makeText(MainActivity.this, NewListname, Toast.LENGTH_LONG).show();
ArrayAdapter<String> adp = new ArrayAdapter<String>(
MainActivity.this, R.layout.list, MyList);
lv.setAdapter(adp);
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// create an alert dialog
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}

Related

Displaying ListView in alertDailog android

When the user clicks the item of list View a alert Dialog box will be displayed to the user with options like "view","delete" or "update"
the list view item. Clicking on the list view item for the first time displays the alert Dialog with options but when i click another list View item or the same list View item the second time the app crashes.Thanks in advance.
Error :java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
//The main list displayed when the user log in successfully
ListView = (ListView) findViewById(R.id.list);
ListView2 = new ListView(this); //List to be displayed in the alert Dailog box
ListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
long i = ListView.getItemIdAtPosition(position);
showAlertDailog();//The dialog box method
}
});
if(mCursorAdapter == null) //To check if the list is empty
{
TextView emptyTextView = (TextView) findViewById(R.id.empty);
emptyTextView.setText("No Notes");
ListView.setEmptyView(emptyTextView);
}else {
mCursorAdapter = new DataCursorAdapter(this, null);
ListView.setAdapter(mCursorAdapter);
}
public void showAlertDailog()
{
String [] items = {"View","Delete","Update"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.item_todo_2,R.id.textView4,items);
ListView2.setAdapter(adapter);
final android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(HomeActivity.this);
builder.setView(ListView2)
.setCancelable(false)
.setPositiveButton("Close",null);
android.app.AlertDialog alertDialog = builder.create();
alertDialog.show();
}
ArrayList<String> arrayList = new ArrayList<String>();
CharSequence[] animals = arrayList.toArray(new String[arrayList.size()]);
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
dialogBuilder.setItems(animals, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
String selectedText = animals[item].toString();
}
});
AlertDialog alertDialogObject = dialogBuilder.create();
alertDialogObject.show();
This is simple way to display list inside alert dialog, where in arraylist just add string whatever you have to display inside alert dialog list.
It is exactly what you need
public class MainActivity extends Activity implements OnClickListener {
private Button mDoneButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDoneButton = (Button) findViewById(R.id.done_button);
mDoneButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
final CharSequence[] items = {
"Rajesh", "Mahesh", "Vijayakumar"
};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Make your selection");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
// Do something with the selection
mDoneButton.setText(items[item]);
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
http://rajeshvijayakumar.blogspot.in/2013/04/alert-dialog-dialog-with-item-list.html
OUTPUT
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String[] listItems = { "Colour", "Font Size", };
if (listItems[position].equals("Font Size")) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
Settings.this );
// set title
alertDialogBuilder.setTitle("Choose Font Size");
// set dialog message
alertDialogBuilder
.setMessage("Click yes to exit!")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
}
});
public void showAlertDailog(Context context) {
ListView listView=new ListView(context);
yourLayout.LayoutParams params= yourLayout.LayoutParams(Yourlayout.LayoutParams.MATCH_PARENT,Yourlayout.LayoutPa
rams.MATCH_PARENT);
listview.setLayoutParams(params);
String [] items = {"View","Delete","Update"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,items);
listView.setAdapter(adapter);
final android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(context);
builder.setView(listView)
.setCancelable(false)
.setPositiveButton("Close",(dialog,something)->{dialog.dismiss()});
android.app.AlertDialog alertDialog = builder.create();
alertDialog.show();
}

Display more than one item in array list

Hello guys i would like to display more than one item horizontally in a single list. This items are picked from the dialog box that has two button ok and cancel. When you click okay it should pick the values from the edit text and a string value from the dialog and display in the list horizontally.
Array List Adapter
public class VehicleListAdapter extends ArrayAdapter<String> {
private Context context;
private List<String> vehicle_no;
public VehicleListAdapter(Context context, int resource, List<String> objects) {
super(context, resource, objects);
this.context = context;
this.vehicle_no = objects;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//get the vehicle number we are displaying
String my_vehicle = vehicle_no.get(position);
//get the inflater and inflate the XML layout for each item
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.vehicle_num_layout, null);
TextView txt_vehicle_num = (TextView) view.findViewById(R.id.txt_vehicle_num);
txt_vehicle_num.setText(my_vehicle);
return view;
}
}
Activity with the list
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// get prompts.xml view
LayoutInflater li = LayoutInflater.from(context);
View promptsView = li.inflate(R.layout.prompts, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(promptsView);
final EditText userInput = (EditText) promptsView
.findViewById(R.id.editTextDialogUserInput);
// set dialog message
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// get user input and set it to result
// edit text
if(userInput.getText().toString().trim().length()==0) {
userInput.setError("Vehicle Number Required");
focus.start();
String stime = focus.getText().toString();
}
else {
result.setText(userInput.getText());
ListView list = (ListView) findViewById(R.id.list);
vehicle_list.add(userInput.getText().toString().trim());
VehicleListAdapter listAdapter = new VehicleListAdapter(StartWatch.this, 0, vehicle_list);
list.setAdapter(listAdapter);
}
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
First of all, why you are creating new listview every time when user hit ok of dialog. Remove this line ListView list = (ListView) findViewById(R.id.list); from dialog positive button click.
Add line as shown below after your code in positive button click
VehicleListAdapter listAdapter = new VehicleListAdapter(StartWatch.this, 0, vehicle_list);
list.setAdapter(listAdapter);
listAdapter.notifyDataSetChanged(); // Add this line to tell adapter that data has been changed

AlertDialog box containing spinner and edittext returning a nullpointer when called

I have a dialog box that works perfectly well when i use just the spinner, but returns a null pointer when i try to fetch a value from an edit Text to be used for calculation in the OK button.
below is my deliberations for your consideration
//the alert box
public boolean dialogValuessP1() {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
Arena.this);
alertDialogBuilder.setTitle("Connecting Options");
LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View dialogView = li.inflate(R.layout.option, null);
EditText connectionNumber = (EditText)findViewById(R.id.txtConnectedBy);
connectedV = Integer.parseInt(connectionNumber.getText().toString());
spinnercategory = (Spinner) dialogView.findViewById(R.id.viewSpin);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.category, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnercategory.setAdapter(adapter);
spinnercategory.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
ConnectBY = spinnercategory.getItemAtPosition(arg2).toString();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
alertDialogBuilder.setView(dialogView);
alertDialogBuilder.setPositiveButton("Connect",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
if (ConnectBY == "Single Score") {
scoreV = 1 * connectedV;
} else if (ConnectBY == "Double Score") {
scoreV = 2 * connectedV;
} else if (ConnectBY == "Tripple Score") {
scoreV = 3 * connectedV;
}
alert = true;
}
});
alertDialogBuilder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// cancel the alert box and put a Toast to the user
dialog.cancel();
Toast.makeText(getApplicationContext(),
"Please choose Connection Pattern",
Toast.LENGTH_LONG).show();
// Arena.this.finish(); used to close the game
alert = false;
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
return alert;
}
//the option.xml is the Xml file used
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingLeft="10dip" >
<Spinner
android:id="#+id/viewSpin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="#array/category" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/connectwith" />
<EditText
android:id="#+id/txtConnectedBy"
android:layout_width="76dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
</LinearLayout>
Since the edittext is inside the alert dialog
try using
EditText connectionNumber =(EditText) dialogView .findViewById(R.id.txtConnectedBy);
and to retrive the value use
String edittextValue=connectionNumber.getText().toString()
I think you can achieve this functionality with a custom dialog functionality
final Dialog dialog = new Dialog(CustomDialog.this);
// Include dialog.xml file
dialog.setContentView(R.layout.dialog);
// Set dialog title
dialog.setTitle("Custom Dialog");
// set values for custom dialog components - text, image and button
EdittText text = (EdittText ) dialog.findViewById(R.id.textDialog);
Spinner spinimage = (Spinner) dialog.findViewById(R.id.spinner);
//spinner details here
dialog.show();
Button ok= (Button) dialog.findViewById(R.id.ok);
ok.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Close dialog
dialog.dismiss();
String text = text.getText().toString();
//do something with the edittext value
}
});
please make a try and share your view
Thanks for the suggestions, i made the following changes to the code and was able to use both an edittext and a spinner in the same alertdialog
public boolean dialogValuessP1() {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
alertDialogBuilder.setTitle("Connecting Options");
LayoutInflater li = LayoutInflater.from(context);
final View dialogView = li.inflate(R.layout.option, null);
final EditText input = (EditText) dialogView
.findViewById(R.id.txtConnectedBy);
spinnercategory = (Spinner) dialogView.findViewById(R.id.viewSpin);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.category, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnercategory.setAdapter(adapter);
spinnercategory.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
position = arg2 + 1;
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
alertDialogBuilder.setView(dialogView);
alertDialogBuilder.setPositiveButton("Connect",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
edittextValue = input.getText().toString();
connectedV = Integer.parseInt(edittextValue);
scoreV = position * connectedV;
playNext();
alert = true;
}
});
alertDialogBuilder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// cancel the alert box and put a Toast to the user
dialog.cancel();
Toast.makeText(getBaseContext(),
"It is not your turn to Play",
Toast.LENGTH_LONG).show();
alert = false;
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
return true;
}

Unable to See Button(s) in an inflated View

I had created AlertDialog by inflating the following XML:
LayoutInflater li = LayoutInflater.from(this);
View dialogView = li.inflate(R.layout.activity_list_logs, null);
ListView list = (ListView) dialogView.findViewById(R.id.listDates);
The full code for showing alertDialog is:
private void showLogs(final List<Absentees> abs) {
LayoutInflater li = LayoutInflater.from(this);
View dialogView = li.inflate(R.layout.activity_list_logs, null);
ListView list = (ListView) dialogView.findViewById(R.id.listDates);
list.setAdapter(new CustomAdapterTagAbsentees(this,abs));
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View view,int position, long id) {
Absentees a =(Absentees)adapter.getItemAtPosition(position);
try
{
showLogDetails(a);
}
catch(Exception e)
{
show_popup(e+"");
}
}
});
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setView(dialogView);
alertDialogBuilder.setTitle("Attendance Details:");
alertDialogBuilder.setMessage("Day : ");
alertDialogBuilder
.setCancelable(true)
.setPositiveButton("Dismiss",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
The Layout Used :
<?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"
android:background="#color/white"
>
<ListView
android:id="#+id/listDates"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:textSize="20sp"
/>
</LinearLayout>
About My Problem:
if the List is small enough to fit the screen I can see "Dismiss" button(The positiveButton).
Else if the List is too large, I can't see the "Dismiss" Button
Kindly Help me!!, I tried to add a Button to Bottom of the within the layout itself, Its showing correctly, But i wonder why this default way of showing a positiveButton is not displaying at all
Use a RelativeLayout instead of a Linear. Put the buttons to layout_alignParentBottom="true". Put the listview to layout_above="id of button". This will force the buttons to the bottom of the screen and reserve space for them before the listview is drawn. Otherwise the listview is greedy and will suck up all the space needed.
Here is the final Solution for my problem, Thanks for #blackbelt for the suggestion
private void showLogs(final List<Absentees> abs) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
final ListAdapter adapter = new CustomAdapterTagAbsentees(this,abs);
alertDialogBuilder.setAdapter(adapter, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
showLogs(abs);
Absentees a =(Absentees) adapter.getItem(item);
showLogDetails(a);
}
});
alertDialogBuilder.setTitle("Attendance Details:");
//alertDialogBuilder.setMessage("Day : XXXX");
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("Dismiss",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
I have to comment/remove
alertDialogBuilder.setMessage("Day : XXXX ");
for succesfull implementation, Dont know why!!

Show list view in a popup window - Android

I'm working on a project in Android and I have a problem.
I have an activity which includes three buttons, edit text and a list view.
I want to change that implementation and to show the list view on a new popup window only when the user press the select all button.
I've added my code, thanks.
public class Notepadv1 extends ListActivity implements OnClickListener {
private WordsDbAdapter mDbHelper;
private Button selectAllButton;
private PopupWindow mPopup;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
selectAllButton = (Button)findViewById(R.id.selectAll);
selectAllButton.setOnClickListener(this);
mDbHelper = new WordsDbAdapter(this);
mDbHelper.open();
fillData();
}
public void onClick(View v) {
switch(v.getId()){
case(R.id.selectAll):
selectAll();
break;
}
}
private void selectAll(){
}
private void fillData() {
Cursor c = mDbHelper.fetchAllNotes();
startManagingCursor(c);
String[] from = new String[] { WordsDbAdapter.KEY_WORD };
int[] to = new int[] { R.id.text1 };
SimpleCursorAdapter notes = new SimpleCursorAdapter(this, R.layout.words_row, c, from, to);
setListAdapter(notes);
}
}
Show a simple Alert Dialog with a list:
final CharSequence[] items = {"Red", "Green", "Blue"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a color");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert = builder.create();
alert.show();
Yes you can do it.
1st way:
define an activity as Dialog with the below attribute in AndroidManifest.xml file:
<activity android:theme="#android:style/Theme.Dialog" />
2nd way:
You can inflate the XML layout inside the dialog as below:
Dialog dialog = new Dialog(context);
LayoutInflater li = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = li.inflate(R.layout.my_layout, null, false);
dialog.setContentView(v);
dialog.show();
for example:
edit: link fixed
Android Dialog with ListView.
If using a custom ArrayAdapter, use setAdapter():
AlertDialog.Builder builder = new Builder(this)
.setTitle("Dialog Title")
.setAdapter(new CustomAdapter(context, items, ...), (dialog, itemPosition) -> {
// Handle item click
});
builder.show();

Categories

Resources