我想在Android中編寫代碼以從日期選擇器中獲取保存日期並希望顯示在吐司中。我有一個網格10x10並單擊每個單元格Datepicker出現。我選擇一些日期,然後按Done,然後選擇保存在Sharedpreference對象中的日期。我在Log Cat中獲得了正確的保存結果,但我無法在吐司中獲得正確的值。如何從Android的Datepicker的DialogFragment中獲取返回值?
我的代碼:
MainActivity.java
package com.example.complexdatepicker;
import java.util.ArrayList;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.GridView;
public class MainActivity extends FragmentActivity
{
ArrayList<String> abc;
TestGrid tg;
GridView gv;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gv = (GridView) findViewById(R.id.gridView1);
abc = new ArrayList<String>();
for(int i=0;i<100;i++)
{
abc.add(String.valueOf(i));
}
tg = new TestGrid(MainActivity.this,MainActivity.this,abc);
gv.setAdapter(tg);
}
}
TestGrid.java
package com.example.complexdatepicker;
import java.util.ArrayList;
import java.util.Calendar;
import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.Toast;
public class TestGrid extends BaseAdapter
{
private ArrayList<String> abc;
private FragmentActivity activity;
private Context context;
String text;
int c = 0;
int x = 0;
DialogFragment newFragment;
public TestGrid(FragmentActivity activity , Context cont,ArrayList<String> abc)
{
super();
this.activity=activity;
this.context = cont;
this.abc = abc;
}
@Override
public int getCount()
{
return abc.size();
}
@Override
public Object getItem(int in)
{
return abc.get(in);
}
@Override
public long getItemId(int arg0)
{
return 0;
}
public class ViewHolder
{
public TextView txt;
}
@Override
public View getView(final int arg0, View arg1, ViewGroup arg2)
{
final SharedPreferences pref = context.getApplicationContext().getSharedPreferences("TestDate", 0);
ViewHolder view;
LayoutInflater inflator = activity.getLayoutInflater();
if(arg1==null)
{
view = new ViewHolder();
arg1 = inflator.inflate(R.layout.test_grid, null);
view.txt = (TextView) arg1.findViewById(R.id.txt);
arg1.setTag(view);
}
else
{
view = (ViewHolder) arg1.getTag();
}
View.OnClickListener alert = new OnClickListener()
{
public void onClick(View v)
{
Toast.makeText(activity.getApplicationContext(), abc.get(arg0)+" - Clicked ", Toast.LENGTH_SHORT).show();
newFragment = new SelectDateFragment();
Bundle args = new Bundle();
newFragment.setArguments(args);
// Getting Error here //
// The method setOnDateSetListener(new DatePickerDialog.OnDateSetListener() {}) is undefined for the type DialogFragment
newFragment.setOnDateSetListener(new DatePickerDialog.OnDateSetListener()
{
@Override
public void onDateSet(DatePicker view, int yy, int mm, int dd)
{
String saved_date = pref.getString("SavedDate", null);
Toast.makeText(activity.getApplicationContext(), "SavedDate : "+saved_date,Toast.LENGTH_LONG).show();
}
});
newFragment.show(activity.getSupportFragmentManager(), "DatePicker");
}
};
view.txt.setOnClickListener(alert);
DatePickerDialog.OnDateSetListener pickerListener = new DatePickerDialog.OnDateSetListener()
{
@Override
public void onDateSet(DatePicker view, int year,int monthOfYear, int dayOfMonth)
{
String saved_date = pref.getString("SavedDate", null);
Toast.makeText(activity.getApplicationContext(), "SavedDate : "+saved_date, Toast.LENGTH_LONG).show();
}
};
text = String.valueOf(abc.get(arg0));
view.txt.setBackgroundResource(R.drawable.txtbk);
view.txt.setTextSize(18);
view.txt.setTextColor(Color.BLACK);
view.txt.setGravity(Gravity.CENTER);
view.txt.setText(text);
return arg1;
}
@SuppressLint({ "ValidFragment", "NewApi" })
public class SelectDateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener
{
private DatePickerDialog.OnDateSetListener externalListener;
public void setOnDateSetListener(DatePickerDialog.OnDateSetListener listener)
{
this.externalListener = listener;
}
@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)
{
SharedPreferences pref = context.getApplicationContext().getSharedPreferences("TestDate", 0);
Editor et = pref.edit();
et.putString("SavedDate", String.valueOf(dd+"/"+mm+"/"+yy));
et.commit();
Log.d("SavedDate : ", String.valueOf(dd+"/"+mm+"/"+yy));
if(externalListener != null)
externalListener.onDateSet(view, yy, mm, dd);
}
public void onFinishEditDialog(String inputText)
{
}
}
}
我從我的代碼面臨的問題是,每當我點擊一個單元格,Date-picker就會出現Sharedpreference對象也開始在Toast中顯示。但它顯示了較舊的值。
從日期選擇器中選擇日期後,值保存在Sharedpreference對象中,但未找到任何技術在Toast中顯示最新保存的結果。
請幫助我,建議我一些好的解決方案。
得到以下錯誤,方法setOnDateSetListener(new DatePickerDialog.OnDateSetListener(){})未定義類型D ialogFragment,請幫我解決... – user3668350
你在'SelectDateFragment'中定義了方法嗎? – CurlyPaul
是的,我已經完成了所有的工作,正如你向我建議的那樣,我正在編輯我的問題......請通過它...... – user3668350