where to start..
I’m new to android and trying to create a small app for android that would show time table (5 days and 8 hours), those’d be displayed as a table of 40 squares. User can add an event by choosing Day(from spinner), Time(spinner), add a note (max 15 characters) and choose a background color. All that has to be added to a string array and send to main activity. From there by getting day and time slot values from that string array appropriate square will be updated with text and background color from the same string array. I’ve been trying to solve this for about four days now, but I just can’t get my head round this… Here’s some code:
Main Activity:
package com.tt;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button settings = (Button)findViewById(R.id.settings);
Intent a = getIntent();
String[] add = a.getStringArrayExtra("strings");
setContentView(R.layout.activity_main);
TextView detail = (TextView)findViewById(R.id.m1);
//detail.setText(notes);
settings.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(v.getContext(),Settings.class);
startActivityForResult(i,0);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Add event:
package com.tt;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.content.Intent;
public class Add extends Activity implements OnClickListener, OnCheckedChangeListener {
String[] DOW;
String[] time;
String clr;
EditText nts;
boolean click = true;
Button save;
RadioGroup days;
RadioGroup times;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.add);
initialize();
DOW = getResources().getStringArray(R.array.DOW);
Spinner s1 =(Spinner)findViewById(R.id.spinner1);
ArrayAdapter<String>adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,DOW);
s1.setAdapter(adapter);
s1.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?>arg0,View arg1, int arg2, long arg3) {
int index = arg0.getSelectedItemPosition();
Toast.makeText(getBaseContext(), "Chosen Day of the Week is "+ DOW[index], Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?>arg0){
// nothing
}
});
time = getResources().getStringArray(R.array.time);
Spinner s2 = (Spinner)findViewById(R.id.spinner2);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item,time);
s2.setAdapter(adapter2);
s2.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?>arg0,View arg1, int arg2, long arg3) {
int index = arg0.getSelectedItemPosition();
Toast.makeText(getBaseContext(), "Time is set to "+ time[index], Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// nothing
}
});
}
private void initialize() {
save = (Button)findViewById(R.id.save);
save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String notes = nts.getText().toString();
String day = DOW.toString();
String tim = time.toString();
//String color = clr.
Bundle b = new Bundle();
/*bag.putString("key1", day);
//bag.putString("key2",tim);
//bag.putString("key3", notes);
//bag.putString("key4", color);*/
/*String[] added = new String[4];
added[0]=day;
added[1]=tim;
added[2]=color;
added[3]=notes;*/
//b.putStringArray("key",added);
String[] added = new String[] {day, tim, notes};
//b.putStringArray("key", new String[]{day, tim, notes});
Intent a = new Intent(Add.this, MainActivity.class);
a.putExtra("strings",added);
startActivity(a);
}
});
nts = (EditText) findViewById(R.id.notes);
//days.setOnCheckedChangeListener((OnCheckedChangeListener) this);
//times.setOnCheckedChangeListener((OnCheckedChangeListener) this);
}
@Override
public void onClick(View arg0) {
// nothing
}
@Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
switch(arg1) {
case R.id.blue:
clr = "blue";
break;
case R.id.green:
clr = "green";
break;
case R.id.yellow:
clr = "yellow";
break;
case R.id.red:
clr = "red";
break;
}
}
}
UPDATE
Right… I’ve cut down string array to just one string… Here’s the code that suppose to send data:
private void initialize()
{
save = (Button)findViewById(R.id.save);
nts = (EditText) findViewById(R.id.notes);
}
public void onClick(View arg0)
{
String notes = nts.getText().toString();
//String day = DOW.toString();
//String tim = time.toString();
Bundle b = new Bundle();
b.putString("AddedNote", notes);
Intent a = new Intent(Add.this, MainActivity.class);
a.putExtras(b);
startActivity(a);
/*String[] added = new String[] {notes};
a.putExtra("strings",added);
*/
};
And the “receiver”:
public class MainActivity extends Activity
{
String gotNotes;
TextView notes;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize();
Button settings = (Button)findViewById(R.id.settings);
Bundle gotPackage = getIntent().getExtras();
gotNotes = gotPackage.getString("AddedNote");
if (gotNotes != "")
{
notes.setText(gotNotes);
}
else{}
settings.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent(v.getContext(),Settings.class);
startActivityForResult(i,0);
}
});
}
private void initialize()
{
// TODO Auto-generated method stub
notes = (TextView)findViewById(R.id.m1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
The result I’m getting is caused by java.lang.nullpointerexception at line :
gotNotes = gotPackage.getString("AddedNote");
of the “receiver” activity..
It depend on your activity android:launchMode, I suspect you have it singleTask or similar. In this case you should override onNewIntent where you should receive the intent with your strings array.
Alternative solution, is to store data in a shared preference, you can serialize it as JSON object if needed.