I follow the tutorial on Android Developer "Accessing Contacts" and implement step by step but i encounter a problem which through nullpointerexpection when i try to call the "setonitemclicklistener" inside the fragement. I tried number of solutions but i am not able to resolve the problem. Please help in resolving this issue. Thanks
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sharpapp.findloveone/com.mycompany.myapp.addfrndactivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2436)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2495)
at android.app.ActivityThread.access$900(ActivityThread.java:170)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1304)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5635)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.mycompany.myapp.addfrndactivity$PlaceholderFragment.onActivityCreated(addfrndactivity.java:111)
at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1794)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:977)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1136)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:548)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1177)
at android.app.Activity.performStart(Activity.java:5595)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2409)
Code Snippet
package com.mycompany.myapp;
import android.annotation.SuppressLint;
import android.database.Cursor;
import android.net.Uri;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.SimpleCursorAdapter;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.provider.ContactsContract;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.widget.AdapterView;
import android.widget.ListView;
public class addfrndactivity extends ActionBarActivity {
#SuppressLint("InlinedApi")
private final static String[] FROM_COLUMNS = {
Build.VERSION.SDK_INT
>= Build.VERSION_CODES.HONEYCOMB ?
ContactsContract.Contacts.DISPLAY_NAME_PRIMARY :
ContactsContract.Contacts.DISPLAY_NAME
};
private final static int[] TO_IDS = {
android.R.id.text1
};
static ListView mContactsList;
static long mContactId;
static String mContactKey;
static Uri mContactUri;
private static SimpleCursorAdapter mCursorAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_addfrndactivity);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_addfrndactivity, menu);
return true;
}
#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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment implements
LoaderManager.LoaderCallbacks<Cursor>,
AdapterView.OnItemClickListener{
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_addfrndactivity, container, false);
return rootView;
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getLoaderManager().initLoader(0, null, this);
// Gets the ListView from the View list of the parent activity
mContactsList = (ListView) getActivity().findViewById(android.R.id.list);
// Set the item click listener to be the current fragment.
mContactsList.setOnItemClickListener(this);
// Gets a CursorAdapter
mCursorAdapter = new SimpleCursorAdapter(
getActivity(),
R.layout.contacts_list_item,
null,
FROM_COLUMNS, TO_IDS,
0);
// Sets the adapter for the ListView
mContactsList.setAdapter(mCursorAdapter);
}
#SuppressLint("InlinedApi")
private final String[] PROJECTION =
{
ContactsContract.Contacts._ID,
ContactsContract.Contacts.LOOKUP_KEY,
Build.VERSION.SDK_INT
>= Build.VERSION_CODES.HONEYCOMB ?
ContactsContract.Contacts.DISPLAY_NAME_PRIMARY :
ContactsContract.Contacts.DISPLAY_NAME
};
private static final int CONTACT_ID_INDEX = 0;
private static final int LOOKUP_KEY_INDEX = 1;
// Defines the text expression
#SuppressLint("InlinedApi")
private final String SELECTION =
Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ?
ContactsContract.Contacts.DISPLAY_NAME_PRIMARY + " LIKE ?" :
ContactsContract.Contacts.DISPLAY_NAME + " LIKE ?";
// Defines a variable for the search string
private String mSearchString;
// Defines the array to hold values that replace the ?
private String[] mSelectionArgs = { mSearchString };
#Override
public void onItemClick(
AdapterView<?> parent, View item, int position, long rowID) {
// Get the Cursor
Cursor cursor = ((SimpleCursorAdapter) parent.getAdapter()).getCursor();
// Move to the selected contact
cursor.moveToPosition(position);
// Get the _ID value
mContactId = cursor.getLong(CONTACT_ID_INDEX);
// Get the selected LOOKUP KEY
mContactKey = cursor.getString(LOOKUP_KEY_INDEX);
// Create the contact's content Uri
mContactUri = ContactsContract.Contacts.getLookupUri(mContactId, mContactKey);
}
#Override
public Loader<Cursor> onCreateLoader(int loaderId, Bundle args) {
/*
* Makes search string into pattern and
* stores it in the selection array
*/
mSelectionArgs[0] = "%" + mSearchString + "%";
// Starts the query
return new CursorLoader(
getActivity(),
ContactsContract.Contacts.CONTENT_URI,
PROJECTION,
SELECTION,
mSelectionArgs,
null
);
}
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
// Put the result Cursor in the adapter for the ListView
mCursorAdapter.swapCursor(cursor);
}
#Override
public void onLoaderReset(Loader<Cursor> loader) {
// Delete the reference to the existing Cursor
mCursorAdapter.swapCursor(null);
}
}
}
activity_addfrndactivity.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.mycompany.myapp.addfrndactivity"
tools:ignore="MergeRootFrame" />
fragement_addfrndactivity.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="com.mycompany.myapp.addfrndactivity$PlaceholderFragment">
</RelativeLayout>
contact_list_view.xml
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
goto this layout :
R.layout.fragment_addfrndactivity
and make sure you have a ListView with this id : android.R.id.list
most probably you dont have
so change this line to your id in the layout xml file:
// Gets the ListView from the View list of the parent activity
mContactsList = (ListView) getActivity().findViewById(ID_FROM_YOUR_XML_FILE);
Related
This is my first app and i'm having some problem whit ListView
How can I get the ID in the database after clicking on a Item?
I can't use position because the Id may not be continuous and even if it is in order sometimes does not return the correct Id any.
this is my code, Thank you for your help:
import android.content.Intent;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
public class livello1 extends AppCompatActivity {
DatabaseHelper myDb;
Button next;
#Override
protected void onCreate(Bundle savedInstanceState) {
myDb = new DatabaseHelper(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.livello1);
populateListView();
registerClick();
}
private void registerClick(){
ListView list =(ListView)findViewById(R.id.listViewMain);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View viewClicked, int position, long id) {
Intent i = new Intent(livello1.this, bossi.note.Edit.class);
i.putExtra("id", position);
//i.putExtra("id", id);
startActivity(i);
}
});
}
private void populateListView(){
Cursor res = myDb.getAllData();
String[] myItems = new String[myDb.numRow()];
int cont = 0;
if(res.getCount() == 0){
// show message
return;
}
while( res. moveToNext()){
myItems[cont] = res.getString(1);
cont ++;
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.da_item, myItems);
ListView list =(ListView)findViewById(R.id.listViewMain);}
There are a lot of solution approaches.
For example, you can store of Id's if ArrayList before ListView initialization.
That is, execute "SELECT id FROM mytable"
Then store in arraylist and use while click method.
Example:
//in class declaration
private ArrayList<Long> ar_ids = new ArrayList<Long>;
//
String sql = "SELECT id FROM table";
Cursor cur = db.rawQuery(sql, null);
String out = "";
ArrayList<Long> ar_ids = new ArrayList<Long>;
if (cur.moveToFirst()) {
do {
ar.add(cur.getString(0));
} while (cur.moveToNext());
}else{
}
}
cur.close();
for click event:
#Override
public void onItemClick(AdapterView<?> parent, View viewClicked, int position, long id) {
Intent i = new Intent(livello1.this, bossi.note.Edit.class);
i.putExtra("id", ar_id.get(position));
//this is awsome!
startActivity(i);
}
and initialize this something like:
private void myfunc() {
String sql = "SELECT id FROM table";
Cursor cur = myDb.rawQuery(sql, null);
String out = "";
ArrayList<Long> ar_ids = new ArrayList<Long>;
if (cur.moveToFirst()) {
do {
ar.add(cur.getString(0));
} while (cur.moveToNext());
}else{
throw new NullPointerException();
}
}
cur.close();
}
private void populateListView(){
myfunc();
Cursor res = myDb.getAllData();
String[] myItems = new String[myDb.numRow()];
int cont = 0;
if(res.getCount() == 0){
// show message
return;
}
while( res. moveToNext()){
myItems[cont] = res.getString(1);
cont ++;
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.da_item, myItems);
ListView list =(ListView)findViewById(R.id.listViewMain);}
well instead of using the ArrayAdapter you can extend a BaseAdapter or ListAdapter or even the ArrayAdapter to make your custom adapter. First of all make a plain java class to map your database data including id to an object. Then make a custom BaseAdapter instead of using the ArrayAdapter. In your BaseAdapter class you have to override various methods including getItem and getItemId methods. Now you can return the whole mapped object from the getItem method and simply obtain the object of selected item of the ListView by using listView.getSelectedItem() method in the Activity or Fragment class. Once you get the object you can access the id easily.
You can find a good example and explanation of a custom adapter here
About mapping the database result to an object, you can get some concept here
Here, you can replace you code class with this one, I have added a cursorAdapter and attached it with your ListView, in adapter I have added a method for getting Id that I call from click listener to retrieve id from cursor, you will have to set column number for _ID in public void getId(int position) from MyCursorAdapter class below.
import android.content.Intent;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
public class livello1 extends AppCompatActivity {
DatabaseHelper myDb;
Button next;
private MyCursorAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
myDb = new DatabaseHelper(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.livello1);
populateListView();
registerClick();
}
private void registerClick(){
ListView list =(ListView)findViewById(R.id.listViewMain);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View viewClicked, int position, long id) {
Intent i = new Intent(livello1.this, bossi.note.Edit.class);
//retrieve id from cursor
int _id = adapter.getId(position)
i.putExtra("id", _id);
//i.putExtra("id", id);
startActivity(i);
}
});
}
private void populateListView(){
Cursor res = myDb.getAllData();
adapter = new MyCursorAdapter(this, res, 0);
ListView list =(ListView)findViewById(R.id.listViewMain);
list.setAdapter(adapter);
}
//adapter for list view
class MyCursorAdapter extends CursorAdapter {
Cursor cursor;
// Default constructor
public MyCursorAdapter(Context context, Cursor cursor, int flags) {
super(context, cursor, flags);
this.cursor = cursor;
}
public void bindView(View view, Context context, Cursor cursor) {
String text = cursor.getString(1);
//make sure the TextView id is "#+id/text1" in da_item.xml
TextView tvText = (TextView) view.findViewById(R.id.text1);
tvText.setText(text);
}
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflator inflater = (LayoutInflater) context.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
return Inflater.inflate(R.layout.da_item, parent, false);
}
public int getId(int position){
cursor.moveToPosition(position);
int colId = //set id column number here
int id = cursor.getLong(colId);
return id;
}
}
Since the code is untested, you might face a build issue in start, but it should give an idea of what's going on in code. feel free to ask any question if you face any problem in compilation
I have a listview on my mainactivity that is showing items from a database. I have an add button up in the action bar. When the add button is clicked on a dialog pops up and the user fills out fields for the new item and then they click "add" and it adds the item to the database. The only problem is that the listview on the mainactivity doesn't update to show the new item. I can close the app and reopen it and I see the new item. It just doesn't update immediatly. (Same problem with deleting an item, only the delete happens onLongPress)
Mainactivity.java
package blah.blah.blah
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
public class InitActivity extends FragmentActivity {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
// Gets the data repository in write mode
PlayersDBHelper mDbHelper = new PlayersDBHelper(getBaseContext());
SQLiteDatabase db = mDbHelper.getWritableDatabase();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_init);
// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}//End onCreate()
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.init, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_add:
showAddPlayerDialog();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void showAddPlayerDialog() {
// Create an instance of the dialog fragment and show it
DialogFragment dialog = new addPlayerDialog();
dialog.show(this.getFragmentManager(), "addPlayerFragment");
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
if(position == 0) {
Fragment playersFragment = new PlayersFragment();
return playersFragment;
} else if(position == 1){
Fragment otherFragment= new otherFragment();
return otherFragment;
} else {
Fragment otherFragment2 = new otherFragment2();
return otherFragment2;
}
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
}//End sectionsPagerAdapter()
public static class PlayersFragment extends Fragment {
public static final String ARG_SECTION_NUMBER = "section_number";
public int myFragmentId = 1;
private ListView mylistview;
private String[] values;
public ArrayAdapter<String> adapter;
public PlayersFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_players,
container, false);
mylistview = (ListView) rootView.findViewById(R.id.myListView);
registerForContextMenu(mylistview);
PlayersDBHelper mDbHelper = new PlayersDBHelper(rootView.getContext());
SQLiteDatabase db = mDbHelper.getReadableDatabase();
// Define a projection that specifies which columns from the database
// you will actually use after this query.
String[] projection = {
PlayerEntry._ID,
PlayerEntry.COLUMN_NAME_ID,
PlayerEntry.COLUMN_NAME_NAME,
PlayerEntry.COLUMN_NAME_POSITION
};
String selection = null; //Null will return all rows for given table
String[] selectionArgs = null; //Null should return all data
// How you want the results sorted in the resulting Cursor
String sortOrder =
PlayerEntry.COLUMN_NAME_NAME + " DESC";
Cursor c = db.query(
PlayerEntry.TABLE_NAME, // The table to query
projection, // The columns to return
selection, // The columns for the WHERE clause
selectionArgs, // The values for the WHERE clause
null, // don't group the rows
null, // don't filter by row groups
sortOrder // The sort order
);
values = new String[] {};
String array[] = new String[c.getCount()];
int i = 0;
c.moveToFirst();
while (c.isAfterLast() == false) {
array[i] = c.getString(c.getColumnIndexOrThrow(PlayerEntry.COLUMN_NAME_NAME));
i++;
c.moveToNext();
}
for(int x = 0; x < array.length ; x++){
Log.d("Logan", "Entry at:" + x + " is " + array[x]);
values = push(values, array[x]);
}
adapter = new ArrayAdapter<String>(this.getActivity(),
android.R.layout.simple_list_item_1, values);
mylistview.setAdapter(adapter);
return rootView;
}
private static String[] push(String[] array, String push) {
String[] longer = new String[array.length + 1];
for (int i = 0; i < array.length; i++)
longer[i] = array[i];
longer[array.length] = push;
return longer;
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
if (v.getId()==R.id.myListView) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
menu.setHeaderTitle(values[info.position]);
String[] menuItems = {"Edit", "Delete"};
for (int i = 0; i<menuItems.length; i++) {
menu.add(Menu.NONE, i, i, menuItems[i]);
}
}
}
#Override
public boolean onContextItemSelected(MenuItem item) {
PlayersDBHelper mDbHelper = new PlayersDBHelper(getActivity());
final SQLiteDatabase db = mDbHelper.getWritableDatabase();
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
int menuItemIndex = item.getItemId();
String[] menuItems = {"Edit", "Delete"};
String menuItemName = menuItems[menuItemIndex];
String listItemName = values[info.position];
if(menuItemName.equalsIgnoreCase("Edit")) {
} else {
//menuItemName === Delete
// Define 'where' part of query.
String selection = PlayerEntry.COLUMN_NAME_NAME + " =? ";
// Specify arguments in placeholder order.
String[] selectionArgs = { listItemName };
// Issue SQL statement.
db.delete(PlayerEntry.TABLE_NAME, selection, selectionArgs);
adapter.notifyDataSetChanged();
}
return true;
}
}
public static class otherFragment extends Fragment {
public static final String ARG_SECTION_NUMBER = "section_number";
public otherFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_lineup,
container, false);
return rootView;
}
}
public static class otherFragment2 extends Fragment {
public static final String ARG_SECTION_NUMBER = "section_number";
public otherFragment2() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_position,
container, false);
return rootView;
}
}
}
addPlayerDialog.java
package blah.blah.blah;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
public class addPlayerDialog extends DialogFragment{
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Gets the data repository in write mode
PlayersDBHelper mDbHelper = new PlayersDBHelper(getActivity().getBaseContext());
final SQLiteDatabase db = mDbHelper.getWritableDatabase();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
final LayoutInflater inflater = getActivity().getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
final View view = inflater.inflate(R.layout.addplayerdialog, null);
final ListView list = (ListView)view.findViewById(R.id.myListView);
builder.setView(view)
// Add action buttons
.setPositiveButton(R.string.add, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
// sign in the user ...
EditText fName = (EditText) view.findViewById(R.id.editFirstName);
Editable firstName = fName.getText();
EditText lName = (EditText) view.findViewById(R.id.editLastName);
Editable lastName = lName.getText();
EditText number = (EditText) view.findViewById(R.id.playerNumber);
int num = Integer.parseInt(number.getText().toString());
Spinner spinner = (Spinner) view.findViewById(R.id.positionSpinner);
String position = spinner.getSelectedItem().toString();
// Create a new map of values, where column names are the keys
ContentValues values = new ContentValues();
values.put(PlayerEntry.COLUMN_NAME_ID, num);
values.put(PlayerEntry.COLUMN_NAME_NAME, firstName + " " + lastName);
values.put(PlayerEntry.COLUMN_NAME_POSITION, position);
// Insert the new row, returning the primary key value of the new row
long newRowId;
newRowId = db.insert(
PlayerEntry.TABLE_NAME,
null,
values);
//**** HERE IS WHERE I THINK THE CHANGE NEEDS TO BE! ****
((ArrayAdapter) list.getAdapter()).notifyDataSetChanged();
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
Spinner spinner = (Spinner) view.findViewById(R.id.positionSpinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(),
R.array.positions, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
return builder.create();
}
}
addPlayerDialog.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:orientation="vertical" >
<EditText
android:id="#+id/editFirstName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/fName" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/editLastName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/lName" />
<EditText
android:id="#+id/playerNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/playerNumberHint"
android:inputType="number" />
<Spinner
android:id="#+id/positionSpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
I think you inserted the data to the DB but forgot to call yourListViewAdapter.add method before calling notifyDataSetChanged
I have read for hours and hours to figure out how to sort and alphabetize a String[], but since I am pulling from my phone's Contacts and this whole ListView is a Fragment, there is nothing that really addresses my situation. I must use a CursorAdapter in my String[] and some alphabetize tutorials use other adapters, so I couldn't figure it out.
If you know of a very simple way to sort/alphabetize my String[] using the code I already have (considering all the particulars that come with using a ListView, Fragment, and pulling from Contacts), I would be very grateful. Currently this code works, but it pulls contacts that are not sorted.
Thanks for your suggestions.
ContactsFragment.java
package com.example.practiceapp.addressbook;
import java.util.ArrayList;
import java.util.Arrays;
import android.app.ListFragment;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import com.example.practiceapp.R;
/*
* Taken from http://stackoverflow.com/questions/18199359/how-to-display-contacts-in-a-listview-in-android-for-android-api-11
*/
public class ContactsFragment extends ListFragment implements
LoaderCallbacks<Cursor>{
private CursorAdapter mAdapter;
public ListView listView;
// Name should be displayed in the text1 TextView in item layout
public static String[] from = { ContactsContract.Contacts.DISPLAY_NAME_PRIMARY };
private static final int[] TO = { android.R.id.text1 };
private android.content.Context context;
ArrayList<String> elements;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// create adapter once
context = getActivity();
int layout = android.R.layout.simple_list_item_1;
Cursor c = null; // there is no cursor yet
int flags = 0; // no auto-requery! Loader requeries.
Arrays.sort(from);
// put List in adapter
mAdapter = new SimpleCursorAdapter(context, layout, c, from, TO, flags);
}
// columns requested from the database
private static final String[] PROJECTION = {
Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME_PRIMARY
};
// Empty public constructor, required by the system
public ContactsFragment() {}
// A UI Fragment must inflate its View (all fragments must override onCreateView)
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the fragment layout
View view = inflater.inflate(R.layout.contact_list_view,
container, false);
listView = (ListView) view.findViewById(R.id.list);
return view;
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// and tell loader manager to start loading
getLoaderManager().initLoader(0, null, this);
listView.setAdapter(mAdapter);
listView.setFastScrollEnabled(true);
}
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
// load from the "Contacts table"
Uri contentUri = Contacts.CONTENT_URI;
// no sub-selection, no sort order, simply every row
// projection says we want just the _id and the name column
return new CursorLoader(getActivity(),
contentUri,
PROJECTION,
null,
null,
null
);
}
#Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
// Once cursor is loaded, give it to adapter
mAdapter.swapCursor(data);
}
#Override
public void onLoaderReset(Loader<Cursor> loader) {
// Delete the reference to the existing Cursor,
// so it can recycle it
mAdapter.swapCursor(null);
}
}
The last parameter to the CursorLoader constructor is the sort order. It uses the same syntax as a normal SQL ORDER BY clause.
I have built three row layout. Assume RowLayout1, RowLayout2, RowLayout3 are the three row layouts. I would like to add RowLayout1 as first row, followed by RowLayout2 and as last row rowLayout3. Data for RowLayout2 comes from sqllite, other data from values .
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class Paasurams extends Activity {
private Divyadesamdb dbHelper;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pasuram);
dbHelper = new Divyadesamdb(this);
dbHelper.open();
/*
* Check if data already exist if total count is
*/
int totalRows = dbHelper.getCountAllPasurams();
Log.d("VC", "totalRows = " + totalRows);
//dbHelper.deleteAllPasurams();
//Log.d("VC", "totalRows after delete = " + totalRows);
if (totalRows < 4535) {
// Clean all data commented to improve performance
dbHelper.deleteAllPasurams();
// Add some data
dbHelper.insertPasurams();
totalRows = dbHelper.getCountAllPasurams();
Log.d("VC","After insertPasurams rows found are "+totalRows);
}
Intent intent = getIntent();
String mcategory = intent.getStringExtra(MainActivity.EXTRA_CATEGORY);
String mstart = intent.getStringExtra(MainActivity.EXTRA_START);
String mending = intent.getStringExtra(MainActivity.EXTRA_ENDING);
String mpasuramNumber = intent
.getStringExtra(MainActivity.EXTRA_PASURAMNUMBER);
String maayiram = intent.getStringExtra(MainActivity.EXTRA_AAYIRAM);
String mazhwaar = intent.getStringExtra(MainActivity.EXTRA_AZHWAAR);
String mmangalasasanamOn = intent
.getStringExtra(MainActivity.EXTRA_MANGALASASANAMON);
String msubCategory = intent
.getStringExtra(MainActivity.EXTRA_SUBCATEGORY);
String mtitle = intent.getStringExtra(MainActivity.EXTRA_TITLE);
// Generate ListView from SQLite Database
displayListView(mpasuramNumber, maayiram, mazhwaar, mcategory,
mmangalasasanamOn, msubCategory, mtitle, mstart, mending);
}
#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;
}
private void displayListView(String mpasuramNumber, String maayiram,
String mazhwaar, String mcategory, String mmangalasasanamOn,
String msubCategory, String mtitle, String mstart, String mending) {
String category = mcategory;
String starting = mstart;
String ending = mending;
String pasuramNumber = mpasuramNumber;
String aayiram = maayiram;
String azhwaar = mazhwaar;
String mangalasasanamOn = mmangalasasanamOn;
String subCategory = msubCategory;
String title = mtitle;
int rowsFound = 0;
Cursor cursor = dbHelper.fetchAllPasurams(pasuramNumber, aayiram,
azhwaar, category, mangalasasanamOn, subCategory, title,
starting, ending);
/*
* How many rows returned from the query
*/
rowsFound = cursor.getCount();
// the desired columns to be bound
String[] columns = new String[] {
Divyadesamdb.PASURAMS_COLUMN_PAASURAMNUMBER,
Divyadesamdb.PASURAMS_COLUMN_AAYIRAM,
Divyadesamdb.PASURAMS_COLUMN_AZHWAAR,
Divyadesamdb.PASURAMS_COLUMN_CATEGORY,
Divyadesamdb.PASURAMS_COLUMN_MANGALASASANAMON,
Divyadesamdb.PASURAMS_COLUMN_PAASURAM_EN_STR,
Divyadesamdb.PASURAMS_COLUMN_SUBCATEGORY,
Divyadesamdb.PASURAMS_COLUMN_TITLE,
Divyadesamdb.PASURAMS_COLUMN_TITLENUMBER,
Divyadesamdb.PASURAMS_COLUMN_IMAGE_ID };
// the XML defined views which the data will be bound to
int[] to = new int[] { R.id.tvPaasuramNumber, R.id.tvAayiram,
R.id.tvAzhwaar, R.id.tvCategory, R.id.tvMangalasasanamOn,
R.id.tvPaasuram, R.id.tvSubCategory, R.id.tvTitle,
R.id.tvtvTitleNumber, R.id.ivAzhwaar };
// create the adapter using the cursor pointing to the desired data
// as well as the layout information
MyCursorAdapter dataAdapter = new MyCursorAdapter(this,
R.layout.paasuram_single_item, cursor, columns, to, 0);
ListView listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(dataAdapter);
Log.d("VC", "Found rows " + rowsFound);
}
// extend the SimpleCursorAdapter to create a custom class where we
// can override the getView to change the row colors
private class MyCursorAdapter extends SimpleCursorAdapter {
Context mContext;
public MyCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
mContext = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// get reference to the row
View view = super.getView(position, convertView, parent);
// check for odd or even to set alternate colors to the row
// background
// want to get the value of this text view,
// but not getting handle.
if (position % 2 == 0) {
view.setBackgroundColor(Color.rgb(238, 233, 233));
} else {
view.setBackgroundColor(Color.rgb(255, 255, 255));
}
return view;
}
}
}
In my application 4 fragments are attached to one activity class. In my activity class i have set the root content view using this
setContentView(R.layout.fragment_pager);
my four fragments are in seperate java files.
In one of my fragment i am displaying a list of contact for this i used
mAdapter = new SimpleCursorAdapter(getActivity(),
android.R.layout.simple_list_item_1, null,
new String[] {ContactsContract.Contacts.DISPLAY_NAME},
new int[] { android.R.id.text1}, 0);
setListAdapter(mAdapter);
setListShown(true);
I want to know that what is the use of setListAdapter in my case and where it put list of content given by SimpleCursorAdapter class? and how can i set the title of my fragment.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:drawable/gallery_thumb">
<TextView android:id="#+id/text"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/hello_world"/>
<!-- The frame layout is here since we will be showing either
the empty view or the list view. -->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" >
<!-- Here is the list. Since we are using a ListActivity, we
have to call it "#android:id/list" so ListActivity will
find it -->
<ListView android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawSelectorOnTop="false"/>
<!-- Here is the view to show if the list is emtpy -->
<TextView android:id="#android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="No items."/>
</FrameLayout>
</LinearLayout>
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
mAdapter = new SimpleCursorAdapter(getActivity(),
android.R.layout.simple_list_item_1, null,
new String[] {ContactsContract.Contacts.DISPLAY_NAME},
new int[] { android.R.id.text1}, 0);
//setListAdapter(mAdapter);
// setListShown(true);
// getLoaderManager().initLoader(0, null, this);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_pager_list, container, false);
View tv = v.findViewById(R.id.text);
((TextView)tv).setText("Contacts");
return v;
}
My Cursor display data when i use this method and removes two methods onCreate and onCreateView
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mAdapter = new SimpleCursorAdapter(getActivity(),
android.R.layout.simple_list_item_1, null,
new String[] {ContactsContract.Contacts.DISPLAY_NAME},
new int[] { android.R.id.text1}, 0);
setListAdapter(mAdapter);
setListShown(true);
getLoaderManager().initLoader(0, null, this);
}
but when i use those three methods together then my application stops unexpectedly. What can be the error when i use these three method together?
package com.keepintouch.android;
import android.os.Bundle;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import com.actionbarsherlock.app.SherlockListFragment;
public class ContactsFragment extends SherlockListFragment implements LoaderManager.LoaderCallbacks<Cursor>{
// This is the Adapter being used to display the list's data.
SimpleCursorAdapter mAdapter;
// If non-null, this is the current filter the user has provided.
String mCurFilter;
View lv;
// These are the Contacts rows that we will retrieve.
static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
};
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
public static Fragment newInstance(Context context){
ContactsFragment contactFragment = new ContactsFragment();
return contactFragment;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mAdapter = new SimpleCursorAdapter(getActivity(),
android.R.layout.simple_list_item_1, null,
new String[] {ContactsContract.Contacts.DISPLAY_NAME},
new int[] { android.R.id.text1}, 0);
setListAdapter(mAdapter);
setListShown(true);
getLoaderManager().initLoader(0, null, this);
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Insert desired behavior here.
Log.i("FragmentComplexList", "Item clicked: " + id);
}
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
// This is called when a new Loader needs to be created. This
// sample only has one Loader, so we don't care about the ID.
// First, pick the base URI to use depending on whether we are
// currently filtering.
Uri baseUri;
if (mCurFilter != null) {
baseUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_FILTER_URI,
Uri.encode(mCurFilter));
} else {
baseUri = ContactsContract.Contacts.CONTENT_URI;
}
// Now create and return a CursorLoader that will take care of
// creating a Cursor for the data being displayed.
String select = "((" + ContactsContract.Contacts.DISPLAY_NAME + " NOTNULL) AND ("
+ ContactsContract.Contacts.HAS_PHONE_NUMBER + "=1) AND ("
+ ContactsContract.Contacts.DISPLAY_NAME + " != '' ))";
return new CursorLoader(getActivity(), baseUri,
CONTACTS_SUMMARY_PROJECTION, select, null,
ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
}
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
// Swap the new cursor in. (The framework will take care of closing the
// old cursor once we return.)
mAdapter.swapCursor(data);
// The list should now be shown.
if (isResumed()) {
setListShown(true);
} else {
setListShownNoAnimation(true);
}
}
public void onLoaderReset(Loader<Cursor> loader) {
// This is called when the last Cursor provided to onLoadFinished()
// above is about to be closed. We need to make sure we are no
// longer using it.
mAdapter.swapCursor(null);
}
}
If you are using the Listfragement setListAdapter set the Adapter in your default list comes with Listfragement..
if using action bar (android:targetSdkVersion="15")
ActionBar ab = this.getActionBar();
ab.setTitle(" ");
if not then
getActivity().setTitle(" "); // after onAttach called