can't pass listView selected item to textview - java

I created two dialogs, the first one opens the second dialog that would access the listview and it did, the second dialog selects specific item in the listView and transfers that item to the textview in the first dialog... I messed up my logic, how can I do this?
here's the first dialog:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_students_dialog);
imb1=(ImageButton)findViewById(R.id.im1);
imb2=(ImageButton)findViewById(R.id.im2);
imb3=(ImageButton)findViewById(R.id.im3);
text1=(TextView)findViewById(R.id.Text1);
text2=(TextView)findViewById(R.id.Text2);
text3=(TextView)findViewById(R.id.Text3);
/* Get values from Intent */
Intent intent = getIntent();
String name = intent.getStringExtra("Title1");
text1.setText(name);
ie1=(EditText)findViewById(R.id.edit1);
ib=(Button)findViewById(R.id.dialog_button_cancel);
ib1=(Button)findViewById(R.id.dialog_button_ok);
ib.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(InsertStudent.this, StudentsActivity.class);
startActivity(intent);
}
});
ib1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String data1 = ie1.getText().toString();
String data2 = ie2.getText().toString();
mySQLiteAdapter.insertSchool(data1, data2);
updateList();
Intent myIntent = new Intent(InsertStudent.this, StudentsActivity.class);
startActivity(myIntent);
}
});
imb1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(InsertStudent.this, Add1.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(myIntent);
}
});
imb2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(InsertStudent.this, Add2.class);
startActivity(myIntent);
}
});
imb3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(InsertStudent.this, Add3.class);
startActivity(myIntent);
}
});
}
private void updateList(){
cursor.requery();
}
}
here's the second dialog:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a_add_class_students);
listContent = (ListView)findViewById(R.id.listView1);
mySQLiteAdapter = new Database(this);
mySQLiteAdapter.openToWrite();
cursor = mySQLiteAdapter.queueSchoolAll();
button1 = (Button) findViewById(R.id.Button01);
button2 = (Button) findViewById(R.id.dialog_button_ok);
// Capture button clicks
button1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent myIntent = new Intent(Add1.this, InsertSubject.class);
startActivity(myIntent);
}
});
cursor.requery();
String[] from = new String[]{Database.KEY_ID2, Database.KSCHOOL, Database.KSCHOOLCODE};
int[] to = new int[]{R.id.rid, R.id.rt1, R.id.rt2};
cursorAdapter =
new SimpleCursorAdapter(this, R.layout.row_school, cursor, from, to);
listContent.setAdapter(cursorAdapter);
listContent.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Cursor cursor = (Cursor) arg0.getItemAtPosition(arg2);
final int item_id = cursor.getInt(cursor.getColumnIndex(Database.KEY_ID2));
sdid.sdid(item_id);
Intent intent=new Intent(Add1.this,DetailsSchool.class);
startActivity(intent);
}
});
cursor.requery();
listContent.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Cursor cursor = (Cursor) arg0.getItemAtPosition(arg2);
final int item_id = cursor.getInt(cursor.getColumnIndex(Database.KSCHOOLCODE));
Intent intent = new Intent(getApplicationContext(),InsertStudent.class);
intent.putExtra("Title1", item_id);
startActivity(intent);
}
});
cursor.requery();
}
private void updateList(){
cursor.requery();
}
}

you've called setOnItemClickListener twice for the same listview in your second dialog

Related

Call a fragment from an activity with a fragmenttransaction

