Transferring from one activity ( database ) to another activity - android

import android.app.Activity;
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
public class Database extends ListActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
DatabaseHelper dbh = new DatabaseHelper(this);
Cursor myCursor = dbh.getReadableDatabase()
.rawQuery("SELECT _id, " + DatabaseHelper.NAME +
", " + DatabaseHelper.VALUE +
" FROM " + DatabaseHelper.TABLE, null);
String[] dataFrom = {DatabaseHelper.NAME, DatabaseHelper.VALUE};
int[] dataTo = {R.id.name, R.id.value};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.row, myCursor, dataFrom, dataTo);
setListAdapter(adapter);
/*TextView tv = new TextView(this);
tv.setText("Hello, Android");
setContentView(tv);*/
}
public void onListItemClick()
{
}
}
I am trying to insert a button at the bottom of the Listview to goto another activity. Is it possible to insert a button in this listview ? I need the button to access the next activity which will be a a menu containing options.

If you want to Add an extra button on the Activity then you have to replace this line--
public class Database extends ListActivity
from this line--
public class Database extends Activity
and you have to use this line also--
setContentView(R.layout.main);
Because if you want to add extra control then its neccessary to set layout..
Afterthat Simply In your main.xml--
<Button android:id="#+id/button" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:gravity="center"
android:text="Next Activity" />
<ListView android:id="#android:id/list" android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
In Activity use add this also--
ListView lv=(ListView)findViewById(R.id.list);
lv.setClickable(true);
// to set button
Button button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent=new Intent(this,NextActivity.class);
startActivity(intent);
}
});
And Replace this line--
setListAdapter(adapter);
From--
lv.setAdapter(adapter);
I have solved your problem....whereever i told the mistake.... changed it accordingly...

In XML :
<ListView android:id="#android:id/list" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:dividerHeight="1dip"
android:cacheColorHint="#00000000" />
<Button android:id="#+id/button1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:gravity="center"
android:text="Next" />
In Activity :
Button button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
}
});

Related

OnItemClickListener doesn't fire

The Register Button works well, but the debugger doesn't run into the setOnItemClickListener onItemClick. If You click on a item, the activity for editing the record should be launched.
package at.bomsbg.MindX;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import at.bomsbg.MindX.sqlite.helper.MindXopenHelper;
import at.bomsbg.MindX.sqlite.helper.MindXtableAdapter;
public class Activity_cardmanager extends Activity {
MindXtableAdapter mindxtableadapt;
MindXopenHelper openHelper;
ListView nameList;
Button registerBtn;
Cursor cursor;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cardmanager);
nameList = (ListView) findViewById(R.id.title);
registerBtn = (Button) findViewById(R.id.btn_register);
mindxtableadapt = new MindXtableAdapter(this);
String[] from = { MindXopenHelper.title, MindXopenHelper.detail,
MindXopenHelper.active };
int[] to = { R.id.tv_title, R.id.tv_detail, R.id.tv_active };
cursor = mindxtableadapt.queryName();
SimpleCursorAdapter cursorAdapter =
new SimpleCursorAdapter(this,
R.layout.row, cursor, from, to);
nameList.setAdapter(cursorAdapter);
nameList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> tobjParent, View tobjView,
int tintPosition, long tlngid) {
Bundle passdata = new Bundle();
Cursor listCursor = (Cursor) tobjParent
.getItemAtPosition(tintPosition);
int nameId = listCursor.getInt(listCursor
.getColumnIndex(MindXopenHelper.KEY_ID));
passdata.putInt(MindXopenHelper.KEY_ID, nameId);
Intent passIntent = new Intent(Activity_cardmanager.this,
Activity_edit_mindxtables.class);
Toast.makeText(getApplicationContext(),
Integer.toString(nameId), 500).show();
passIntent.putExtras(passdata);
startActivity(passIntent);
}
});
registerBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent registerIntent = new Intent(Activity_cardmanager.this,
Activity_MindXtablesRecord.class);
startActivity(registerIntent);
}
});
}
#Override
public void onResume() {
super.onResume();
cursor.requery();
}
}
activity_Cardmanager.html
<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:orientation="vertical" >
<ListView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView>
<Button
android:id="#+id/btn_register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/register" />
</LinearLayout>
row.xml
I add the item clickable for the textfield. I think that is not realy necesarry.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:orientation="horizontal" >
<TextView
android:id="#+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:width="100dp" />
<TextView
android:id="#+id/tv_detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:width="100dp" />
<CheckBox
android:id="#+id/tv_active"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp" />
</LinearLayout>
You have focusable/clickable items in your list view item - the checkbox.
Option 1 - Set the android:descendantFocusability="blocksDescendants" flag to your ListView and you should have fired the onItemClickListener.
Option 2 - In your Adapter you can set an onClickListener on the entire list view item object, and handle it from there.
the Problem of my solution was on row.xml
again thanX on all:
row.xml
<LinearLayout
android:clickable="true" (removed)
<TextView
android:id="#+id/tv_title"
android:onClick="Title" (added)
android:clickable="true" (added)
android:focusable="true" (added)
on my Activity_cardmanager I inserted following code after nameList.setAdapter...
nameList.setClickable(true); // 2015-04-14
nameList.setFocusable(true); // 2015-04-14
nameList.setItemsCanFocus(true); // 2015-04-14
setViewItemListener();
.
.
.
public void setViewItemListener() {
nameList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> tobjParent, View tobjView, int tintPosition, long tlngid) {
Bundle passdata = new Bundle();
// Cursor listCursor = (Cursor) tobjParent.getItemAtPosition(tintPosition);
Cursor listCursor = (Cursor) nameList.getItemAtPosition(tintPosition); //2015-04-15
int nameId = listCursor.getInt(listCursor
.getColumnIndex(MindXopenHelper.KEY_ID));
passdata.putInt(MindXopenHelper.KEY_ID, nameId);
Intent passIntent = new Intent(Activity_cardmanager.this,
Activity_edit_mindxtables.class);
Toast.makeText(getApplicationContext(),
Integer.toString(nameId), Toast.LENGTH_LONG).show();
passIntent.putExtras(passdata);
startActivity(passIntent);
}
});
}
the returnValue is true, but that is another story.

