-3

首先,这是我在这里的第一个问题,感谢您的回复:) 我有一个片段扩展了 SherlockListFragment,它有一个保存在数据库中的(事件)列表,当我单击列表中的任何项目时,我想要获取所有事件数据并填写另一个具有editText(s)的活动......这是frgment.java代码

public class EventsFragment extends SherlockListFragment  {

EditText _eventName_;
EditText next_activty_text;
List<String> presidents = new ArrayList<String>();
public DBAdapter db;

String buf = "";

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_events, container, false);

    _eventName_ = (EditText) v.findViewById(R.id.editText11);
    next_activty_text = (EditText) v.findViewById(R.id.editText12);

    // The most Important part in action bar menus :)
    setHasOptionsMenu(true);

    /*
    //This is how to link a button in fragment by its .xml file :Ds
    Button create_btn = (Button) v.findViewById(R.id.button1);

    create_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            //startActivity(new Intent("com.nazmy.CreateEventActivity"));

        }
    });*/

    return v;
}



@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    db = new DBAdapter(getActivity());
    db.open();

    int i = 1;
    while(db.getEvent(i).isLast()){
        Cursor c = db.getEvent(i);
        presidents.add(c.getString(1));
        i++;
    }


    db.close();
    setListAdapter(new ArrayAdapter<String>(getActivity(),
            android.R.layout.simple_list_item_1, presidents));

    setListAdapter(new ArrayAdapter<String>(getActivity(),
            android.R.layout.simple_list_item_1, presidents));

}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.fragment_events_menu, menu);
 }

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch(item.getItemId()) {
        case R.id.add:
            startActivity(new Intent("com.nazmy.CreateEventActivity"));
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    //startActivity(new Intent("com.nazmy.CreateEventActivity"));

    db = new DBAdapter(getActivity());
    db.open();

    Cursor c = db.getEvent(id+1);

    //populate(c.getString(1));
    _eventName_.setText(c.getString(1));
    db.close();
}
 }