what I am trying to do is call fragment from an activity with a FragmentTransaction, at that point it works for me, the problem is that when the activity calls the fragment, the two are presented on the screen.
Activity java code :
public class ElectricalCalculators extends AppCompatActivity implements View.OnClickListener {
Button kwbutton , evbutton ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_electrical_calculators);
findViewById(R.id.menu_back).setOnClickListener(this);
findViewById(R.id.menu_home).setOnClickListener(this);
Button kwbutton = (Button) findViewById(R.id.ampsBoutton);
kwbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent( v.getContext() , AmpsConversion.class);
startActivityForResult(intent,0);
}
});
Button evbutton = (Button) findViewById(R.id.evBoutton);
evbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent( v.getContext() , ElectronVoltsConversion.class);
startActivityForResult(intent,0);
}
});
Button jbutton = (Button) findViewById(R.id.joulesBoutton);
jbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent( v.getContext() , JoulesConversion.class);
startActivityForResult(intent,0);
}
});
Button Kwbutton = (Button) findViewById(R.id.kwBoutton);
Kwbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent( v.getContext() , KwConversion.class);
startActivityForResult(intent,0);
}
});
Button Kwhbutton = (Button) findViewById(R.id.kwhBoutton);
Kwhbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent( v.getContext() , KwhConversion.class);
startActivityForResult(intent,0);
}
});
}
#Override
public void onClick(View v) {
Class clazz = null;
FragmentManager fragmentManager = getSupportFragmentManager();
if (v.getId() == R.id.menu_back){
fragmentManager.beginTransaction().replace(R.id.elctricalCalculator, new Fragmen2()).commit();
}
/** switch (v.getId()) {
case R.id.menu_back:
//clazz = Fragmen2.class;
fragmentManager.beginTransaction().replace(R.id.elctricalCalculator, new Fragmen2()).commit();
break;
case R.id.menu_home:
clazz = MainActivity.class;
break;
/**case R.id.five_tabs_changing_colors:
clazz = FiveColorChangingTabsActivity.class;
break;*/
//}
// startActivity(new Intent(this, clazz));
}
}
Fragment code :
public class Fragmen2 extends Fragment {
Button Tboton,
Lboton,
Pboton,
Dboton,
wboton,
eboton;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_fragmen2, container, false);
Tboton = (Button)view.findViewById(R.id.boton1fragmen2); /**boton que conecta el fragment 2 con la actividad temperatura */
Tboton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent Tboton = new Intent( getActivity() ,Temperatura.class);
startActivity(Tboton);
}
});
Lboton =(Button)view.findViewById(R.id.boton2fragmen2);
Lboton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent Lboton = new Intent(getActivity() , Dimensions.class);
startActivity(Lboton);
}
});
View v = inflater.inflate(R.layout.fragment_fragmen1, container, false);
/**Pboton = (Button)view.findViewById(R.id.boton3fragmen2);
Pboton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});*/
Dboton = (Button)view.findViewById(R.id.boton4fragmen2);
Dboton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent Dboton = new Intent(getActivity(),Dimensions.class);
startActivity(Dboton);
}
});
wboton = (Button)view.findViewById(R.id.boton3fragmen2);
wboton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent wboton = new Intent(getActivity(),Weight.class);
startActivity(wboton);
}
});
eboton = (Button)view.findViewById(R.id.boton4fragmen2);
eboton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent eboton = new Intent(getActivity(),ElectricalCalculators.class);
startActivity(eboton);
}
});
return view;
}
}

app forces close every time i open a new class

i have an app that has a int called Money and i .putExtra it to go to another class but every time i push the button to go to that class it forces close the app.
here is the code i used to send the int
int Money = 0;
GoToSettings.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(getApplicationContext(), settings.class);
intent.putExtra("money",Money);
startActivity(intent);
}
});
and here is where i receive it and try to save it
public class settings extends Activity {
int LoadDef = 0;
Button Save,Load,ClsSave;
Intent intent = getIntent();
int Money = intent.getIntExtra("money", 0);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
Save = (Button) findViewById(R.id.Save);
Load = (Button) findViewById(R.id.Load);
ClsSave = (Button) findViewById(R.id.ClsSave);
Save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
SharedPreferences sharedPreferences=getSharedPreferences("MoneySave", Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putInt("MoneySave", Money);
editor.commit();
}
});
could someone help me fine out why it forces close?
The problem is here :
Intent intent = getIntent();
you should get extra from your intent , in onCreate() not in class definition
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
Intent intent = getIntent();
int Money = intent.getIntExtra("money", 0);

