I want to make it so that a user clicks a button to launch a dialog. It crashes when I click the button. Here's the logcat:
nullpointerexception at com.shoppinglist.ShoppingList$1$2.onClick(ShoppingList.java:50) at android.view.View.performClick(View.java:2344) at android.view.View.onTouchEvent(View.java:4133) at android.widget.TextView.onTouchEvent(TextView.java:6510) at android.view.View.dispatchTouchEvent(View.java:3672) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
Here's my .java file:
package com.shoppinglist;
import java.util.ArrayList;
import android.app.Dialog;
import android.app.ListActivity;
import android.os.Bundle;
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 ShoppingList extends ListActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText et = (EditText) findViewById(R.id.edittext);
ListView lv = (ListView) findViewById(android.R.id.list);
final ArrayList<String> items = new ArrayList<String>();
final ArrayAdapter<String> adapter;
adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_list_item_1, items);
lv.setAdapter(adapter);
Button button1main = (Button) findViewById(R.id.add);
button1main.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
final Dialog additem = new Dialog(ShoppingList.this);
additem.setContentView(R.layout.maindialog);
additem.setTitle("Type your item");
additem.setCancelable(true);
et.setHint("Type the name of an item...");
Button button = (Button) findViewById(R.id.cancel);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
additem.dismiss();
}
});
additem.show();
Button ok = (Button) findViewById(R.id.ok);
ok.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
items.add(et.getText().toString());
adapter.notifyDataSetChanged();
additem.dismiss();
et.setText("");
}
}
);
}
});
}
}
Here's my main.xml:
<?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:background="#FF9900"
android:padding="10dp"
android:orientation="vertical">
<Button
android:id="#+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add item..."
android:layout_gravity="center"/>
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFCC00"/>
</LinearLayout>
Here's my dialog xml:
<?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:id="#+id/layout_root"
android:orientation="vertical"
android:background="#FFFFFF"
android:minHeight="100dp"
android:minWidth="300dp">
<EditText
android:id="#+id/edittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:id="#+id/button_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:layout_gravity="bottom">
<Button
android:id="#+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ok"/>
<Button
android:id="#+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"/>
</LinearLayout>
</LinearLayout>
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText et = (EditText) findViewById(R.id.edittext);
ListView lv = (ListView) findViewById(android.R.id.list);
final ArrayList<String> items = new ArrayList<String>();
final ArrayAdapter<String> adapter;
adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_list_item_1, items);
lv.setAdapter(adapter);
Button button1main = (Button) findViewById(R.id.add);
In your code, you use setContentView to R.layout.main. Then you try to find R.id.edittext in the R.layout.main but edittext exists in maindialog.xml.
So when you call
final EditText et = (EditText) findViewById(R.id.edittext);
et will be null and any method call on et will result of a NullPointerException
Edit
Try this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView lv = (ListView) findViewById(android.R.id.list);
final ArrayList<String> items = new ArrayList<String>();
final ArrayAdapter<String> adapter;
adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_list_item_1, items);
lv.setAdapter(adapter);
Button button1main = (Button) findViewById(R.id.add);
button1main.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
final Dialog additem = new Dialog(ShoppingList.this);
additem.setContentView(R.layout.maindialog);
final EditText et = (EditText)additem.findViewById(R.id.edittext);
additem.setTitle("Type your item");
additem.setCancelable(true);
et.setHint("Type the name of an item...");
Button button = (Button) additem.findViewById(R.id.cancel);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
additem.dismiss();
}
});
additem.show();
Button ok = (Button) additem.findViewById(R.id.ok);
ok.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
items.add(et.getText().toString());
adapter.notifyDataSetChanged();
additem.dismiss();
et.setText("");
}
});
}
});
If I've got my line numbers correct, the null pointer exception is occurring in this block:
new OnClickListener() {
#Override
public void onClick(View v) {
items.add(et.getText().toString());
adapter.notifyDataSetChanged();
additem.dismiss();
et.setText("");
}
The error is saying that one of the variables you are accessing is null. In this function you are accessing et, et.getText(), items, adapter, and additem. I'd suggest debugging this section (through eclipse, or printing log statements) to determine which item is null.
Related
I need to use a .java file to perform actions for a .xml layout. I have used this layout for a dialog box. I have linked the .java file to that layout but it doesn't seem to work at all.
This is the MainActivity
package com.example.ayush.projectfive;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button btn;
AlertDialog.Builder alrt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
alrt = new AlertDialog.Builder(MainActivity.this);
alrt.setIcon(R.mipmap.ic_launcher);
alrt.setTitle("Login");
alrt.setCancelable(false);
alrt.setView(R.layout.mylayout);
alrt.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
alrt.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alrt.show();
Intent i = new Intent(MainActivity.this,Second.class);
startActivity(i);
}
});
}
}
This is the activity_main
<?xml version="1.0" encoding="utf-8"?>
<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"
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="com.example.ayush.projectfive.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EXIT"
android:id="#+id/button"
android:layout_gravity="center_horizontal" />
This is the dialog box layout
<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="LOGIN"
android:id="#+id/textView"
android:layout_gravity="center_horizontal" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:layout_marginTop="20dp"
android:id="#+id/editText" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:layout_marginTop="15dp"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/editText2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:layout_marginTop="20dp"
android:id="#+id/button3" />
This is the .java file I'd like to link with.
package com.example.ayush.projectfive;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Second extends AppCompatActivity{
EditText et, et2;
Button btn2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout);
et = (EditText) findViewById(R.id.editText);
et2 = (EditText) findViewById(R.id.editText2);
btn2 = (Button) findViewById(R.id.button3);
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String s1 = et.getText().toString();
String s2 = et.getText().toString();
if(s1.equals(s2)){
Toast.makeText(Second.this, "Login Successful", Toast.LENGTH_SHORT).show();
Intent i = new Intent(Second.this,Third.class);
startActivity(i);
}
else{
}
}
});
}
}
This is the .java file I'd like to link with [...]
public class Second extends AppCompatActivity{
Okay... That's what this code does
Intent i = new Intent(MainActivity.this,Second.class);
startActivity(i);
If you are trying to use the views that are contained within the dialog box and respond to the buttons there to login, then you need to remove the button from the activity, add alrt.show() in the onCreate instead of onClick and move the Activity start code into the dialog's onClick handlers.
It's also worth mentioning that the object is a builder, so you can do this
View dialogView = LayoutInflater.from(MainActivity.this).inflate(R.layout.mylayout, null);
final EditText editUsername = dialogView.findViewById(R.id.editText);
// TODO: Get other views from mylayout.xml
new AlertDialog.Builder(MainActivity.this)
.setIcon(R.mipmap.ic_launcher)
.setTitle("Login")
.setCancelable(false)
.setView(dialogView)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
String username = editUsername.getText().toString();
// TODO: Get more variables
// TODO: Verify credentials
Intent i = new Intent(MainActivity.this,Second.class);
startActivity(i);
}
})
.setNegativeButton("CANCEL", null)
.show();
And, as stated earlier, remove btn.setOnClickListener
just add alrt.show(); in your MainActivity.java e.g. here:
public class MainActivity extends AppCompatActivity {
Button btn;
AlertDialog.Builder alrt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
alrt = new AlertDialog.Builder(MainActivity.this);
alrt.setIcon(R.mipmap.ic_launcher);
alrt.setTitle("Login");
alrt.setCancelable(false);
alrt.setView(R.layout.mylayout);
alrt.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
alrt.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
alrt.show();
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,Second.class);
startActivity(i);
}
});
}
}
I would like to create checkbox with text that would behave something like what i have below in pseudo code:
Set in text in EditText field
if Button clicked:
create new Checkbox with entered text.
How do i go about doing that in Android?
So far ive got the button :) ... but dont know how to get new Checkbox with text in it ,if its clicked
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.text);
Button create = (Button) findViewById(R.id.button1);
create.setOnClickListener(this);
EditText textInCheckBox = (EditText) findViewById(R.id.editText1);
}
#Override
public void onClick(View v) {
// TODO Automatisch generierter Methodenstub
switch (v.getId()) {
case R.id.button1:
//after creating new checkbox with text in it
//Editbox should restet text field to null;
break;
}
}
}
Here is some code that will demonstrate how to add checkboxes dynamically with the title you entered into the EditText.
MainActivity.java
package com.example.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText txtTitle = (EditText) findViewById(R.id.txtTitle);
final Button btnCreate = (Button) findViewById(R.id.btnCreate);
final RadioGroup radContainer = (RadioGroup) findViewById(R.id.radContainer);
final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
btnCreate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String name = txtTitle.getText().toString().trim();
if (!name.isEmpty()) {
CheckBox checkBox = new CheckBox(MainActivity.this);
checkBox.setText(name);
checkBox.setLayoutParams(params);
radContainer.addView(checkBox);
} else {
Toast.makeText(MainActivity.this, "Enter a title for the checkbox", Toast.LENGTH_SHORT).show();
}
}
});
}
}
activity_main.xml
<?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="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="16dp" >
<EditText
android:id="#+id/txtTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone" />
<Button
android:id="#+id/btnCreate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Create Checkbox" />
<RadioGroup
android:id="#+id/radContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
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 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();
}
});
}
}
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) {
}
});