-1

This code works to the extent I get a list of events. Select one and go to detail, and I can update etc. and I return to my list. The first item becomes the "ADD" button and 2ed item becomes the "Exit" function. But I want real buttons.

I added real buttons and after tries, the code intended to handle the real buttons either has errors or just crashes. My latest is copied directly from this site:

How to handle button clicks using the XML onClick within Fragments

Look at the code between the double "****". What am I going wrong?

I included the xml below; I suspect the problem is there.

EventListFragment.java

public class EventListFragment extends ListFragment
                                  implements OnClickListener,
                                  LoaderCallbacks<Cursor>
   {
    private SimpleCursorAdapter mAdapter;
    List strRecId = new ArrayList();

    private ListAdapter lstAdapter;
    private Button btn_Add;

    List strRecord = new ArrayList(3);
    List strFields = new ArrayList();

    @Override
    public void onCreate(Bundle savedInstanceState)
       {
        super.onCreate(savedInstanceState);
         // View myFragmentView = inflater.inflate(R.layout.event_list);

//-----------------------------------------------------;
// ALSO LOOK FOR "INITRUN" BELOW AND COMMENT OUT STATEMENT;
//   IN TWO PLACES;
//-------------------------------------------------------; 
//  BELOW READS FROM DATABASE AND DISPLAYS EVENTNAME;
//  THEN ALLOWS UPDATE OF THEM ABD INSERT FROM EXISTING;        
//      Log.d("EventLst","X onCrt: x01 START" ); 

        String[]  strItems =  new String[]  
           { 
            EventProvider.COLUMN_EVENTNAME
           };
        Log.d("EventLst","X onCrt: x01strI:" + strItems[0]);          
        int[] iCnt = new int[]
           {  
            R.id.text1
         };
        mAdapter =  new SimpleCursorAdapter(getActivity(),R.layout.event_row,
                    null, strItems, iCnt,  0);
        Log.d("EventLst","X onCrt: x02 START" );

        setListAdapter(mAdapter);
  getLoaderManager().initLoader(0, null, this);

  Log.d("EventLst","X onCrt: x03 START" );

       }  //----> backto public void onCreate

//
//-------------------------------------------------------
//      Log.d("EventLst","X onCrt: DONE" ); 
//---------------------------------------------------------     


    @Override
    public void onActivityCreated(Bundle savedInstanceState)
//*      The activity (not fragment's) onCreate() method returns and calls
//*       onActivityCreated() callback
       { 
  super.onActivityCreated(savedInstanceState);
  Log.d("EventLst","S onActCrtd");
//* The ListFragment supports showing a message when the list is empty.  Uses a
//* value of R.string.loadingmsg - Add to strings.xml
  setEmptyText(getResources().getString(R.string.loadingmsg));    
  registerForContextMenu(getListView());
  setHasOptionsMenu(true);

  Log.d("EventLst","X onActCrtd" );
       } 

    //*---------------------------------------------------------------
    //*      START EDIT ACTIVITY:    
            //  user clicked in list, start edit view
            // startActivity (...) starts edit activity, passing id = n
            //      where "n" is (record key) id from list
        @Override
        public void onListItemClick(ListView l,View V, int position, long id)
           {
            super.onListItemClick(l, V, position,  id);
            Log.d("EventLst","S onLsItmCk" + ";P:" + position + ";I:" + id);
            long lgRecN = 0;
    // id is the position in the list of activities,
    //     we will use it to get the RowId of this record in DB  
        if (id == 1)
           {
           android.os.Process.killProcess(android.os.Process.myPid());
           }
        if (id > 0)
           {
            String strRecN = (String) strRecId.get((int) id-1);    
            lgRecN = Long.parseLong(strRecN); 
           }
        else
           {
            lgRecN = 0;
           };
        startActivity(new Intent(getActivity(), EventEditActivity.class)
              .putExtra(EventProvider.COLUMN_ROWID, lgRecN) );
        // Log.d("EventLst","X onLsItmCk" + ";I:" + id);       
       }
//****************************************************;        
//****************************************************;    
//  This was copied precisely from:

//      https://stackoverflow.com/questions/6091194/how-to-handle-button-clicks-using-
//          the-xml-onclick-within-fragments/7969020#7969020
//     then shangrd "StartButton" to  btn_Exit
// I had errors     

    //@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
               Bundle savedInstanceState)
       {
        View v = inflater.inflate(R.layout.event_list, container, false);
        Log.d("EventLst","S creatrView");
        Button b = (Button) v.findViewById(R.id.btn_Add);
        b.setOnClickListener(this);
        Log.d("EventLst","S onLsItmCk" + v);
        return v;
       }

        @Override
        public void onClick(View v)
           {
            Log.d("EventLst","S onclkBtn" + v);
           }  