How to work with onBackPressed() in Android?

I have Activity 'A', Activity 'B', Activity 'C', Activity 'D', Activity 'E', Activity 'E' . I want to exit the Activity click on onBackPressed(). When i run the app it goes to Activity 'A' it does not exit the app. How can i implement with this.
Here is my Code
public class MainActivity extends Activity {
TableLayout table_layout;
EditText firstname_et, lastname_et;
Button addmem_btn;
DatabaseHelper databaseHelper = new DatabaseHelper(this);
protected SQLiteDatabase db;
ProgressDialog PD;
ImageView imgButtonBack;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.performance_report);
table_layout = (TableLayout) findViewById(R.id.tableLayout_PerformanceReport);
//ImageView imgButtonBack;
imgButtonBack = (ImageView)findViewById(R.id.imagBackButton);
imgButtonBack.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(MainActivity.this, Employee_List.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
//finish();
}
});
}
}
#Override
public void onBackPressed()
{
AlertDialog.Builder alertbox = new AlertDialog.Builder(MainActivity.this);
alertbox.setTitle("Do you wish to exit ?");
alertbox.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
// finish used for destroyed activity
finish();
}
});
alertbox.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
// Nothing will be happened when clicked on no button
// of Dialog
}
});
alertbox.show();
}
}
Thanks to Appreciate.
You can try this in onBackPressed:
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
Which exits the application. And works fine for me. Hope it helps.
call this.finish() while you open your new activity(i.e going from activty A to some other activity). In your case write this
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(MainActivity.this, Employee_List.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
MainActivity.this.finish();
}
});
You can do something like this -
private exit = false;
public void onBackPressed() {
if (exit)
Home.this.finish();
else {
Toast.makeText(this, "Press Back again to Exit.",
Toast.LENGTH_SHORT).show();
exit = true;
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
exit = false;
}
}, 3 * 1000);
}
}

how to change next activity background when i press button in current activity