` 这是活动代码

public class CreateEventActivity extends FragmentActivity {

EditText mEdit;
EditText mEdit2;

EditText mEditFromTime;
EditText mEditToTime;

int whichDateBtn;
int whichTimeBtn;

DBAdapter db = new DBAdapter(this);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_create_event);

    db.open();
    Cursor c = db.getAllEvents();
    if (c.moveToFirst())
    {
    do {
    DisplayEvent(c);
    } while (c.moveToNext());
    }
    db.close();

    Button cancel_btn = (Button) findViewById(R.id.button4);
    Button save_btn = (Button) findViewById(R.id.button1);  //if clicked -> save in data-base

    save_btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            db.open();
            EditText event_name = (EditText)findViewById(R.id.editText12);
            EditText event_place = (EditText)findViewById(R.id.editText13);
            EditText from_date = (EditText)findViewById(R.id.editText14);
            EditText from_time = (EditText)findViewById(R.id.editText15);
            EditText to_date = (EditText)findViewById(R.id.EditText16);
            EditText to_time = (EditText)findViewById(R.id.EditText17);
            CheckBox share = (CheckBox)findViewById(R.id.checkBox1);
            CheckBox alarm = (CheckBox)findViewById(R.id.checkBox2);

            long id2 = db.insertEvent(event_name.getText().toString(), event_place.getText().toString(), 
                    from_date.getText().toString(), to_date.getText().toString(),
                    from_time.getText().toString(),
                    to_time.getText().toString(),
                    share.isChecked(),alarm.isChecked());

            db.close();
            event_name.setText("");
            event_place.setText("");
            from_date.setText("");
            to_date.setText("");
            from_time.setText("");
            to_time.setText("");
            if (share.isChecked()) {
                share.setChecked(false);
             }
            if (alarm.isChecked()) {
                alarm.setChecked(false);
             }

            startActivity(new Intent("com.nazmy.HomeActivity"));

        }
    });


    cancel_btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            startActivity(new Intent("com.nazmy.HomeActivity"));

        }
    });
}

//This method shows the dialog when Button is clicked
public void selectFromtDate(View view) {
    DialogFragment mDialog = new SelectDateFragment();
    mDialog.show(getFragmentManager(), "DatePicker");
    whichDateBtn = 0;
}

public void selectToDate(View view) {
    DialogFragment mDialog2 = new SelectDateFragment();
    mDialog2.show(getFragmentManager(), "DatePicker");
    whichDateBtn = 1;
}

//This method will update the EditText field with the following code.
public void populateSetDate(int year, int month, int day) {
    mEdit = (EditText)findViewById(R.id.editText14);    //brbt el editText (mEdit) be el text ely hayzhar fel text Field
    mEdit2 = (EditText)findViewById(R.id.EditText16);

    if(whichDateBtn==0) {
        mEdit.setText(month+"/"+day+"/"+year);
    }
    else if (whichDateBtn == 1) {
        mEdit2.setText(month+"/"+day+"/"+year);
    }   
}

//This subclass includes method to display the datepicker fragment to the user. 
//It also has the method to handle the event on setting the date.

 public class SelectDateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

     //The onCreateDialog() method creates a calendar object.
     //Using this object the current day, month and year that will be retrieved.
     //This current instance will return to the activity to display the date picker with the current date by default.
     //onDateSet() method calls the populateSetDate() with the selected date parameters.

     @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            final Calendar calendar = Calendar.getInstance();
            int yy = calendar.get(Calendar.YEAR);
            int mm = calendar.get(Calendar.MONTH);
            int dd = calendar.get(Calendar.DAY_OF_MONTH);
            return new DatePickerDialog(getActivity(), this, yy, mm, dd);
        }

        public void onDateSet(DatePicker view, int yy, int mm, int dd) {
            populateSetDate(yy, mm+1, dd);
        }
    }

 ////////////////////////////////////////////////////////////////////////////////////////////////////////
 //      Time Pickers
 ////////////////////////////////////////////////////////////////////////////////////////////////////////
 public void selectFromTime(View v) {
        DialogFragment newFragment = new TimePickerFragment();
        newFragment.show(getFragmentManager(), "TimePicker");
        whichTimeBtn = 0;
    }
 public void selectToTime(View v) {
        DialogFragment newFragment = new TimePickerFragment();
        newFragment.show(getFragmentManager(), "TimePicker");
        whichTimeBtn = 1;
    }

 public void populateSetTime(int hour, int min) {
        mEditFromTime = (EditText)findViewById(R.id.editText15);    //brbt el editText (mEdit) be el text ely hayzhar fel text Field
        mEditToTime = (EditText)findViewById(R.id.EditText17);

        if(whichTimeBtn==0) {
            mEditFromTime.setText(hour+":"+min);
        }
        else if (whichTimeBtn == 1) {
            mEditToTime.setText(hour+":"+min);
        }   
    }

 public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            // Use the current time as the default values for the picker
            final Calendar c = Calendar.getInstance();
            int hour = c.get(Calendar.HOUR_OF_DAY);
            int minute = c.get(Calendar.MINUTE);

            // Create a new instance of TimePickerDialog and return it
            return new TimePickerDialog(getActivity(), this, hour, minute,DateFormat.is24HourFormat(getActivity()));
        }

        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            // Do something with the time chosen by the user
            populateSetTime(hourOfDay, minute);
        }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_create_event, menu);
    return true;
}

public void DisplayEvent(Cursor c)
{
    Toast.makeText(this,
    "id : " + c.getString(0) + "\n" +
    "Event Name: " + c.getString(1) + "\n" +
    "Place : " + c.getString(2) + "\n" +
    "From Date : " + c.getString(3)+ "\n" +
    "From Time : " + c.getString(5)+ "\n" +
    "To Date : " + c.getString(4)+ "\n" +   
    "To Time : " + c.getString(6),
    Toast.LENGTH_LONG).show();
}
}

`提前谢谢:)

4

1 回答 1

1

你有两个选择。从您的数据库中获取数据,然后将该数据作为附加EventsFragment.onListItemClick()信息传递给您的第二个。或者您可以传递单击项目的 id,并在您的第二个中执行数据库操作。无论哪种方式,您都必须使用 Intents 传递数据ActivityIntentActivity

public class EventsFragment extends SherlockListFragment {
.
.
    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        Intent intent = new Intent(this, CreateEventActivity.class);

        // OPTION 1: Fetch data here, and pass it with the intent
        db = new DBAdapter(getActivity());
        db.open();

        Cursor c = db.getEvent(id+1);

        intent.putExtra("id", c.getString(1));
        intent.putExtra("event_name", c.getString(2));
        // Do this for all your values.

        db.close();
        startActivity(intent);



        // OPTION 2: Pass the id of the selected item, and fetch the data in second activity
        intent.putExtra("selected_id", id+1);
        startActivity(intent);

    }

}

然后,CreateEventActivity您必须获得您选择通过Intent.

@Override
protected void onCreate(Bundle savedInstanceState) {
    .
    .
    Intent intent = getIntent();

    // If you chose option 1, you have to get all the extras you attached to the Intent.
    long id = intent.getLongExtra("id", -1)
    String eventName = intent.getStringExtra("event_name", "defValue");
    // Fetch all the values here..

    // If you chose option 2, you fetch the id and do the database operations to retrive
    // the data
    long id = intent.getLongExtra("selected_id", -1);

    // DB stuff here..

}
于 2013-03-01T03:27:59.917 回答