//**************************************************;    
//***************************************************;    
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState)
       {
        super.onViewCreated(view, savedInstanceState);
        Log.d("EventLst","S onVwCrt");
      setEmptyText(getResources().getString(R.string.loadingmsg));
        registerForContextMenu(getListView());
      Log.d("EventLst","X onVwCrt");
       }  

event_list.xml

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:name="com.dummies.android.taskreminder.EventListFragment" >
 <Button  android:id="@+id/btn_Add"
    android:layout_width="50dp"
    android:layout_height="30dp"   
    android:textSize="12dp"
    android:padding="1dip"
    android:text="@string/add" />
  <Button  android:id="@+id/btn_Exit"
    android:layout_width="50dp"
    android:layout_height="30dp"
    android:textSize="12dp"
    android:padding="1dip"
    android:layout_marginLeft="5dp"
    android:layout_toRightOf="@+id/btn_Add" 
    android:text="@string/exit" />
  <Button  android:id="@+id/btn_Other"
    android:layout_width="50dp"
    android:layout_height="30dp"
    android:textSize="12dp"
    android:padding="1dip"
    android:layout_marginLeft="5dp"
    android:layout_toRightOf="@+id/btn_Exit" 
    android:text="@string/other" />
 <fragment
    android:name="com.dummies.android.taskreminder.EventListFragment"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/btn_Add"  />
 </RelativeLayout>   

event_row.xml

  <TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/text1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:textSize="12dp"
    android:padding="2dip" />
Community
  • 1
  • 1
ClarkG
  • 27
  • 1
  • 10

1 Answers1

0

i didn't get what you really want but i assume that you have a problem with the button click ? if i am right. you can use one of these ways.

1- in your layout add this simple code to your button

android:onClick="MyButton"

and in you class add

public void MyButton(View v) {
// you may want to perform your clicks here.
}

2- you can use directly from your class

MyButtonId.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
        // you may want to perform your clicks here.

        }
    });
Kosh
  • 6,140
  • 3
  • 36
  • 67
  • First, Thanks for the reply. Also thanks to @alex for making it readable. My follow-up didn't get posted, so I will re-ask: XML = – ClarkG Apr 20 '13 at 16:01
  • However: With same XML: (android:onClick="myButton" . . .) Tried this, NOT in onCreate, public void myButton(View v) { Log.d(" . . ."); } I get: java.lang.IllegalStateException: Could not find a method myButton(View) in the activity So aI am still doing something wrong! Probably very basic. My screen is a fragment with dynamic list and now 3 buttons. I just want to select a button and invoke code - for now a "log.d...;" will make me happy! – ClarkG Apr 20 '13 at 16:18
  • I have about come to the conclusion that my problem is that while clicking items in my list created by "setListAdapter(lstAdapter);" can be handled by the fragment, a button cannot. Why the two are different, I do not understand. Unless there is a better way, I guess I spend the next couple days moving the fragment code into the activity. However, can someone tell me if I can use setListAdapter the same way on the activity? – ClarkG Apr 21 '13 at 00:25