Add user input into a ListView on button click

I am trying to make a TO DO LIST. I have a EditText, Button, and ListView. On button click I want to add, what I typed into the EditText into a ListView.
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<EditText
android:id="#+id/editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter task"
android:textSize="24dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:id="#+id/addTaskBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Add Task"
android:layout_below="#+id/editText"
android:layout_alignParentLeft="true" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=" Task"
android:id="#+id/header"
android:layout_below="#+id/addTaskBtn"
android:background="#5e5e5e"
android:textColor="#FFFFFF"
android:textSize="14dp"/>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/list"
android:layout_below="#+id/header"
android:layout_centerHorizontal="true" />
Main_ToDoList.java
package com.example.todolist;
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
public class Main_ToDoList extends Activity implements OnClickListener
{
private Button btnAdd;
private EditText et;
private ListView lv;
ArrayList<String> list = new ArrayList<String>();
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
btnAdd = (Button)findViewById(R.id.addTaskBtn);
btnAdd.setOnClickListener(this);
et = (EditText)findViewById(R.id.editText);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, list);
lv.setAdapter(adapter);
}
public void onClick(View v)
{
String input = et.getText().toString();
if(input.length() > 0)
{
list.add(input);
adapter.notifyDataSetChanged();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main__to_do_list, menu);
return true;
}
}
The code doesn't work, new to android developing, and just trying to create a simple To Do List. Thanks for your help!
This should do it.
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
btnAdd = (Button)findViewById(R.id.addTaskBtn);
btnAdd.setOnClickListener(this);
et = (EditText)findViewById(R.id.editText);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, list);
// set the lv variable to your list in the xml
lv=(ListView)findViewById(R.id.list);
lv.setAdapter(adapter);
}
public void onClick(View v)
{
String input = et.getText().toString();
if(input.length() > 0)
{
// add string to the adapter, not the listview
adapter.add(input);
// no need to call adapter.notifyDataSetChanged(); as it is done by the adapter.add() method
}
}
you need to find your listview and then set the adapter:
lv=(ListView)findViewById(android.R.id.yourlistview);
lv.setAdapter(adapter);

How to get data form SQLite database and display it in ListView