i want to change the background for next activity when i press a button in my current activity & i have 32 buttons and all will go to same activity but i want to change the back ground of it according to button prees so it will change 32 times I try this but it doesn't work
buttons[0].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(Sori.this,listnameseries.class);
myIntent.putExtra("namemovies","fattetl3bet");
startActivity(myIntent);
buttons[0].setBackgroundResource(R.drawable.back1);
}
});
buttons[1].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(Sori.this,listnameseries.class);
myIntent.putExtra("namemovies","sikkarwasat");
startActivity(myIntent);
buttons[0].setBackgroundResource(R.drawable.back254);
}
});
buttons[2].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(Sori.this,listnameseries.class);
myIntent.putExtra("namemovies","superfamily");
startActivity(myIntent);
buttons[0].setBackgroundResource(R.drawable.back2978);
}
});
buttons[3].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(Sori.this,listnameseries.class);
myIntent.putExtra("namemovies","wiladaminalkhasira1");
startActivity(myIntent);
buttons[0].setBackgroundResource(R.drawable.back27);
}
});
buttons[4].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(Sori.this,listnameseries.class);
myIntent.putExtra("namemovies","wiladaminalkhasira2");
startActivity(myIntent);
buttons[0].setBackgroundResource(R.drawable.back280);
}
});
buttons[5].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(Sori.this,listnameseries.class);
myIntent.putExtra("namemovies","wiladaminalkhasira3");
startActivity(myIntent);
buttons[0].setBackgroundResource(R.drawable.back278);
}
});
buttons[6].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(Sori.this,listnameseries.class);
myIntent.putExtra("namemovies","zamanlbarghout1");
startActivity(myIntent);
buttons[0].setBackgroundResource(R.drawable.back278);
}
});
buttons[7].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(Sori.this,listnameseries.class);
myIntent.putExtra("namemovies","zamanlbarghout2");
startActivity(myIntent);
buttons[0].setBackgroundResource(R.drawable.back278);
}
});
buttons[8].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(Sori.this,listnameseries.class);
myIntent.putExtra("namemovies","ta7ounalchar1");
startActivity(myIntent);
buttons[0].setBackgroundResource(R.drawable.back2896);
}
});
buttons[9].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(Sori.this,listnameseries.class);
myIntent.putExtra("namemovies","ta7ounalchar2");
startActivity(myIntent);
buttons[0].setBackgroundResource(R.drawable.back287);
}
});
buttons[10].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(Sori.this,listnameseries.class);
myIntent.putExtra("namemovies","sar5troo7");
startActivity(myIntent);
buttons[0].setBackgroundResource(R.drawable.back2879);
}
});
buttons[11].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(Sori.this,listnameseries.class);
myIntent.putExtra("namemovies","sabaya1");
startActivity(myIntent);
buttons[0].setBackgroundResource(R.drawable.back2789);
}
});
buttons[12].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(Sori.this,listnameseries.class);
myIntent.putExtra("namemovies","sabaya2");
startActivity(myIntent);
buttons[0].setBackgroundResource(R.drawable.back234);
}
});
buttons[13].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(Sori.this,listnameseries.class);
myIntent.putExtra("namemovies","sabaya3");
startActivity(myIntent);
buttons[0].setBackgroundResource(R.drawable.back2435);
}
});
buttons[14].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(Sori.this,listnameseries.class);
myIntent.putExtra("namemovies","sabaya4");
startActivity(myIntent);
buttons[0].setBackgroundResource(R.drawable.back2345);
}
});
buttons[15].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(Sori.this,listnameseries.class);
myIntent.putExtra("namemovies","sabaya5");
startActivity(myIntent);
buttons[0].setBackgroundResource(R.drawable.back2435);
}
});
buttons[16].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(Sori.this,listnameseries.class);
myIntent.putExtra("namemovies","sana3oud");
startActivity(myIntent);
buttons[0].setBackgroundResource(R.drawable.back2345);
}
});
buttons[17].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(Sori.this,listnameseries.class);
myIntent.putExtra("namemovies","ta7tasama2alwatan");
startActivity(myIntent);
buttons[0].setBackgroundResource(R.drawable.back2435);
}
});
buttons[18].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(Sori.this,listnameseries.class);
myIntent.putExtra("namemovies","natrin");
startActivity(myIntent);
buttons[0].setBackgroundResource(R.drawable.back26546);
}
});
buttons[19].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(Sori.this,listnameseries.class);
myIntent.putExtra("namemovies","7oudoudchakika");
startActivity(myIntent);
buttons[0].setBackgroundResource(R.drawable.back2546);
}
});
buttons[20].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(Sori.this,listnameseries.class);
myIntent.putExtra("namemovies","yasmin3atik");
startActivity(myIntent);
buttons[0].setBackgroundResource(R.drawable.back2546);
}
});
buttons[21].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(Sori.this,listnameseries.class);
myIntent.putExtra("namemovies","znoodelset");
startActivity(myIntent);
buttons[0].setBackgroundResource(R.drawable.back2546);
}
});
buttons[22].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(Sori.this,listnameseries.class);
myIntent.putExtra("namemovies","l3btmoot");
startActivity(myIntent);
buttons[0].setBackgroundResource(R.drawable.back25464);
}
});
add an extra to your intent, for example a resource id
myIntent.putExtra("resource_id", R.color.myPreferedBackground);
then in the next activity in onCreate():
int bg = getIntent.getIntExtra("resource_id");
myLayout.setBackgroundResource(bg);
Along with the intent send the information regarding the color to next activity.
In next activity onCreate change the color based on information.

How to call activity from the items of the spinner

