私はアンドロイドを使用してジョブポータル用のモバイルアプリを開発しています。リストビューがあるため、リストビューの日付列でイメージビューを使用しました。イメージビューをクリックすると、カレンダーイベントを自動的に追加する必要があります。以下は私が使用したコードです。アプリケーションを実行した後、エミュレータを表示するアプリが予期せず終了し、カレンダーイベントに関する応答がなく、すべて正常に動作する場合があります。助けてくれてありがとう。
Button btnLogin;
Button btnRegister;
Button btnsignUp;
ViewFlipper viewFlipper;
EditText etUserName;
EditText etPassword;
EditText etUserNameSignUp;
EditText etPasswordSignUp;
EditText etPhoneSignUp;
Button btnExit;
Button btnHome;
Button btnNotifications;
Button btnAvalability;
Button btnUpdate;
Button btnPublish;
Button btnStatus;
Button btnNotify;
String[] companies = new String[] { "Infosys", "Wipro", "Cisco", "ibm",
"tcs", "NWS" };
int[] images = new int[] { R.drawable.notifications,
R.drawable.notifications, R.drawable.notifications,
R.drawable.notifications, R.drawable.notifications,
R.drawable.notifications,
};
String[] dates = new String[] { "21/08/12", "22/08/12", "20/08/12",
"16/08/12", "18/08/12", "24/08/12", };
String[] type = new String[] { "Face To Face", "Written", "Online",
"telephonc", "group discussion", "telephonic" };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.home);
userName.add("Raghu");
password.add("Raghu");
viewFlipper = (ViewFlipper) findViewById(R.id.main);
btnLogin = (Button) findViewById(R.id.loginButton);
btnLogin.setOnClickListener(this);
btnRegister = (Button) findViewById(R.id.signupButton);
btnRegister.setOnClickListener(this);
btnsignUp = (Button) findViewById(R.id.signupButton);
btnsignUp.setOnClickListener(this);
etUserName = (EditText) findViewById(R.id.userEditText);
etPassword = (EditText) findViewById(R.id.pwEditText);
etUserNameSignUp = (EditText) findViewById(R.id.regNameEditText);
etPasswordSignUp = (EditText) findViewById(R.id.regEmailEditText);
etPhoneSignUp = (EditText) findViewById(R.id.regPhEditText);
btnHome = (Button) findViewById(R.id.homeButton);
btnHome.setOnClickListener(this);
btnExit = (Button) findViewById(R.id.exitButton);
btnExit.setOnClickListener(this);
btnNotifications = (Button) findViewById(R.id.listButton);
btnNotifications.setOnClickListener(this);
btnAvalability = (Button) findViewById(R.id.rightButton);
btnAvalability.setOnClickListener(this);
btnUpdate = (Button) findViewById(R.id.plusButton);
btnUpdate.setOnClickListener(this);
btnPublish = (Button) findViewById(R.id.publishButton);
btnPublish.setOnClickListener(this);
btnStatus = (Button) findViewById(R.id.updateButton);
btnStatus.setOnClickListener(this);
btnNotify = (Button) findViewById(R.id.notificationButton);
btnNotify.setOnClickListener(this);
// Each row in the list stores company name, interview date,
// notification icon and type of interview
List<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < 6; i++) {
HashMap<String, String> hm = new HashMap<String, String>();
hm.put("cmp", companies[i]);
hm.put("date", " " + dates[i]);
hm.put("img", Integer.toString(images[i]));
hm.put("type", " " + type[i]);
aList.add(hm);
}
// Keys used in Hashmap
String[] from = { "cmp", "date", "img", "type" };
// Ids of views in lvrow.xml
int[] to = { R.id.cmp, R.id.date, R.id.imgv, R.id.type };
// Instantiating an adapter to store each items
// R.layout.lvrow.xml defines the layout of each item
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList,
R.layout.listview, from, to);
// Getting a reference to listview of listvi.xml layout file
ListView listView = (ListView) findViewById(R.id.listview);
// Setting the adapter to the listView
listView.setAdapter(adapter);
/*listView.setOnItemClickListener(listener);*/
}
OnItemClickListener listener = new OnItemClickListener() {
// @Override
// onItemClick(AdapterView<?> parent, View view, int position, long id)
// {
// ((TextView) view.findViewById(R.id.yourTextViewId)).getText();
// // or do your stuff
// }
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
ImageView viewObj = (ImageView) arg1.findViewById(R.id.imgv);
viewObj.setImageResource(R.drawable.bgrighticon);
}
};
private void readContent(String uriString) {
Uri calendarUri;
Uri eventUri;
if (android.os.Build.VERSION.SDK_INT <= 7 )
{
//the old way
calendarUri = Uri.parse("content://calendar/calendars");
eventUri = Uri.parse("content://calendar/events");
}
else
{
//the new way
calendarUri = Uri.parse("content://com.android.calendar/calendars");
eventUri = Uri.parse("content://com.android.calendar/events");
}
}
private void addEvent(){
try {
ContentValues event = new ContentValues();
event.put("calendar_id", "1");
event.put("title", "tet event");
event.put("description", "hello this is testing of event");
event.put("eventLocation", "Ahmedabad");
Calendar c = Calendar.getInstance();
long date = c.getTimeInMillis();
event.put("dtstart", date);
event.put("dtend", date);
event.put("allDay", 1);
event.put("eventStatus", 1);
event.put("hasAlarm", 1);
Uri eventsUri = Uri.parse("content://calendar/events");
Uri url = getContentResolver().insert(eventsUri, event);
Log.e("uri", url.toString());
} catch (Exception e) {
Log.e("error", e.getMessage());
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.home, menu);
return true;
}
public void onClick(View v) {
if (v == btnLogin) {
if (userName.contains(etUserName.getText().toString())
&& password.contains(etPassword.getText().toString()))
{
viewFlipper.setDisplayedChild(3);
} else {
}
loggedin = true;
} else if (v == btnRegister) {
viewFlipper.setDisplayedChild(5);
} else if (v == btnsignUp) {// reister new user
// Take the values from the user entered data and save it in
// Arraylist
} else if (v == btnHome) {
if (loggedin) {
viewFlipper.setDisplayedChild(3);
} else {
viewFlipper.setDisplayedChild(0);
}
} else if (v == btnExit) {
System.exit(0);
} else if (v == btnNotifications) {
if (loggedin) {
viewFlipper.setDisplayedChild(4);
}
} else if (v == btnAvalability) {
if (loggedin) {
viewFlipper.setDisplayedChild(2);
}
} else if (v == btnUpdate) {
if (loggedin) {
viewFlipper.setDisplayedChild(1);
}
}
else if (v == btnPublish) {
if (loggedin) {
viewFlipper.setDisplayedChild(2);
}
} else if (v == btnStatus) {
if (loggedin) {
viewFlipper.setDisplayedChild(1);
}
} else if (v == btnNotify) {
if (loggedin) {
viewFlipper.setDisplayedChild(4);
}
}
}
}