I have edited my previous post here.(How to use Android SQLite Database)
In this post I have changed the code for 'Show' button. Now I am trying to access the database. I have added a ListView and trying to display the content of database in it. But again getting the same error. ("Unfortunately learn has stopped.") on tapping the Show button.
Here is the complete code -
package com.example.learn;
import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
public class MainActivity extends Activity {
Button b1, b2, b3;
EditText t1;
ListView lv1;
SQLiteDatabase db;
Cursor c;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.button1);
b2 = (Button) findViewById(R.id.button2);
b3 = (Button) findViewById(R.id.button3);
t1 = (EditText) findViewById(R.id.editText1);
lv1 = (ListView) findViewById(R.id.listView1);
db = openOrCreateDatabase("MyDB", MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS Names(Name VARCHAR)");
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (t1.getText().toString().equals("")) {
t1.setText("Enter Name");
}
else {
String input = t1.getText().toString();
db.execSQL("INSERT INTO Names VALUES(" + "'" + input + "'"
+ ")");
}
}
});
b2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
c = db.rawQuery("SELECT * FROM Names", null);
int count = c.getCount();
String[] values = new String[count + 1];
int i = 0;
do {
values[i] = c.getString(c.getColumnIndex("Name"));
i++;
} while (c.moveToNext());
// Define a new Adapter
// First parameter - Context
// Second parameter - Layout for the row
// Third parameter - ID of the TextView to which the data is
// written
// Forth - the Array of data
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getBaseContext(), android.R.layout.simple_list_item_1,
android.R.id.text1, values);
// Assign adapter to ListView
lv1.setAdapter(adapter);
}
});
b3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
db.execSQL("DELETE FROM Names");
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
and this is activity_main.xml -
<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"
tools:context=".MainActivity" >
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:ems="10"
android:hint="Add" >
</EditText>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/editText1"
android:layout_marginTop="18dp"
android:text="#string/add" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button1"
android:layout_alignBottom="#+id/button1"
android:layout_centerHorizontal="true"
android:text="#string/show" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button2"
android:layout_alignBottom="#+id/button2"
android:layout_alignParentRight="true"
android:text="#string/del" />
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_below="#+id/button1" >
</ListView>
</RelativeLayout>
I think something wrong with
do {
values[i] = c.getString(c.getColumnIndex("Name"));
i++;
}
while (c.moveToNext());
what is the problem now? Please Help.
Try looping like this:
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
values[i] = c.getString(c.getColumnIndex("Name"));
}
I think you should try this code.
Cursor cursor = new Cursor();
cursor.moveToFirst();
do{
values[i++] = cursor.getString(cursor.getColumnIndex("name"));
}while(cursor.moveToNext());
cursor.close();

How I can select a value in my Android ListView and send this Information to a other Activity?

