I am working on a bluetooth application but my application crash .Here is my MainActivity.java
package com.race_gurram.bluetoothadapter;
import java.util.ArrayList;
import java.util.Set;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button on, off, get, bring;
private BluetoothAdapter BA;
private Set<BluetoothDevice> pairedDevices;
private ListView lv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
on = (Button) findViewById(R.id.on);
off = (Button) findViewById(R.id.off);
get = (Button) findViewById(R.id.get);
bring = (Button) findViewById(R.id.list);
lv = (ListView) findViewById(R.id.listView1);
BA = BluetoothAdapter.getDefaultAdapter();
}
public void on(View view)
{
if(!BA.isEnabled())
{
Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnOn,0);
Toast.makeText(getApplicationContext(), "Turned On", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getApplicationContext(), "Already on", Toast.LENGTH_SHORT).show();
}
}
public void list(View view)
{
pairedDevices = BA.getBondedDevices();
ArrayList list = new ArrayList();
for(BluetoothDevice bt : pairedDevices)
list.add(BA.getName());
Toast.makeText(getApplicationContext(), "Searching for devices", Toast.LENGTH_SHORT).show();
final ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,list);
lv.setAdapter(adapter);
}
public void get()
{
Intent getVisible = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(getVisible,0);
}
public void off()
{
BA.disable();
Toast.makeText(getApplicationContext(), "Turned off", Toast.LENGTH_SHORT).show();
}
#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, menu);
return true;
}
}
and this is my activity.xml file
<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: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=".MainActivity" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/on"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="on"
android:text="#string/on" />
<Button
android:id="#+id/get"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="get"
android:text="#string/get" />
<Button
android:id="#+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="list"
android:text="#string/bring" />
<Button
android:id="#+id/off"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="off"
android:text="#string/off" />
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible" >
</ListView>
</LinearLayout>
</ScrollView>
</RelativeLayout>
Whenever I onclick on a button the application crashes... Please help me out with this .. thank in advance.
On get and off method, there should be a View parameter, right?
public void get(View v) {
Intent getVisible = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(getVisible,0);
} public void off(View v) {
BA.disable();
Toast.makeText(getApplicationContext(), "Turned off", Toast.LENGTH_SHORT).show(); }
Perhaps it is when you click the list button. Assuming that the phone has 0 bonded devices,
paired.getBondedDevices() will probably return null and the program crashes due to a NullPointerException.
pairedDevices = BA.getBondedDevices();
ArrayList list = new ArrayList();
if(pairedDevices != null) {
for(BluetoothDevice bt : pairedDevices)
list.add(BA.getName());
}
Related
I am trying to make a simple Text to Speech application but it is throwing an error and thus, as a result, the toast is appearing when the button is clicked instead of getting TextToSpeech.SUCCESS as the int status.
Here is the code I used:
package com.example.notifyme;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
EditText txt;
Button b;
String s;
TextToSpeech tts;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = findViewById(R.id.input);
b = findViewById(R.id.button);
}
public void notify(View view) {//for creating tts activity
tts = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
#Override
public void onInit(int status) {
s = txt.getText().toString();
if (status == TextToSpeech.SUCCESS) {
tts.setLanguage(Locale.ENGLISH);
tts.setSpeechRate(1.0f);
tts.speak(s, TextToSpeech.QUEUE_FLUSH, null, null);
Toast.makeText(getApplicationContext(),"Here it is",Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Something unexpected happened", Toast.LENGTH_SHORT).show();
}
}
});
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onPause();
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<EditText
android:id="#+id/input"
android:layout_width="330dp"
android:layout_height="72dp"
android:layout_marginTop="156dp"
android:ems="10"
android:hint="#string/add_your_text_here"
android:gravity="start|top"
android:layout_marginBottom="50dp"
android:inputType="textMultiLine"
app:layout_constraintBottom_toTopOf="#+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.493"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button"
android:layout_width="114dp"
android:layout_height="55dp"
android:backgroundTint="#4CAF50"
android:text="#string/speak"
android:textAllCaps="true"
android:onClick="notify"
android:textFontWeight="40"
app:layout_constraintTop_toBottomOf="#+id/input"
android:layout_marginTop="30dp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="266dp" />
</androidx.constraintlayout.widget.ConstraintLayout/>
Kindly let me know what is the mistake.
I am working on an Android app with Tab navigation. I have three tabs and I would like to display separate informations on each tab. On the first tab, I would like to display a list of items that are retrieved from an SQlite database. The items are entered through dialogs, which is working well. I keep the logic for the collection and display of data in the Main Activity:
package com.example.TodoList;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.app.AlertDialog;
import android.widget.SimpleCursorAdapter;
import android.database.sqlite.SQLiteDatabase;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.example.TodoList.db.TaskContract;
import com.example.TodoList.db.TaskDBHelper;
import com.example.TodoList.fragments.ThreeFragment;
import com.example.TodoList.fragments.TwoFragment;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private ListAdapter listAdapter;
private TaskDBHelper helper;
private Button btnIconTextTabs;
private TabLayout tabLayout;
private ViewPager viewPager;
private int[] tabIcons = {
R.drawable.ic_tab_favourite,
R.drawable.ic_tab_call,
R.drawable.ic_tab_contacts
};
private void setupTabIcons() {
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
tabLayout.getTabAt(2).setIcon(tabIcons[2]);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_icon_text_tabs);
//ListView listView = (ListView) findViewById(R.id.list);
//listView.setAdapter(listAdapter);
updateUI();
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
btnIconTextTabs = (Button) findViewById(R.id.btnIconTextTabs);
//btnIconTextTabs.setOnClickListener(this);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new Fragment(), "ONE");
adapter.addFrag(new TwoFragment(), "TWO");
adapter.addFrag(new ThreeFragment(), "THREE");
viewPager.setAdapter(adapter);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final EditText inputField = new EditText(this);
builder.setNegativeButton("Cancel", null);
switch (item.getItemId()) {
case R.id.action_add_task:
builder.setTitle("Add an article to your shopping list");
builder.setMessage("What would you like to add?");
builder.setView(inputField);
builder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
String task = inputField.getText().toString();
helper = new TaskDBHelper(MainActivity.this);
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues values = new ContentValues();
values.clear();
values.put(TaskContract.Columns.TASK, task);
db.insertWithOnConflict(TaskContract.TABLE, null, values, SQLiteDatabase.CONFLICT_IGNORE);
updateUI();
}
});
builder.create().show();
return true;
case R.id.action_remove_task:
builder.setTitle("Remove an article from the shopping list");
builder.setMessage("Did you found this article?");
builder.setNegativeButton("Remove", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
String task = inputField.getText().toString();
helper = new TaskDBHelper(MainActivity.this);
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues values = new ContentValues();
values.clear();
updateUI();
}
});
case R.id.action_show_mylocation:
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Log.d("MyTagGoesHere", "This is my log message at the debug level here");
//Intent intent=new Intent(this,LbsGeocodingActivity.class);
//startActivity(intent);
Intent GeoLocationIntent = new Intent(MainActivity.this, GeoActivity.class);
//myIntent.putExtra("key", value); //Optional parameters
MainActivity.this.startActivity(GeoLocationIntent);
}
builder.create().show();
return true;
}
private void updateUI() {
helper = new TaskDBHelper(MainActivity.this);
SQLiteDatabase sqlDB = helper.getReadableDatabase();
Cursor cursor = sqlDB.query(TaskContract.TABLE,
new String[]{TaskContract.Columns._ID, TaskContract.Columns.TASK},
null, null, null, null, null);
listAdapter = new SimpleCursorAdapter(
this,
R.layout.task_view,
cursor,
new String[]{TaskContract.Columns.TASK},
new int[]{R.id.taskTextView},
0
);
}
public void onDoneButtonClick(View view) {
View v = (View) view.getParent();
TextView taskTextView = (TextView) v.findViewById(R.id.taskTextView);
String task = taskTextView.getText().toString();
String sql = String.format("DELETE FROM %s WHERE %s = '%s'",
TaskContract.TABLE,
TaskContract.Columns.TASK,
task);
helper = new TaskDBHelper(MainActivity.this);
SQLiteDatabase sqlDB = helper.getWritableDatabase();
sqlDB.execSQL(sql);
updateUI();
}
public void onSubmitPriceClick(View view) {
Intent SubmitPriceIntent = new Intent(MainActivity.this, SubmitPriceActivity.class);
MainActivity.this.startActivity(SubmitPriceIntent);
}
public void onWebViewButtonClick(View view) {
Intent intent = new
Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.batprice.com:1337"));
startActivity(intent);
finish();
}
public void onGeoLocationButtonClick(View view) {
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Log.d("MyTagGoesHere", "This is my log message at the debug level here");
Intent GeoLocationIntent = new Intent(MainActivity.this, GeoActivity.class);
MainActivity.this.startActivity(GeoLocationIntent);
}
/*#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnIconTextTabs:
startActivity(new Intent(MainActivity.this, IconTextTabsActivity.class));
break;
}
}*/
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
This is my First fragment, where I want to display the list items:
package com.example.TodoList.fragments;
import android.content.Context;
import android.database.SQLException;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import com.example.TodoList.R;
import java.util.ArrayList;
public class OneFragment extends FragmentActivity {
public OneFragment() {
// Required empty public constructor
}
private class mylocationlistener implements LocationListener {
#Override
public void onLocationChanged(Location location) {
if (location != null) {
Log.d("LOCATION CHANGED", location.getLatitude() + "");
Log.d("LOCATION CHANGED", location.getLongitude() + "");
Toast.makeText(OneFragment.this,
location.getLatitude() + "" + location.getLongitude(),
Toast.LENGTH_LONG).show();
}
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener ll = new mylocationlistener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
return inflater.inflate(R.layout.fragment_one, container, false);
}
/*
public static class OneFragment extends Fragment {
ListView list;
list = (ListView) view.findViewById(R.id.listview);
DataDB data = new DataDB();
ArrayAdapter<String> listAdapter;
public ListDoctorFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.listdoctor, container, false);
ArrayList<String> names = new ArrayList<String>();
try {
names = data.getDoctorlistDB(getActivity());
} catch (SQLException e) {
e.printStackTrace();
}
listAdapter = new ArrayAdapter<String>(getActivity(), R.layout.support_simple_spinner_dropdown_item, names);
// set the adapter
list.setAdapter(listAdapter);
return view;
}
}
*/
}
This is the activity icon text tabs layout file:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMode="fixed" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
This is my main activity layout file:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
android:orientation="vertical">
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="350dp"
android:layout_margin="5dp" />
<Button
android:id="#+id/btnIconTextTabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/btn_icon_text_tabs"
android:textSize="15dp" />
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
This is my main layout file:
<?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">
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="350dp"
android:layout_margin="5dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="clip_vertical"
android:onClick="onGeoLocationButtonClick"
android:text="Your Location" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="clip_vertical"
android:onClick="onSubmitPriceClick"
android:text="Submit a price" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="clip_vertical"
android:onClick="onScrollViewButtonClick"
android:text="Scrollview" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="clip_vertical"
android:onClick="onWebViewButtonClick"
android:text="Webview" />
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
This is my Fragment one layout file:
<?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">
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="350dp"
android:layout_margin="5dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="clip_vertical"
android:onClick="onGeoLocationButtonClick"
android:text="Your Location" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="clip_vertical"
android:onClick="onSubmitPriceClick"
android:text="Submit a price" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="clip_vertical"
android:onClick="onScrollViewButtonClick"
android:text="Scrollview" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="clip_vertical"
android:onClick="onWebViewButtonClick"
android:text="Webview" />
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
I would like to display the entered list items - this whole processus is working well - in fragment One, but the list items are not showing up. I don´t get any errors so I´m a bit stuck here. Any help or hints would be very appreciated, thanks.
I'm using fragment.replace to lunch a list of user in a chat application using parse and sinch,at the begin i'm using views but now I'm changing to fragments, but the place just append instead of replace the fragment. I'm using android.support.v4.app.Fragment,FragmentManager and FragmentTransaction. What am I doing wrong .
Here's my class:
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.app.Activity;
import android.os.Bundle;
import com.parse.LogInCallback;
import com.parse.Parse;
import com.parse.ParseException;
import com.parse.ParseUser;
import com.parse.SignUpCallback;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.paul.mapasxavo.ListUsersActivity;
import com.example.paul.mapasxavo.R;
public class ChatActivity extends Fragment {
private Button signUpButton;
private Button loginButton;
private EditText usernameField;
private EditText passwordField;
private String username;
private String password;
private Intent intent;
private Intent serviceIntent;
Context thiscontext;
private final String TAG="ChatActivity";
public ChatActivity(){
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (currentUser != null) {
}
}
#Override
public void onDestroy() {
super.onDestroy();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_chat, container, false);
loginButton = (Button) rootView.findViewById(R.id.loginButton);
signUpButton = (Button) rootView.findViewById(R.id.signupButton);
usernameField = (EditText) rootView.findViewById(R.id.loginUsername);
passwordField = (EditText) rootView.findViewById(R.id.loginPassword);
loginButton = (Button) rootView.findViewById(R.id.loginButton);
signUpButton = (Button) rootView.findViewById(R.id.signupButton);
usernameField = (EditText) rootView.findViewById(R.id.loginUsername);
passwordField = (EditText) rootView.findViewById(R.id.loginPassword);
loginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
username = usernameField.getText().toString();
password = passwordField.getText().toString();
ParseUser.logInInBackground(username, password, new LogInCallback() {
public void done(ParseUser user, com.parse.ParseException e) {
if (user != null) {
ListUsersActivity listUsersActivity = new ListUsersActivity();
FragmentManager fragmentManager= getFragmentManager();
FragmentTransaction fragmentTransaction= fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.listados,listUsersActivity);
fragmentTransaction.commit();
Log.v(TAG,"boton login");
} else {
Toast.makeText(getActivity(),
getString(R.string.login_error),
Toast.LENGTH_LONG).show();
}
}
});
}
});
signUpButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
username = usernameField.getText().toString();
password = passwordField.getText().toString();
ParseUser user = new ParseUser();
user.setUsername(username);
user.setPassword(password);
user.signUpInBackground(new SignUpCallback() {
public void done(com.parse.ParseException e) {
if (e == null) {
ListUsersActivity listUsersActivity = new ListUsersActivity();
FragmentManager fragmentManager= getFragmentManager();
FragmentTransaction fragmentTransaction= fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.listados,listUsersActivity);
fragmentTransaction.commit();
Log.v(TAG,"boton registrar");
} else {
Toast.makeText(getActivity(),
getString(R.string.sigin_error)
, Toast.LENGTH_LONG).show();
}
}
});
}
});
return rootView;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
#Override
public void onDetach() {
super.onDetach();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
My fragment is this:
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.paul.mapasxavo.ChatActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:id="#+id/listados"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="#string/txt_usuario"
android:id="#+id/loginUsernameText"
android:layout_gravity="center_horizontal"
android:textSize="20sp"/>
<EditText
android:id="#+id/loginUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:inputType="text"
android:padding="10dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp">
<requestFocus/>
</EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="#string/txt_password"
android:id="#+id/loginPasswordText"
android:layout_gravity="center_horizontal"
android:textSize="20sp"/>
<EditText
android:id="#+id/loginPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="40dp"
android:inputType="textPassword"
android:padding="10dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp">
</EditText>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginBottom="40dp"
android:layout_gravity="center">
<Button
style="#style/botones"
android:id="#+id/loginButton"
android:layout_width="168dp"
android:layout_height="wrap_content"
android:text="Login"
/>
<Button
style="#style/botones"
android:id="#+id/signupButton"
android:layout_width="163dp"
android:layout_height="wrap_content"
android:text="Registrar"
/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
My replace fragment is this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.paul.mapasxavo.ListUsersActivity">
<Button
style="#style/botones"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/logout"
android:id="#+id/logoutButton"
android:gravity="center_vertical|center_horizontal"
android:layout_gravity="center_horizontal"
android:textSize="24sp"
android:padding="15dp"
/>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:background="#color/colorPrimary"
android:id="#+id/usersListView">
</ListView>
</LinearLayout>
Please give a hint.
Thanks a lot
Searchin inside the forum, found some anwers, based on this [link][Android Replace Fragment within a Fragment. I could make it work making some changes in fragment transaction and xml.
Chat Activity:
loginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
username = usernameField.getText().toString();
password = passwordField.getText().toString();
ParseUser.logInInBackground(username, password, new LogInCallback() {
public void done(ParseUser user, com.parse.ParseException e) {
if (user != null) {
FragmentManager fragmentManager = myContext.getSupportFragmentManager();
//start transaction
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ListUsersActivity listUsersActivity = new ListUsersActivity();
fragmentTransaction.addToBackStack("xyz"); fragmentTransaction.replace(R.id.listados,listUsersActivity);
fragmentTransaction.commit();
serviceIntent = new Intent(getActivity().getApplicationContext(), MessageService.class);
getActivity().startService(serviceIntent);
} else {
Toast.makeText(getActivity(),
getString(R.string.login_error),
Toast.LENGTH_LONG).show();
}
}
);
}
});
activity_chat.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"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:id="#+id/listados"
tools:context="com.example.paul.mapasxavo.ChatActivity">
Now it show as a a new layer over the last one.
I'm trying to create a list of Card view using a custom adapter. I have defined the layout of a single row of list, consisting a card view and imageview/textviews in it, in a separate .xml file. I'm using a custom srrsy adapter. My app crashes when I try to open the activity having list view , giving only an Runtime-exception error.
Error:
AndroidRuntime(2583): at graph.prathya.com.nextstepz.CustomAdapters.PostArrayAdapter.getView(PostArrayAdapter.java:44)
This error is at the line
LayoutInflater li =(LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
in custom adapter.
Here is single_card_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/ll1">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:id="#+id/ll2">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_margin="10dp"
android:id="#+id/imgIcon"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/ll3"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SampleTitle1"
android:layout_marginTop="10dp"
android:background="#00b5ad"
android:textSize="20dp"
android:gravity="center"
android:padding="5dp"
android:id="#+id/title"
android:textColor="#ffffff"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Sampledisription1"
android:layout_marginTop="10dp"
android:background="#ffffff"
android:textSize="10dp"
android:gravity="center"
android:padding="5dp"
android:id="#+id/desciption"
/>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
Here is PostArrayAdapter.java
package graph.prathya.com.nextstepz.CustomAdapters;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import graph.prathya.com.nextstepz.R;
/**
* Created by Prathya on 5/23/2015.
*/
public class PostArrayAdapter extends ArrayAdapter<Post>{
Context context;
Post data[] =null;
int layoutid;
public PostArrayAdapter(Context context, int layoutid, Post data[]) {
super(context,layoutid);
this.data=data;
this.layoutid=layoutid;
}
private class PostHolder{
ImageView imgIcon;
TextView title,description;
}
#Override
public int getCount(){
return data.length;
}
#Override
public View getView(int Position, View convertView, ViewGroup parent){
PostHolder holder;
View v = convertView;
if(v==null){
LayoutInflater li = LayoutInflater.from(context);/* RunTimeExceptio error at this line of code */
v= li.inflate(layoutid,parent,false);
holder = new PostHolder();
holder.imgIcon = (ImageView)v.findViewById(R.id.imgIcon);
holder.title = (TextView)v.findViewById(R.id.title);
holder.description= (TextView)v.findViewById(R.id.desciption);
v.setTag(holder);
}
else {
holder = (PostHolder)v.getTag();
}
Post post = data[Position];
holder.imgIcon.setImageResource(post.imgIcon);
holder.title.setText(post.title);
holder.description.setText(post.description);
return v;
}
}
Here is activity in which listView lies: HomeScreenAvtivity.java
package graph.prathya.com.nextstepz;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.ListView;
import graph.prathya.com.nextstepz.Communicator.Communicater1;
import graph.prathya.com.nextstepz.CustomAdapters.Post;
import graph.prathya.com.nextstepz.CustomAdapters.PostArrayAdapter;
public class HomeScreenActivity extends ActionBarActivity {
ImageButton passionbtn, eventbtn, projectbtn, groupstudybtn;
Dialog dg;
ListView listView = null;
Post post[] =new Post[] {
new Post(R.drawable.img1,"Cats and Children","Cats can be a fascinating experience for children, but young minds can sometimes confuse a pet for a toy. Teach children how to respect and properly handle your cat for best results."),
new Post(R.drawable.img2,"Android:The Best OS","Android powers hundreds of millions of mobile devices in more than 190 countries around the world.Android’s openness has made it a favorite for consumers."),
new Post(R.drawable.img3,"Beautiful","Monica is an Italian actor and model who started her modelling career at the age of 13 by posing for a local photo enthusiast."),
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);
listView = (ListView)findViewById(R.id.homelist);
listView.setAdapter(new PostArrayAdapter(getApplicationContext(),R.layout.single_card_view,post));
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
if (item.getItemId() == R.id.nxtsignupitem) {
Intent in = new Intent(getApplicationContext(), PostActivity.class);
startActivity(in);
}
if (item.getItemId() == R.id.eventitem1) {
Intent in = new Intent(getApplicationContext(), PostDetailActivity.class);
startActivity(in);
}
if (item.getItemId() == R.id.postitem11) {
dg = new Dialog(HomeScreenActivity.this);
dg.requestWindowFeature(Window.FEATURE_NO_TITLE);
dg.setContentView(R.layout.fragment_new_post_dialogue);
dg.setTitle("Please choose One option");
dg.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dg.show();
passionbtn = (ImageButton) dg.findViewById(R.id.imageButton1st);
eventbtn = (ImageButton) dg.findViewById(R.id.imageButton2);
projectbtn = (ImageButton) dg.findViewById(R.id.imageButton3);
groupstudybtn = (ImageButton) dg.findViewById(R.id.imageButton4);
passionbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Communicater1.setpostButtonid(1);
Intent in = new Intent(getApplicationContext(), PostActivity.class);
startActivity(in);
}
});
eventbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Communicater1.setpostButtonid(2);
Intent in = new Intent(getApplicationContext(), PostActivity.class);
startActivity(in);
}
});
projectbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Communicater1.setpostButtonid(3);
Intent in = new Intent(getApplicationContext(), PostActivity.class);
startActivity(in);
}
});
groupstudybtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Communicater1.setpostButtonid(4);
Intent in = new Intent(getApplicationContext(), PostActivity.class);
startActivity(in);
}
});
}
return super.onOptionsItemSelected(item);}
#Override
public boolean onCreateOptionsMenu(Menu menu){
// TODO Auto-generated method stub
MenuInflater mi = getMenuInflater();
mi.inflate(R.menu.menu_home_screen, menu);
return super.onCreateOptionsMenu(menu);
} }
XML layout file of HomeScreenActivity
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/DrawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/lord"
android:layout_marginBottom="20dp"
/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/homelist">
</ListView>
</LinearLayout>
</ScrollView>
Post.java used in Adapter
package graph.prathya.com.nextstepz.CustomAdapters;
/**
* Created by Prathya on 5/23/2015.
*/
public class Post {
int imgIcon;
String title,description;
public Post(int imgIcon,String title, String description){
this.imgIcon=imgIcon;
this.title=title;
this.description=description;
}
}
you never assign context in your constructor.
add
this.context = context;
to your constructor
My Android listview does not update with notifydatasetchanged() call.
The Main Code Activity:
package com.example.jokesbook;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.jokesbook.MESSAGE";
CustomAdapter Adapter;
ListView lv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
JokeDB.jokesList = new ArrayList<Joke>();
JokeDB.jokesList.add(new Joke("DDD"));
lv = (ListView)findViewById(android.R.id.list);
Adapter= new CustomAdapter(MainActivity.this, R.layout.joke_list_item, JokeDB.jokesList);
lv.setAdapter(Adapter);
}//onCreate
#Override
public void onResume() {
super.onResume(); // Always call the superclass method first
Log.d("jokesbook", "onResume ");
Adapter.notifyDataSetChanged();
}
class CustomAdapter extends ArrayAdapter<Joke>{
Context context;
int layoutResourceId;
ArrayList<Joke> data = null;
private LayoutInflater mInflater;
public CustomAdapter(Context customAdapter, int layoutResourceId, ArrayList<Joke> data) {
super(customAdapter, layoutResourceId, data);
Log.d("jokesbook", "CustomAdapter ");
this.layoutResourceId = layoutResourceId;
this.context = customAdapter;
this.data = data;
this.mInflater = LayoutInflater.from(customAdapter);
}
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
//item_list
convertView = mInflater.inflate(R.layout.joke_list_item, null);
holder = new ViewHolder();
//fill the views
holder.joke = (TextView) convertView.findViewById(R.id.ListTextView1);
convertView.setTag(holder);
}
else {
// Get the ViewHolder back to get fast access to the TextView
// and the ImageView.
holder = (ViewHolder) convertView.getTag();//
}
holder.joke.setText(data.get(position).jokeStr);
return convertView;
}
class ViewHolder {
TextView joke;
}
}
/** Called when the user clicks the Add a new joke button */
public void goNewJoke(View view) {
Intent intent = new Intent(this, New_joke.class);
startActivity(intent);
}
}
Its xml:
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:id="#+id/ButtonAboveList"
android:layout_width="500dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:onClick="goNewJoke"
android:text="#string/Add_a_new_joke"/>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
When adding content to JokeDB.jokesList in another activity that its code is:
package com.example.jokesbook;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class New_joke extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_joke);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.new_joke, menu);
return true;
}
public void addJoke(View view){
EditText editTextJoke= (EditText)findViewById(R.id.edit_text_jokeToAdd);
JokeDB.jokesList.add(new Joke(editTextJoke.getText().toString()));
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
}
and its XML is:
<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:background="#color/green"
android:orientation="vertical"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:paddingTop="3dp"
tools:context=".New_joke" >
<EditText
android:id="#+id/edit_text_jokeToAdd"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_marginBottom="40dp"
android:background="#color/white"
android:hint="#string/edit_message" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="horizontal"
tools:context=".New_joke" >
<TextView
android:id="#+id/AuthorText"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="left"
android:text="#string/Author"
android:textColor="#android:color/white"
android:textSize="25sp" />
<EditText
android:id="#+id/edit_text_Author"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="#color/white"
android:hint="#string/Only_letters_allowed"
android:inputType="textPersonName" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="60dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="horizontal"
tools:context=".New_joke" >
<TextView
android:id="#+id/DateText"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="left"
android:text="#string/Date"
android:textColor="#android:color/white"
android:textSize="25sp" />
<EditText
android:id="#+id/edit_text_Date"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="#color/white"
android:hint="#string/Only_digits_allowed"
android:inputType="number" />
</LinearLayout>
<Button
android:id="#+id/ButtonAboveList"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:onClick="addJoke"
android:text="#string/Add" />
</LinearLayout>
I cannot see the updated list in the main activity eveb though in onResume I used notifydatasetchanged() function.
Try This....
#Override
public void onResume() {
super.onResume(); // Always call the superclass method first
Log.d("jokesbook", "onResume ");
notifyDataSetChanged();
}