-1

此代码在我获得事件列表的范围内有效。选择一个并转到详细信息,我可以更新等,然后返回我的列表。第一项成为“添加”按钮,第二项成为“退出”功能。但我想要真正的按钮。

我添加了真实按钮,经过尝试,用于处理真实按钮的代码要么有错误,要么只是崩溃。我的最新是直接从这个站点复制的:

如何在 Fragments 中使用 XML onClick 处理按钮点击

查看双“****”之间的代码。我怎么了?

我在下面包含了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" />
4

1 回答 1

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.

        }
    });
于 2013-04-19T04:59:02.787 回答