Hi I want to built a android a application with a ListView and if I select a value in this ListView I want to start a other activity with this value. The Problem is I don't know how I can click on a value in my ListView and the other activity get the informations. Here is my Code:
My First Activity:
package de.android.shilfe;
import de.android.shilfe.DatenbankManager;
import de.android.shilfe.R;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import android.app.ListActivity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class MainActivity extends ListActivity {
private SQLiteDatabase mDatenbank;
private DatenbankManager mHelper;
private static final String KLASSEN_SELECT_ROW =
"SELECT _id, name FROM Stundenplan";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mHelper.STUNDENPLAN_CREATE = "CREATE TABLE Stundenplan(" +
"_id INTEGER PRIMARY KEY AUTOINCREMENT, "+
"name TEXT NOT NULL)";
mHelper = new DatenbankManager(this);
//what I must do here ??? :(
}
#Override
protected void onPause() {
super.onPause();
mDatenbank.close();
Toast.makeText(this,
getResources().getString(R.string.db_close),
Toast.LENGTH_SHORT).show();
}
#Override
protected void onResume() {
super.onResume();
mDatenbank = mHelper.getReadableDatabase(); //Datenbank öffnen
Toast.makeText(this,
getResources().getString(R.string.db_open),
Toast.LENGTH_SHORT).show();
ladeDaten();
}
private void ladeDaten() {
Cursor KlassenCursor = mDatenbank.rawQuery(KLASSEN_SELECT_ROW, null); // Gibt ein Index zurück
startManagingCursor(KlassenCursor); //Durchläuft diesen
android.widget.SimpleCursorAdapter KlassenAdapter = new android.widget.SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,
KlassenCursor,
new String[] {"name"},
new int[] {
android.R.id.text1
});
setListAdapter(KlassenAdapter);
}
public void onButtonClick(View view){
EditText et =(EditText) findViewById(R.id.editText1);
ContentValues werte = new ContentValues();
werte.put("name",et.getText().toString());
mDatenbank.insert("stundenplan", null, werte);
ladeDaten();
et.setText("");
}
}
My First Activity Layout xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="5">
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/create_defaulttext">
<requestFocus />
</EditText>
<Button
android:id="#+id/sf_daten_datenbanken_einfuegen"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/sf_daten_datenbanken_einfuegen"
android:onClick="onButtonClick" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<ListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</ListView>
<TextView
android:id="#+id/android:empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tx_daten_datenbank_leer"/>
</LinearLayout>
</LinearLayout>
I try it in a other Application with this code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List valueList = new ArrayList<String>();
for(int i=0;i<10;i++)
{
valueList.add("value" + i);
}
ListAdapter adapter = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,valueList);
final ListView lv = (ListView)findViewById(R.id.listview1);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3){
Intent intent = new Intent();
intent.setClassName(getPackageName(), getPackageName() + ".Show_Activity");
intent.putExtra("selected",lv.getAdapter().getItem(arg2).toString());
startActivity(intent);
}
});
}
but in the other Application I fill the ListView by a EditText Control
I need help :(
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
Intent intent = new Intent(context, Show_Activity.class);
intent.putExtra("selected", (String) lv.getItemAtPosition(position));
startActivity(intent);
}
});
You already did pass the value to another activity with putExtra
Hey You question is not complete but I am giving this answer as per what I understand. In the onListItemClick() method last parameter of long datatype is id of a row selected. in your case, it will _id of table. If you have 10 rows in your table with id 10 to 20 and displayed the same in listview. Now if you click on 5 item then its id will be 15 which was stored in database. Pass this value to next intent and fetch the details of that row and display in the next activity.
When you use a ListActivity so you can use onListItemClick method.This method will be called when an item in the list is selected.
You can to create a custom list adapter and handle the onClick event in the getView method of the adapter. The item will be available in this method.
public class AccountListAdapter extends ArrayAdapter<Account> {
#Override
public View getView(int position, View convertView, ViewGroup parent){
final Account item = this.getItem(position);
// item = the particular list item you clicked
// layoutItem = layout view passed into the adapter
layoutItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
callActivity(item);
}
});
}
}
While handling the event, create an intent with extras and pass the value(s) you desire via the extras. You can use a simplelistadapter, but this way gives you considerably more control and power!

Using a list view in a custom dialogue

How would I use a list in this custom dialogue? ive set a list int the .xml of the listview but am unfamiliar with how to use it properly (I want the values for the list to come from a string array)
My main java:
package custom.dialouge.list;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
public class CustomDialougeListTestActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Context mContext = this;
final Context context = this;
Button button;
button = (Button) findViewById(R.id.button01);
// add button listener
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.list);
dialog.setTitle("List");
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.TextView01);
text.setText("Test");
Button dialogButton = (Button) dialog.findViewById(R.id.Button01);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
});
}
}
My custom dialouge .xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="#+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="Back" />
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="TextView" />
<ListView
android:id="#+id/listview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/Button01"
android:layout_alignParentLeft="true"
android:layout_below="#+id/TextView01" >
</ListView>
</RelativeLayout>
Try something like this:
ArrayList<String> myStringArray = ... // insert your code to get/create the array here
ListView view = dialog.findViewById(R.id.listview1);
ArrayAdapter<String> listAdapter = new ArrayAdapter<String> ( this, android.R.layout.simple_list_item_1, myStringArray);
view.setAdapter(listAdapter);
Like so:
public class CustomDialougeListTestActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Context mContext = this;
final Context context = this;
Button button;
ArrayList<String> myStringArray = ... // insert your code to get/create the array here
ArrayAdapter<String> listAdapter = new ArrayAdapter<String> ( this, android.R.layout.simple_list_item_1, myStringArray);
button = (Button) findViewById(R.id.button01);
// add button listener
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.list);
dialog.setTitle("List");
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.TextView01);
text.setText("Test");
ListView view = dialog.findViewById(R.id.listview1);
view.setAdapter(listAdapter);
Button dialogButton = (Button) dialog.findViewById(R.id.Button01);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
});
}
}

Categories

Resources