如何让按钮工作?

时间:2013-04-19 03:27:03

标签: android button fragment

此代码适用于我获取事件列表的程度。选择一个并转到详细信息,我可以更新等,然后返回列表。第一个项目成为“ADD”按钮,2ed项目成为“退出”功能。但我想要真正的按钮。

我添加了真正的按钮,在尝试之后,用于处理真实按钮的代码有错误或只是崩溃。我的最新信息是直接从这个网站复制的:

How to handle button clicks using the XML onClick within Fragments

查看双“****”之间的代码。我出了什么问题?

我在下面加了xml;我怀疑问题就在那里。

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" />

1 个答案:

答案 0 :(得分:0)

我没有得到你真正想要的东西,但我认为按钮点击有问题?如果我是对的你可以使用其中一种方式。

1-在您的布局中将此简单代码添加到按钮

android:onClick="MyButton"

并在你的课程中添加

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

2-您可以直接在课堂上使用

MyButtonId.setOnClickListener(new View.OnClickListener() {

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

        }
    });