Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
285 views
in Technique[技术] by (71.8m points)

java - Android Sdk DatePickerFragment and TextView

i got the next problem. I want to pass the date i've entered using my DatePickerFragment to TextView, but i don't know how i can actually do it.

When i create TextView object in DatePickerFragment class and then set the text in it using onDateSet method - the app crashes. Maybe this question too basic, but i didn' find any suitable solution.

public class DatePickerFragment extends DialogFragment 
            implements DatePickerDialog.OnDateSetListener{

TextView tvDate1 = (TextView) getView().findViewByiD(R.id.tvDate);


@Override
public Dialog onCreateDialog(Bundle savedInstanceState){
    final Calendar c = Calendar.getInstance();
    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int day = c.get(Calendar.DAY_OF_MONTH);

    return new DatePickerDialog(getActivity(),this,year,month,day);
}

public void onDateSet(DatePicker view,int year, int month, int day){
       String

    StringBuilder sb = new StringBuilder();
    sb.append(day);
    sb.append("/");
    sb.append(month);
    sb.append("/");
    sb.append(year);
    tvDate1.setText(sb);


}
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your app crashed probably because of this

   TextView tvDate1 = (TextView) getView().findViewByiD(R.id.tvDate);

I guess your picker is in a different file and you are trying to initialize textview. findViewById is a method of activity class

Use a interface

http://developer.android.com/guide/components/fragments.html

Check the topic under communicating with activity

public class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {

    TheListener listener;

public interface TheListener{
    public void returnDate(String date);
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
listener = (TheListener) getActivity(); 

// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}

@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
Calendar c = Calendar.getInstance();
c.set(year, month, day);

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = sdf.format(c.getTime());
if (listener != null) 
{
  listener.returnDate(formattedDate); 

}

}
}

Then in MainActivity implement the interface and set the date to textview

public class MainActivity extends Activity implements DatePickerFragment.TheListener{

    Button b;
    TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b= (Button) findViewById(R.id.button1);
        tv= (TextView) findViewById(R.id.textView1);
        b.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                DialogFragment picker = new DatePickerFragment();
                picker.show(getFragmentManager(), "datePicker");
            }

        });
    }

    @Override
    public void returnDate(String date) {
        // TODO Auto-generated method stub
        tv.setText(date);
    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...