我在通知页面中创建了一个列表视图,我想从DateTimePickerDemo向这个ListView添加项目:所以这是我的DateTimePickerDemo java代码和我的Anotification java代码
这是我的代码
public class DateTimePickerDemo extends notification implements OnClickListener, DateTimePickerDialog.DateTimeListener {
/**
* Called when the activity is first created.
*/
View v ;
ArrayList<String> listItems=new ArrayList<String>();
private MovieAdapter listAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.ButtonTime).setOnClickListener(this);
}
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
EditText edit = (EditText) findViewById(R.id.editText);
TextView addtext = (TextView) findViewById(R.id.Date);
int i = 1;
if (view.getId() == R.id.ButtonTime) {
showDateTimeDialog();
}
if (view.getId() == R.id.button) {
adapter.add(edit.getText().toString()+" "+addtext.getText().toString());
edit.setText("");
adapter.notifyDataSetChanged();
}
}
private void showDateTimeDialog() {
DateTimePickerDialog pickerDialog = new DateTimePickerDialog(this, false, this);
pickerDialog.show();
}
@Override
public void onDateTimeSelected(int year, int month, int day, int hour, int min, int am_pm) {
String text = day + "/" + month + "/" + year + " - " + hour + ":" + min;
if (am_pm != -1)
text = text + (am_pm == Calendar.AM ? "AM" : "PM");
((TextView) findViewById(R.id.Date)).setText(text);
}
public class notification extends AppCompatActivity {
ListView listView;
int [] movie_poster_resource = {R.drawable.ic_reunion};
String[] reunion_name;
String[] temps_rendez;
MovieAdapter adapter;
View view;
Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification);
listView= (ListView)findViewById(R.id.listView2);
temps_rendez = getResources().getStringArray(R.array.réunion_temps);
reunion_name = getResources().getStringArray(R.array.réunion_title);
int i=0;
adapter = new MovieAdapter(getApplicationContext(),R.layout.patient_name);
listView.setAdapter(adapter);
for (String titles: reunion_name)
{
MovieDataProvider dataProvider = new MovieDataProvider(movie_poster_resource[0],titles,temps_rendez[i]);
adapter.add(dataProvider);
i++;
}
}
}
public class MovieAdapter extends ArrayAdapter {
List list = new ArrayList();
public MovieAdapter(Context context, int resource) {
super(context, resource);
}
static class DataHandler{
ImageView Poster;
TextView title;
TextView rating;
}
@Override
public void add(Object object) {
super.add(object);
list.add(object);
}
@Override
public int getCount() {
return this.list.size();
}
@Override
public Object getItem(int position) {
return this.list.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row;
row = convertView;
DataHandler handler;
if (convertView == null){
LayoutInflater inflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.patient_name,parent,false);
handler = new DataHandler();
handler.Poster = (ImageView)row.findViewById(R.id.patient_picture);
handler.title = (TextView)row.findViewById(R.id.patient_name);
handler.rating = (TextView)row.findViewById(R.id.temps_rendez_vous);
row.setTag(handler);
}else{
handler = (DataHandler)row.getTag();
}
MovieDataProvider dataProvider;
dataProvider = (MovieDataProvider)this.getItem(position);
handler.Poster.setImageResource(dataProvider.getPatient_poster_resource());
handler.title.setText(dataProvider.getPatient_title());
handler.rating.setText(dataProvider.getTemps());
return row ;
}
}