The following ListFragment doesn't display anything other than a blank screen. I know the data are present. I'm not getting errors, just a blank (white) screen. I'm sure it's something silly. I'm fairly new to Android.
package com.pbs.deliverytrack1;
import com.pbs.deliverytrack1.DBContract;
import com.pbs.deliverytrack1.DBHelper;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class OrderListFragment extends ListFragment {
SimpleCursorAdapter mAdapter;
String TAG = "OrderListFragment";
static final String[] PROJECTION = new String[] {
DBContract.DeliveryOrderTable._ID,
DBContract.DeliveryOrderTable.CUSTOMER,
DBContract.DeliveryOrderTable.ADDRESS };
// Selection criteria
static final String SELECTION = "(("
+ DBContract.DeliveryOrderTable.CUSTOMER + " NOTNULL) AND ("
+ DBContract.DeliveryOrderTable.CUSTOMER + " != '' ) AND ("
+ DBContract.DeliveryOrderTable.DELIVERED_DATETIME + " = '' ))";
private OnOrderSelectedListener listener = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String tag = TAG + ".onCreate()";
Log.d(tag,"Fragment created");
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String tag = TAG + ".onCreateView()";
Log.d(tag, "Inflating fragment_order_list - or trying to.");
View view = inflater.inflate(R.layout.fragment_order_list, container,
false);
if (view == null) {
Log.d(tag, "Problem inflating view, returned null");
}
initializeList();
return view;
}
public void onActivityCreated(Bundle bundle) {
super.onActivityCreated(bundle);
String tag = TAG + ".onActivityCreated()";
Log.d(tag,"Parent Activity Created");
}
public interface OnOrderSelectedListener {
public void onOrderSelected(long orderId);
// show detail record you dummy
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
String tag = "OrderListFragment.onAttach()";
Log.d(tag,"Attached!");
if (activity instanceof OnOrderSelectedListener) {
listener = (OnOrderSelectedListener) activity;
} else {
throw new ClassCastException(activity.toString()
+ " must implement MyListFragment.OnItemSelectedListener");
}
} // onAttach()
public void onStart() {
super.onStart();
Log.d(TAG + ".onStart()", "Started!!!");
}
public void onResume() {
super.onResume();
Log.d(TAG + "onResume()", "Resumed!!!");
}
public void onPause() {
super.onPause();
Log.d(TAG + ".onPause()", "Paused");
}
public void onStop() {
super.onStop();
Log.d(TAG + ".onStop()", "Stopped");
}
public void onDestroyView() {
super.onDestroyView();
Log.d(TAG + ".onDestroyView()", "View Destroyed");
}
public void onDestroy() {
super.onDestroy();
Log.d(TAG + ".onDestroy()", "I'm dying!!!!");
}
public void onDetach() {
super.onDetach();
Log.d(TAG + ".onDetach()", "Off with the fragment!");
}
private void initializeList() {
String tag = TAG + ".initilizeList()";
Log.d(tag,"Setting up cursor.");
// for the cursor adapter, specify which columns go into which views
String[] fromColumns = {
DBContract.DeliveryOrderTable._ID,
DBContract.DeliveryOrderTable.CUSTOMER,
DBContract.DeliveryOrderTable.ADDRESS };
int[] toViews = {
R.id.list_view_order_id,
R.id.list_view_customer_field,
R.id.list_view_address_field };
// create an empty adapter we will use to display the loaded data.
DBHelper dbHelper = new DBHelper(getActivity());
Cursor cursor = dbHelper.getAllOrdersCursor();
if (cursor != null) {
cursor.moveToFirst();
Log.d(tag,"Creating SimpleCursorAdapter mAdapter");
mAdapter = new SimpleCursorAdapter(getActivity(),
R.layout.fragment_order_list_row,
cursor,
fromColumns,
toViews,
0);
setListAdapter(mAdapter);
int count = mAdapter.getCount();
Log.d(tag, "Order Record Count = " + count);
if (mAdapter.isEmpty()) {
Log.d(tag, "Alas! mAdapter is empty");
} // end if mAdapter empty
} else { // cursor is null
Log.d(tag,"The cursor is null");
} // if cursor != null
}
}
This fragment is supposed to show a list derived from a SQLite query. The UI I'm building is a simple split list/detail screen on a tablet. I'm building for pre-Honeycomb, so I'm using the support library.
Let me know if you need to see any other parts of the code.
Here's fragment_order_list.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" >
<ListView
android:id="#id/android:list"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:background="#00FF00"
android:drawSelectorOnTop="false" />
<TextView
android:id="#id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:text="#string/err_no_data" />
</LinearLayout>
Here's the activity the ListFragment is called from:
package com.pbs.deliverytrack1;
import com.pbs.deliverytrack1.OrderListFragment.OnOrderSelectedListener;
import com.pbs.deliverytrack1.DBHelper;
import com.pbs.deliverytrack1.DeliveryOrder;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
public class MainActivity extends FragmentActivity implements
OnOrderSelectedListener {
private final String TAG = "MainActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String tag = TAG + ".onCreate()";
Log.d(tag, "Setting up database.");
new SetupDatabase().execute(this);
Log.d(tag, "Setting Content View");
setContentView(R.layout.activity_main);
}
#Override
public void onOrderSelected(long orderId) {
String tag = TAG + ".onOrderSelected()";
Log.d(tag,"Creating order detail fragment");
OrderDetailFragment fragment = (OrderDetailFragment) getSupportFragmentManager()
.findFragmentById(R.id.detailFragment);
}
#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;
}
}
Here's activity_main.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"
tools:context=".MainActivity"
android:orientation="horizontal"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<fragment
android:id="#+id/action_bar_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
class="com.pbs.deliverytrack1.MyActionBarFragment"
tools:layout="#layout/fragment_actionbar" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="horizontal"
>
<fragment
android:id="#+id/listFragment"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="40"
android:layout_gravity="bottom"
class="com.pbs.deliverytrack1.OrderListFragment" />
<fragment
android:id="#+id/detailFragment"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="60"
class="com.pbs.deliverytrack1.OrderDetailFragment"
tools:layout="#layout/fragment_order_detail" />
</LinearLayout>
</LinearLayout>
MainActivity's LinearLayout is set to:
android:orientation="horizontal"
But you seem to have a "vertical" design in mind, because your first child's width is "match_parent". This means that the ListView is drawn, just off the right side of the screen... Simply change the root layout's orientation.
Related
I'm new at this but I already made a small app that worked the same way.
I can't see what I'm doing wrong because the code looks quiet the same as my previous app.
When I run it or debug my app it shows my layout on my emulator so it does load the page that has to be loaded but that's all it does, it doesn't listen to button clicks. I also gives me no errors.
Here's my XML code for fragment_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" 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=".MainActivity$PlaceholderFragment">
<TextView
android:text="Eventaris"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textSize="100px"
android:textStyle="bold"
android:id="#+id/lblEventaris"
/>
<LinearLayout
android:layout_width="500px"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:orientation="vertical"
android:id="#+id/login">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Gebruikersnaam"
android:id="#+id/txtGebruikersnaam"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Wachtwoord"
android:inputType="textPassword"
android:layout_below="#id/txtGebruikersnaam"
android:id="#+id/txtWachtwoord"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Inloggen"
android:id="#+id/btnInloggen"
android:layout_below="#id/txtWachtwoord"/>
</LinearLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:layout_below="#id/login">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="nog geen account?"
android:gravity="center"
android:id="#+id/lblRegistratie"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Registeren"
android:id="#+id/btnRegistreren"
android:layout_below="#id/lblRegistratie"/>
</RelativeLayout>
</RelativeLayout>
Here's my fragmennt activity MainFragment.Java
package com.example.arno.eventaris;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import java.sql.SQLException;
/**
* Created by Arno on 28/04/2015.
*/
public class MainFragment extends Fragment {
private OnMainFragmentInteractionListener mListener;
private View view;
public MainFragment()
{
//required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
view=inflater.inflate(R.layout.fragment_main, container, false);
Button btnInloggen = (Button) view.findViewById(R.id.btnInloggen);
btnInloggen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
inloggen();
} catch (SQLException e) {
e.printStackTrace();
}
}
});
Button btnRegistreren = (Button) view.findViewById(R.id.btnRegistreren);
btnRegistreren.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
navigeerRegistratie();
}
});
return view;
}
#Override
public void onAttach(Activity activity)
{
super.onAttach(activity);
try{
mListener = (OnMainFragmentInteractionListener) activity;
}
catch (ClassCastException e)
{
throw new ClassCastException(activity.toString() + "must implement OnFragmentInteractionListener");
}
}
public void inloggen() throws SQLException {
EditText gebr=(EditText) view.findViewById(R.id.txtGebruikersnaam);
EditText wachtw=(EditText) view.findViewById(R.id.txtWachtwoord);
String gebruiker = gebr.getText().toString();
String wachtwoord = wachtw.getText().toString();
mListener.login(gebruiker, wachtwoord);
}
public void navigeerRegistratie()
{
mListener.navigeerRegistratie();
}
#Override
public void onDetach()
{
super.onDetach();
mListener = null;
}
public interface OnMainFragmentInteractionListener {
//Todo: Update argument type and name
public void login(String gebruiker, String wachtwoord) throws SQLException;
public void navigeerRegistratie();
}
}
Here is my Main Activity MainActivity.java
package com.example.arno.eventaris;
import android.app.DialogFragment;
import android.database.Cursor;
import android.graphics.Color;
import android.net.Uri;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
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.widget.LinearLayout;
import android.widget.Toast;
import com.example.arno.eventaris.Database.DBAdapter;
import java.sql.SQLException;
public class MainActivity extends ActionBarActivity implements MainFragment.OnMainFragmentInteractionListener,RegistratieFragment.OnRegistratieFragmentInteractionListener{
private Cursor gebruikerCursor;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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_main, 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);
}
#Override
public void login(String gebruiker, String wachtwoord) throws SQLException {
DBAdapter db = new DBAdapter(this);
db.open();
gebruikerCursor = db.getGebruiker(gebruiker);
if(gebruikerCursor.moveToFirst()) {
gebruikerCursor.moveToFirst();
String wwControle = gebruikerCursor.getString(gebruikerCursor.getColumnIndex("wachtwoord"));
if (wachtwoord.equals(wwControle)) {
HomeFragment fragment = new HomeFragment();
Bundle bundle = new Bundle();
bundle.putString("gebruikersnaam", gebruiker);
fragment.setArguments(bundle);
getFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();
} else {
DialogFragment errorlogin = new ErrorLogin();
errorlogin.show(getFragmentManager(), "Wachtwoord incorrect!");
}
}
else
{
DialogFragment errorlogin = new ErrorLogin();
errorlogin.show(getFragmentManager(), "Gebruikersnaam incorrect!");
}
db.close();
}
#Override
public void navigeerRegistratie() {
getFragmentManager().beginTransaction().replace(R.id.container, new RegistratieFragment()).commit();
}
#Override
public void registreren(String gebruiker, String voornaam, String naam, String email, String wachtwoord, String herhaalWachtwoord) {
if(wachtwoord.equals(herhaalWachtwoord)) {
DBAdapter db = new DBAdapter(this);
db.open();
long id = db.insertGebruiker(gebruiker, voornaam, naam, email, wachtwoord);
getFragmentManager().beginTransaction().replace(R.id.container, new MainFragment()).commit();
}
else
{
DialogFragment errorregistratie = new ErrorRegistratie();
errorregistratie.show(getFragmentManager(), "Wachtwoorden komen niet overeen!");
}
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
And as last here is my activity_main.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=".MainActivity" tools:ignore="MergeRootFrame" />
Thanks in advance!
Fixed it by making a new project with the same code, had to be a problem way deeper than my code.
I am trying to develop an application using fragments that has two different layouts, similar to this one:
when i run my app on the phone it crashes immediatly.. here are my codes:
/layout
activity_titles_fragment.xml details_fragment.xml
/layout-land
activity_titles_fragment.xml
/sec
DetailsActivity.java DetailsFragment.java TitlesFragment.java
activity_titles_fragment.xml
<FrameLayout 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"
tools:context=".TitlesFragment" >
<fragment
class="com.example.fragmentsapp.TitlesFragment"
android:id="#+id/titles"
android:layout_height="match_parent"
android:layout_width="match_parent" />
</FrameLayout>
details_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/details"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
and for landscape, activity_titles_fragment.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="horizontal" >
<fragment
class="com.example.fragmentsapp.TitlesFragment"
android:id="#+id/titles"
android:layout_height="match_parent"
android:layout_width="0px"
android:layout_weight="1" />
<FrameLayout
android:id="#+id/details"
android:layout_height="match_parent"
android:layout_width="0px"
android:layout_weight="1" />
</LinearLayout>
DetailsActivity.java
package com.example.fragmentsapp;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class DetailsActivity extends FragmentActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getResources().getConfiguration().orientation
== Configuration.ORIENTATION_LANDSCAPE) {
// If the screen is now in landscape mode, we can show the
// dialog in-line so we don't need this activity.
finish();
return;
}
if (savedInstanceState == null) {
// During initial setup, plug in the details fragment.
DetailsFragment details = new DetailsFragment();
details.setArguments(getIntent().getExtras());
getFragmentManager().beginTransaction().add(
R.id.details, details).commit();
}
}
}
DetailsFragment.java
package com.example.fragmentsapp;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class DetailsFragment extends Fragment {
public static DetailsFragment newInstance(int index)
{
// supply input index as argument
DetailsFragment f = new DetailsFragment();
Bundle args = new Bundle();
args.putInt("index", index);
f.setArguments(args);
return f;
}
public int getShownIndex()
{
return getArguments().getInt("index", 0);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if(container == null)
return null;
TextView text = new TextView(getActivity());
text.setText("Fragment number: " + getShownIndex());
return text;
}
}
TitlesFragment.java
package com.example.fragmentsapp;
import android.app.FragmentTransaction;
import android.app.ListFragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class TitlesFragment extends ListFragment {
boolean mDualPane;
int mCurCheckPosition = 0;
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// populate list items with list view
setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,
getResources().getStringArray(R.array.titles)));
// check for mobile dual pane
View detailsFrame = getActivity().findViewById(R.id.details);
mDualPane = detailsFrame != null && detailsFrame.getVisibility() == View.VISIBLE;
if (savedInstanceState != null)
{
mCurCheckPosition = savedInstanceState.getInt("curChoice",0);
}
if (mDualPane)
{
showDetails(mCurCheckPosition);
}
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
showDetails(position);
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("curChoice", mCurCheckPosition);
}
private void showDetails(int index) {
mCurCheckPosition = index;
if(mDualPane)
{
DetailsFragment details = (DetailsFragment) getFragmentManager().findFragmentById(R.id.details);
if(details == null || details.getShownIndex() != index)
{
details = DetailsFragment.newInstance(index);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.details, details);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
}
} else {
Intent intent = new Intent();
intent.setClass(getActivity(), DetailsActivity.class);
intent.putExtra("index", index);
startActivity(intent);
}
}
}
Have you declared all of your activities in your Android ? Have you not forgotten this one ?
<activity android:name=".activities.FragmentLayout$DetailsActivity">
</activity>
Also, you should add a stacktrace each time you are facing such kind of errors.
I'm would like to make my Android application use Fragment Transactions, so that I can
switch between various Fragments display their associated lists. My application
worked fine prior to attempting to convert to just Fragment Transactions. In my initial activity_main.xml,
I removed the android:name="com.birdsall.peter.chap.ChapterFragment", it is my understanding you can't use xml defined fragments
with fragment transactions.
1) I can't seem to get the .add() within
getSupportFragmentManager correct with it's parameters, even with code from a working example.
I have also attempted to use the newer version of FragmentTransactions to no avail.
I even took a working example of code using getSupportFragmentManager / FragmentTransactions,
modified to use the names I had, it worked. I then imported that code into my application
and it fails on the .add() syntax. I'm somewhat new to Android development and can't
pinpoint where I'm going wrong.
Here is my main xml layout for the FrameLayout
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1" />
<fragment
android:id="#+id/chapterfragments"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- Removed this from above <fragment> block to enable Fragment Transaction
android:name="com.birdsall.peter.chap.ChapterFragment" -->
</LinearLayout>
Here is the xml layout for the ChapterFragment ListView
chapterfragment.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView android:id="#+id/chapterlist" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_alignParentLeft="true" />
</RelativeLayout>
Here is the layout for the details of the list
chapter_info.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="vertical" android:padding="6dip">
<TextView android:id="#+id/chapter1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_alignParentTop="true"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView android:id="#+id/textViewLiteral" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/chapter1" android:text=" - "
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView android:id="#+id/chaptertitle1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/textViewLiteral"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
Here is the MainActivity.java that I have modified. I left the 'setContentView(R.layout.activity_main);' as I thought I needed to create a view (even though it empty) to add the fragment view to.
package com.birdsall.peter.chap;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
public class MainActivity extends FragmentActivity implements ChapterFragment.ChapterSelectedListener {
private static final String TAG = "Main_Activity";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "Starting ...");
setContentView(R.layout.activity_main);
if (findViewById(R.id.fragment_container) != null) {
if (savedInstanceState != null) {
return;
}
// Create an instance of ExampleFragment
ChapterFragment firstFragment = new ChapterFragment();
// Add the fragment to the 'fragment_container' FrameLayout
getSupportFragmentManager().beginTransaction()
**.add**(R.id.fragment_container, firstFragment).commit();
Log.i(TAG, "Ending ...");
}
} ...
Here is my ChapterFragment.java
package com.birdsall.peter.chap;
import android.app.Activity;
import android.app.Fragment;
import android.app.LoaderManager;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
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.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
public class ChapterFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {
ChapterSelectedListener mCallback;
// Container Activity must implement this interface
public interface ChapterSelectedListener {
public void onChapterSelected(String position, int rowId);
}
public SimpleCursorAdapter dataAdapter;
private static final String TAG = "ChapterFragment";
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState ) {
View listview = inflater.inflate(R.layout.activity_main,null);
ListView mList =(ListView)listview.findViewById(R.id.chapterlist);
// The desired columns to be bound
String[] columns = new String[] {
TDAdb.COL_CHAPTER,
TDAdb.COL_CHAPTERTITLE};
// the XML defined views which the data will be bound to
int[] to = new int[] {
R.id.chapter1,
R.id.chaptertitle1,
};
// create an adapter from the SimpleCursorAdapter
dataAdapter = new SimpleCursorAdapter(
getActivity(),
R.layout.chapter_info,
null,
columns,
to,
0);
mList.setAdapter(dataAdapter);
//Ensures a loader is initialized and active.
getLoaderManager().initLoader(0, null, this);
return listview;
}
#Override
public void onStart() {
super.onStart();
Log.i(TAG, " onStart");
displayListView();
Log.i(TAG, " end of onStart");
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
mCallback = (ChapterSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement ChapterSelectedListener");
}
}
#Override
public void onResume() {
super.onResume();
//Starts a new or restarts an existing Loader in this manager
Log.i(TAG, " onResume");
getLoaderManager().restartLoader(0, null, this);
}
private void displayListView() {
Log.i(TAG, " Starting displayListView");
// The desired columns to be bound
String[] columns = new String[] {
TDAdb.COL_CHAPTER,
TDAdb.COL_CHAPTERTITLE};
// the XML defined views which the data will be bound to
int[] to = new int[] {
R.id.chapter1,
R.id.chaptertitle1,
};
// create an adapter from the SimpleCursorAdapter
dataAdapter = new SimpleCursorAdapter(
getActivity(),
R.layout.chapter_info,
null,
columns,
to,
0);
// get reference to the ListView
ListView listView = (ListView) getView().findViewById(R.id.chapterlist);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
//Ensures a loader is initialized and active.
getLoaderManager().initLoader(0, null, this);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> listView, View view,
int position, long id) {
// Get the cursor, positioned to the corresponding row in the result set
Cursor cursor = (Cursor) listView.getItemAtPosition(position);
String chaptervalueselected =
cursor.getString(cursor.getColumnIndexOrThrow(TDAdb.COL_CHAPTER));
mCallback.onChapterSelected(chaptervalueselected, position);
Toast.makeText(getActivity(), "Chapter " + chaptervalueselected, Toast.LENGTH_SHORT).show();
// starts a new Intent to update/delete a Chapter
// pass in row Id to create the Content URI for a single row
//Intent chapterEdit = new Intent(getBaseContext(), ChapterEdit.class);
//Bundle bundle = new Bundle();
//bundle.putString("mode", "update");
//bundle.putString("rowId", rowId);
//chapterEdit.putExtras(bundle);
//startActivity(chapterEdit);
}
});
}
// This is called when a new Loader needs to be created.
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
Log.i(TAG, " onCreateLoader");
String[] projection = {
TDAdb.KEY_ROWID,
TDAdb.COL_CHAPTER,
TDAdb.COL_CHAPTERTITLE};
CursorLoader cursorLoader = new CursorLoader(getActivity(),
TDAProvider.CONTENT_URI, projection, null, null, null);
return cursorLoader;
}
#Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
Log.i(TAG, " ononLoadFinished");
// Swap the new cursor in. (The framework will take care of closing the
// old cursor once we return.)
dataAdapter.swapCursor(data);
}
#Override
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.
Log.i(TAG, " onLoaderReset");
dataAdapter.swapCursor(null);
}
}
If you are already using the add method of FragmentTransaction you must not include the <fragment tag in your layout. What if you just left your main activity XML like this:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
In chapterFragment.java change your import
import android.app.Fragment
To
import android.support.v4.app.Fragment
I am working with the android support library, to be able to use fragments from Froyo, and the Sherlock extension, to show an ActionBar.
I have a fragment which shows three textViews and a button, and I want to be able to change the button text dinamically, depending on the content of the Intent. The problem is that when I call button.setText(String text), a second button appears. However, I've called button.getId() when clicking on each of them, and the Id is the same. I don't have a clue of why this is happening, so I'd appreciate some help.
The app is run on a Samsung Galaxy S, with Android 2.3.3. I can't upload a screen capture because I don't have enough reputation yet :(
This is the code:
FRAGMENT
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import com.actionbarsherlock.app.SherlockFragment;
import com.parse.ParseUser;
import com.tiempoderodar.egdf.EGDF_App;
import com.tiempoderodar.egdf.R;
import com.tiempoderodar.egdf.security.Constants;
/**
* #author Daniel Leal López
*
*/
public class ChapterDetailsFragment extends SherlockFragment{
private Button b;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("ChapterDetailsFragment", "Created");
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ParseUser user = EGDF_App.getUser();
b = (Button) getSherlockActivity().findViewById(R.id.buttonWatchChapter);
if (user != null){
String chapterNumber = "";
Bundle extras = getSherlockActivity().getIntent().getExtras();
String s = extras.getString(Constants.CHAPTER_NUMBER_PARSE);
if((s != null)&&(!s.equals(""))){
// tvNumber.setText(s);
chapterNumber = "Chapter_" + s;
Log.i("Properties", s);
}
String seen = user.getString(chapterNumber);
if((seen != null)&&(!seen.equals(""))&&(seen.equals(Constants.USER_CHAPTER_SEEN))){
b.setText("Second text: "+getString(R.string.watch_chapter_button_text));
Log.i("BUTTON CHANGED", "Text changed to watch");
}
else if((seen != null)&&(!seen.equals(""))&&(seen.equals(Constants.USER_CHAPTER_NOT_SEEN))){
b.setText("Second text: " + getString(R.string.buy_chapter_button_text));
Log.i("BUTTON CHANGED", "Text changed to buy");
}else{
Log.w("DEV ERROR", "Chapter text not obtained");
}
}else{
Log.w("DEV ERROR", "ParseUser is null in EGDF_App!!!");
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.chapter_details, container, false);
return view;
}
public void setText(String item) {
getSherlockActivity().getSupportActionBar().setTitle(item);
}
public void setButtonText(String text){
b.setText(text);
}
}
Activity
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.actionbarsherlock.app.SherlockFragmentActivity;
public class ChapterDetailsActivity extends SherlockFragmentActivity {
private Bundle extras;
MediaPlayer mMediaPlayer;
private String chapterNumber;
private boolean isChapterSeen;
// private int duration;
#Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.Theme_Sherlock_Light_DarkActionBar);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chapter_details);
if (savedInstanceState == null) {
// During initial setup, plug in the details fragment.
ChapterDetailsFragment details = new ChapterDetailsFragment();
details.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction().add(
android.R.id.content, details).commit();
}
}
public void watchChapter(View view){
Log.i("Button", "Watch chapter button PRESSED");
Button b = (Button) view;
Log.d("BUTTON PARENT VIEW", b.getParent().getClass().getName());
Log.d("BUTTON ID", String.valueOf(b.getId()));
String loadChapter = getString(R.string.load_chapter_button_text);
String watchChapter = getString(R.string.watch_chapter_button_text);
String buyChapter = getString(R.string.buy_chapter_button_text);
}
#Override
protected void onPause() {
Log.i("Activity lifecycle", "On pause called");
super.onPause();
}
#Override
protected void onStop() {
Log.i("Activity lifecycle", "On stop called");
super.onStop();
}
#Override
protected void onDestroy() {
Log.i("Activity lifecycle", "On destroy called");
super.onDestroy();
EGDF_App.releaseMediaPlayer();
}
#Override
protected void onResume() {
Log.i("Activity lifecycle", "On resume called");
super.onResume();
}
#Override
protected void onStart() {
Log.i("Activity lifecycle", "On start called");
super.onStart();
}
}
Activity layout
<?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" >
<fragment
android:id="#+id/chapterDetailsFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.tiempoderodar.egdf.content.ChapterDetailsFragment" />
</LinearLayout>
Fragment Layout
<?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" >
<TextView
android:id="#+id/textViewChapterNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="30dip" />
<TextView
android:id="#+id/textViewChapterSeason"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="30dip" />
<TextView
android:id="#+id/textViewChapterSinopsis"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="30dip" />
<TextView
android:id="#+id/textViewChapterCredits"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="30dip" />
<Button
android:id="#+id/buttonWatchChapter"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/watch_chapter_button_text"
android:layout_gravity="center"
android:onClick="watchChapter"/>
</LinearLayout>
Thanks!
I think I might have found the problem. I was calling
b = (Button) getSherlockActivity().findViewById(R.id.button);
but it should be
b = (Button) getView().findViewById(R.id.button);
With that change it works correctly
As with several other posts here, I am trying to create a ListView that includes a CheckBox for each row, and use a SQLite database to store the current state of the selection.
Starting with the example at http://appfulcrum.com/?p=351, which did not quite work as is, I created a simple app that creates the database, populates it with 20 items, and displays the list.
It successfully retrieves the state and stores the state of the selection.
BUT, it does not correctly show the CheckBox state if I change it, scroll to the other end of the list, and scroll back. e.g. if I select the first CheckBox, scroll to the bottom, and come back to the top, the CheckBox is no longer set. This is being run on an Android 2.1 Samsung handset.
If I return to the main screen, come back into the list, the CheckBox is correctly set, so the database has indeed been updated.
The example extends SimpleCursorAdapter, and the getView() invokes setChecked() with true or false as appropriate based on the value of the selection column in the table.
Below are all the sources.
I'd certainly appreciate being told, "Duh, here's your problem..."
CustomListViewDB.java
// src/CustomListViewDB.java
package com.appfulcrum.blog.examples.listviewcustomdb;
import android.app.ListActivity;
import android.database.Cursor;
import android.database.SQLException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public class CustomListViewDB extends ListActivity {
private ListView mainListView = null;
CustomSqlCursorAdapter adapter = null;
private SqlHelper dbHelper = null;
private Cursor currentCursor = null;
private ListView listView = null;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple);
if (this.dbHelper == null) {
this.dbHelper = new SqlHelper(this);
}
listView = getListView();
listView.setItemsCanFocus(false);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
//listView.setClickable(true);
Button btnClear = (Button) findViewById(R.id.btnClear);
btnClear.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Toast.makeText(getApplicationContext(),
" You clicked Clear button", Toast.LENGTH_SHORT).show();
ClearDBSelections();
}
});
new SelectDataTask().execute();
this.mainListView = getListView();
mainListView.setCacheColorHint(0);
}
#Override
protected void onRestart() {
super.onRestart();
new SelectDataTask().execute();
}
#Override
protected void onPause() {
super.onPause();
this.dbHelper.close();
}
protected void ClearDBSelections() {
this.adapter.ClearSelections();
}
private class SelectDataTask extends AsyncTask<Void, Void, String> {
protected String doInBackground(Void... params) {
try {
CustomListViewDB.this.dbHelper.createDatabase(dbHelper.dbSqlite);
CustomListViewDB.this.dbHelper.openDataBase();
CustomListViewDB.this.currentCursor = CustomListViewDB.this.dbHelper
.getCursor();
} catch (SQLException sqle) {
throw sqle;
}
return null;
}
// can use UI thread here
protected void onPostExecute(final String result) {
startManagingCursor(CustomListViewDB.this.currentCursor);
int[] listFields = new int[] { R.id.txtTitle };
String[] dbColumns = new String[] { SqlHelper.COLUMN_TITLE };
CustomListViewDB.this.adapter = new CustomSqlCursorAdapter(
CustomListViewDB.this, R.layout.single_item,
CustomListViewDB.this.currentCursor, dbColumns, listFields,
CustomListViewDB.this.dbHelper);
setListAdapter(CustomListViewDB.this.adapter);
}
}
}
CustomSqlCursorAdapter.java
// src/CustomSqlCursorAdapter.java
package com.appfulcrum.blog.examples.listviewcustomdb;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
public class CustomSqlCursorAdapter extends SimpleCursorAdapter {
private Context mContext;
private SqlHelper mDbHelper;
private Cursor mCurrentCursor;
public CustomSqlCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to, SqlHelper dbHelper) {
super(context, layout, c, from, to);
this.mCurrentCursor = c;
this.mContext = context;
this.mDbHelper = dbHelper;
}
public View getView(int pos, View inView, ViewGroup parent) {
View v = inView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.single_item, null);
}
if (!this.mCurrentCursor.moveToPosition(pos)) {
throw new SQLException("CustomSqlCursorAdapter.getView: Unable to move to position: "+pos);
}
CheckBox cBox = (CheckBox) v.findViewById(R.id.bcheck);
// save the row's _id value in the checkbox's tag for retrieval later
cBox.setTag(Integer.valueOf(this.mCurrentCursor.getInt(0)));
if (this.mCurrentCursor.getInt(SqlHelper.COLUMN_SELECTED_idx) != 0) {
cBox.setChecked(true);
Log.w("SqlHelper", "CheckBox true for pos "+pos+", id="+this.mCurrentCursor.getInt(0));
} else {
cBox.setChecked(false);
Log.w("SqlHelper", "CheckBox false for pos "+pos+", id="+this.mCurrentCursor.getInt(0));
}
//cBox.setOnClickListener(this);
cBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.w("SqlHelper", "Selected a CheckBox and in onCheckedChanged: "+isChecked);
Integer _id = (Integer) buttonView.getTag();
ContentValues values = new ContentValues();
values.put(SqlHelper.COLUMN_SELECTED,
isChecked ? Integer.valueOf(1) : Integer.valueOf(0));
mDbHelper.dbSqlite.beginTransaction();
try {
if (mDbHelper.dbSqlite.update(SqlHelper.TABLE_NAME, values, "_id=?",
new String[] { Integer.toString(_id) }) != 1) {
throw new SQLException("onCheckedChanged failed to update _id="+_id);
}
mDbHelper.dbSqlite.setTransactionSuccessful();
} finally {
mDbHelper.dbSqlite.endTransaction();
}
Log.w("SqlHelper", "-- _id="+_id+", isChecked="+isChecked);
}
});
TextView txtTitle = (TextView) v.findViewById(R.id.txtTitle);
txtTitle.setText(this.mCurrentCursor.getString(this.mCurrentCursor
.getColumnIndex(SqlHelper.COLUMN_TITLE)));
return (v);
}
public void ClearSelections() {
this.mDbHelper.clearSelections();
this.mCurrentCursor.requery();
}
}
ListViewWithDBActivity.java
package com.appfulcrum.blog.examples.listviewcustomdb;
import android.app.Activity;
import android.os.Bundle;
public class ListViewWithDBActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
SqlHelper
// SqlHelper.java
package com.appfulcrum.blog.examples.listviewcustomdb;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;
public class SqlHelper extends SQLiteOpenHelper {
private static final String DATABASE_PATH = "/data/data/com.appfulcrum.blog.examples.listviewcustomdb/databases/";
public static final String DATABASE_NAME = "TODOList";
public static final String TABLE_NAME = "ToDoItems";
public static final int ToDoItems_VERSION = 1;
public static final String COLUMN_ID = "_id"; // 0
public static final String COLUMN_TITLE = "title"; // 1
public static final String COLUMN_NAME_DESC = "description";// 2
public static final String COLUMN_SELECTED = "selected"; // 3
public static final int COLUMN_SELECTED_idx = 3;
public SQLiteDatabase dbSqlite;
private Context mContext;
public SqlHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
mContext = context;
}
#Override
public void onCreate(SQLiteDatabase db) {
createDB(db);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w("SqlHelper", "Upgrading database from version " + oldVersion
+ " to " + newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS ToDoItems;");
createDB(db);
}
public void createDatabase(SQLiteDatabase db) {
createDB(db);
}
private void createDB(SQLiteDatabase db) {
if (db == null) {
db = mContext.openOrCreateDatabase(DATABASE_NAME, 0, null);
}
db.execSQL("CREATE TABLE IF NOT EXISTS ToDoItems (_id INTEGER PRIMARY KEY, title TEXT, "
+" description TEXT, selected INTEGER);");
db.setVersion(ToDoItems_VERSION);
//
// Generate a few rows for an example
//
// find out how many rows already exist, and make sure there's some minimum
SQLiteStatement s = db.compileStatement("select count(*) from ToDoItems;");
long count = s.simpleQueryForLong();
for (int i = 0; i < 20-count; i++) {
db.execSQL("INSERT INTO ToDoItems VALUES(NULL,'Task #"+i+"','Description #"+i+"',0);");
}
}
public void openDataBase() throws SQLException {
String myPath = DATABASE_PATH + DATABASE_NAME;
dbSqlite = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
}
#Override
public synchronized void close() {
if (dbSqlite != null)
dbSqlite.close();
super.close();
}
public Cursor getCursor() {
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
queryBuilder.setTables(TABLE_NAME);
String[] asColumnsToReturn = new String[] { COLUMN_ID, COLUMN_TITLE,
COLUMN_NAME_DESC, COLUMN_SELECTED };
Cursor mCursor = queryBuilder.query(dbSqlite, asColumnsToReturn, null,
null, null, null, COLUMN_ID+" ASC");
return mCursor;
}
public void clearSelections() {
ContentValues values = new ContentValues();
values.put(COLUMN_SELECTED, 0);
this.dbSqlite.update(SqlHelper.TABLE_NAME, values, null, null);
}
}
Start.java
//src/Start.java
package com.appfulcrum.blog.examples.listviewcustomdb;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class Start extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnSimple = (Button) findViewById(R.id.btnSimple);
btnSimple.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Toast.makeText(getApplicationContext(),
" You clicked ListView From DB button", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(v.getContext(), CustomListViewDB.class);
startActivityForResult(intent, 0);
}
});
}
}
layout/main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/buttonlayout" android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:gravity="left|top" android:paddingTop="2dp"
android:paddingBottom="2dp">
<TextView android:id="#+id/txtTest" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:textStyle="bold"
android:text="#string/app_name" android:textSize="15sp"
android:textColor="#FF0000" android:gravity="center_vertical"
android:paddingLeft="5dp">
</TextView>
<Button android:id="#+id/btnSimple"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="15sp"
android:text="Listview from DB"
android:textColor="#000000"
>
</Button>
</LinearLayout>
layout/simple.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:id="#+id/buttonlayout"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:height="32dp"
android:gravity="left|top" android:paddingTop="2dp"
android:paddingBottom="2dp">
<LinearLayout android:id="#+id/buttonlayout2"
android:orientation="horizontal" android:layout_height="wrap_content"
android:gravity="left|center_vertical" android:layout_width="wrap_content"
android:layout_gravity="left|center_vertical">
<TextView android:id="#+id/txtTest"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:textStyle="bold"
android:text="#string/list_header" android:textSize="15sp"
android:gravity="center_vertical" android:paddingLeft="5dp">
</TextView>
<Button android:id="#+id/btnClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Clear"
android:textSize="15sp" android:layout_marginLeft="10px"
android:layout_marginRight="10px"
android:layout_marginBottom="2px"
android:layout_marginTop="2px" android:height="15dp"
android:width="70dp"></Button>
</LinearLayout>
</LinearLayout>
<TableLayout android:id="#+id/TableLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:stretchColumns="*">
<TableRow>
<ListView android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></ListView>
</TableRow>
</TableLayout>
</LinearLayout>
layout/single_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="horizontal" android:gravity="center_vertical">
<CheckBox android:id="#+id/bcheck"
android:layout_width="wrap_content"
android:layout_height="fill_parent" />
<TextView android:id="#+id/txtTitle"
android:layout_width="wrap_content" android:gravity="left|center_vertical"
android:layout_height="?android:attr/listPreferredItemHeight"
android:layout_alignParentLeft="true"
android:textSize="20sp" android:text="Test"
android:textStyle="bold" android:paddingLeft="5dp"
android:paddingRight="2dp" android:focusable="false"
android:focusableInTouchMode="false"></TextView>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="horizontal"
android:gravity="right|center_vertical">
</LinearLayout>
</LinearLayout>
values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, ListViewWithDBActivity!</string>
<string name="app_name">ListViewWithDB</string>
<string name="list_header">List Headers</string>
</resources>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="1" android:versionName="1.0"
package="com.appfulcrum.blog.examples.listviewcustomdb">
<application android:icon="#drawable/icon"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar">
>
<activity android:name=".Start" android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".CustomListViewDB"></activity>
</application>
<uses-sdk android:minSdkVersion="7" /> <!-- android 1.6 -->
</manifest>
If you want to build, throw some arbitrary icon.png into drawable.
Thanks in advance.
I found the most complete solution to the problem off-site.
At Android ListView with CheckBox : Retain State
Appreciate the help folks.
The Views in a ListView are recycled, and this sounds like an issue with that. You probably need to invalidate your onCheckedChangedListener so that when you do setChecked() it isn't calling the previous listener inadvertently. There could be other ramifications of the recycling as well, so keep that in mind.
So try:
cBox.setOnCheckedChangeListener(null);
...
cBox.setChecked();
...
cBox.setOnCheckedChangeListner(<real listener);