I have a page which consist of a spinner and a submit button. What I want to achieve is when user selects an item in the list and click on submit, it should take him to an other layout having a webview. Each item in the spinner should open different .html page in the layout.
What I have now is the item is being selected from the spinner, but I'm not sure how to perform onclick listener to it...
code for main activity.java is:
public class beef extends Activity {
private Spinner spinner1;
private ImageButton btnSubmit;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.beef);
addListenerOnButton();
addListenerOnSpinnerItemSelection();
}
public void addListenerOnSpinnerItemSelection(){
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
}
//get the selected dropdown list value
public void addListenerOnButton() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
btnSubmit = (ImageButton) findViewById(R.id.imageButton1);
btnSubmit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(v.getId() == btnSubmit.getId())
{
Intent intent = new Intent(beef.this,display.class);
intent.putExtra("urlpath", "animalbites.html");
startActivity(intent);
}
}
});
}
}
code of CustomOnItemSelectedListener.java is:
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
if (arg2 == 0) // First item selected
{
//Here I need to give an id for the .html file
}
else if (arg2 == 1) // Second
{
//Here I need to give an id for the .html file
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
and the display.java is:
public class display extends Activity implements OnClickListener {
private WebView webView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
Intent intent=getIntent();
String mUrl=intent.getStringExtra("urlpath");
webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("file:///android_asset/www/"+mUrl);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
i am not exactly getting your problem, but i ll answer according to what i got:
Why are you concerned of adding an onClickListener to the spinner?
Just add onClickListener to the button and on the click of that button do:
String selecteditemName=(String)spinner1.getSelectedItem();
//spinner1 having items : "file1" , "file2"
Intent i=new Intent(this, yournewActivity.class);
i.putExtra("selected_item", selecteditemName);
startActivity(i);
Now in that activity (yournewActivity) containing the webview use this code in onCreate() method:
String item;
Intent i=getIntent();
item = i.getStringExtra("selected_item");
Now you have your spinner selection in the String 'item', you can now do whatever you want:
My point is that : You should use your spinner selection as ID for populating the webview.
If you still want to use any IDs other than the ones present in the spinner then
make an extra java class Idgetter,
class idgetter
{
public static String getID(String name)
{
if(name.equals("file1"))
return "requiredfile1.html";
else if(name.equals("file2"))
return "requiredfile2.html";
}
}
Now, Do exactly as the above code says and after you have the spinner selection in the variable item , use:
String id=idgetter.getID(item); //in younewActivity
You are finally done.
By using Intent's putExtra method.
Intent mIntent = new Intent(this, display.class);
mIntent.putExtra("urlpath", "put value in here");
startActivity(mIntent);
Populate the spinner with number of html pages and Use spinner.getSelectedItem().toString() inside the addListenerOnButton() method. By doing do, you will get the selected html page and pass it using intent to the next layout.
CODE:
`public class beef extends Activity {
private Spinner spinner1;
private ImageButton btnSubmit;
int final websiteA = 1;
int final websiteB = 2;
int final websiteC = 3;
String selectedHtmlPage = "";
.........
.........
.........
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3)
{
switch(arg2)
{
case websiteA :
selectedHtmlPage = "websiteA.html";
break;
case websiteB :
selectedHtmlPage = "websiteB.html";
break;
case websiteC :
selectedHtmlPage = "websiteC.html";
break;
}
}
public void addListenerOnButton()
{
btnSubmit = (ImageButton) findViewById(R.id.imageButton1);
btnSubmit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
if(v.getId() == btnSubmit.getId())
{
Intent intent = new Intent(beef.this,display.class);
intent.putExtra("urlpath", selectedHtmlPage);
startActivity(intent);
}
}
});
}
}`
Solved it, its simple. Here is what I did:
public void addListenerOnSpinnerItemSelection(){
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setOnItemSelectedListener(this);
}
- - -
#Override
public void onClick(View v) {
if(v.getId() == btnSubmit.getId())
{
Intent intent = new Intent(beef.this,display.class);
intent.putExtra("urlpath", mLink);
startActivity(intent);
}
}
});
}
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
if(arg2==0){
mLink="Beef html/BBQ_Meatballs_Recipes.html";
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}

Categories

Resources