Can't handle Intent properly in Android - android

I'm developing a Data Entry system wherein the user can save, search and update data. In this activity, I'm using EditTexts, spinners and a button (to search for Clinic Names). When the user is finish filling those EditTexts and spinners, he can search for Clinic using the Button. This button will trigger the intent to another activity.
The other activity has a EditText(with TextFilters and can search for Clinic) and a ListView which shows the results upon search. When the result shows up, the user can click the ListView Items and that will trigger to the previous activity and will fill-out the Clinic Name to the EditText assigned to it.
My problem is when the user clicked the ListView item, and goes to the previous activity, those data from the EditTexts and Spinners were cleared up!
I want to happen is that, when I search for Clinic Name and goes to the another activity, those data that was already filled-outwill not be cleared or lost.
MainActivity.java
//Getting clinic address
txt_DClinic.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent myIntent = new Intent(getApplicationContext(), SearchClinicActivity.class);
startActivity(myIntent);
}
});
SearchClinicActivity.java
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> listView, View view,
int position, long id) {
TextView tv_clinicId = (TextView) view.findViewById(R.id.textviewId);
String clinicID = tv_clinicId.getText().toString();
Intent intent = new Intent(view.getContext(), MainActivity.class);
intent.putExtra("clinic_id", clinicID);
intent.putExtra("signal", "2");
intent.putExtra("from", "SearchClinic");
intent.putExtra("LoginOrSearch", "ImSearchClinic");
finish();
view.getContext().startActivity(intent);
}
});

When you are clicking on list item and opening intent. set flag to intent.
intent.setFlag(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
and see if its working or not.
if its not than try one thing is that. you are passing extras in intent. at that time pass the values for edittext and item number of spinner. and in onCreate method of Main activity set text of edittext with that extra. and set selected value for spinner.
edtTxt.settext(getIntent().getExtras().get("Text"));
sp.setSelection(getIntent().getExtras().get("Value"));
Hope it Helps!!!

Call setSaveEnabled(true) or add android:saveEnabled="true" to layout of Views you want data retained for.
MyEditText.setSaveEnabled(true);
or
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:saveEnabled="true"/>
Same for Spinner , or any other View you need to retain data of.

Related

Update list view from 2nd activity | Android

hi i have RecyclerView in that when user click on one row i am making changes in ui and updating view now i want when user click one one icon of that row it opening 2nd activity in that i am passing object of that position . and on 2nd activity if tick it i am not able to update view of that RecyclerView .. any way ? ya by doing static arreylist i can do it but cant do static as there is some problem .. part form static other way
icon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent n = new Intent(mContext, PlayerInfo.class);
Players mData = (Players) v.getTag();
((Activity) mContext).startActivityForResult(n, 1111);
}
});
As you code shows,you can start second activity by call startActivityForResult and pass your data to it.
when data changed in 2nd activity,you save the new data in one object and when the activity finishes,you pass it to first activity,and you also need to add some code to handle the new object from 2nd activity in onActivityResult,just copy data to your old data for specific position.

How to pass the value of a pressed button

I was wondering how to pass the value of a button to another activity. I have a screen with 7 buttons, and each button shows different data from the SQLitedatabase. So I was wondering how I can implement that in the activity so that I can select different data from the database, dependable which button was clicked in the previous activity. I know I have to use Bundles and Intents, but I cannot find how I can implement that so that the Activity knows which button was clicked and which data to select. Thanks!
How to pass the value of a pressed button
Buttons don't really have a "value". You could use the button's ID, though:
public void onClick(View v) {
startActivity(new Intent(this, YourOtherActivity.class)
.putExtra(YourOtherActivity.EXTRA_SOME_KEY, v.getId()));
}
This will pass the widget ID of the Button that was clicked in an extra (named YourOtherActivity.EXTRA_SOME_KEY) to YourOtherActivity. YourOtherActivity, in onCreate() or elsewhere, could call getIntent().getIntExtra(EXTRA_SOME_KEY, -1)) to retrieve the widget ID of the button that was clicked, then use a switch statement or some such to route your behavior as needed.
Set it with
Intent myIntent = new Intent(this, ACTIVITY.class);
myIntent.putExtra("buttonValue", BUTTON VALUE);
startActivity(myIntent);
Get it with
getIntent().getStringExtra("buttonValue")
Button Text
String buttonText = button.getText().toString()

How can I reload a single activity with new data each time the activity is started?

I have a list view with over 100 items. Each of these items show description when clicked on them. The problem is that I don't want over a hundred activities to display the description of each item instead I want to implement a single activity which refreshes the whole view and sets it to the new data depending upon the item clicked.How can I achieve this?
Thank you.
Create a new activity that only displays the details of your selected item. This activity is created for each item but you would open it, press the back button open it again with different content. Have a look at Intent and their extras as how to pass the data to your details activity.
try like this
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Intent i=new Intent(yourActivity.this,next.class);
String s = this.getListAdapter().getItem(position);
//query the database for the row with this string.. and get the necessary information
i.putExtra("name",s1);
i.putExtra("id",s2);
startActivity(i);
}

How to get the previous item of clicked listviewitem

I have two activities. In activity1 , I have list view which is populated from the database. On item click it should go to activity2. Activity2 contains two buttons (next and previous) and display the product details. In stead of "next" and "previous" button text, i was trying to get the previous item of clicked listview item and set text in button. and so for the "next" button.
myList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
MyClass item = (MyClass) adapter.getItem(position-1);
Intent i = new Intent(this, Activity2.class);
i.putExtra("item",item.toString());
startActivity(Activity2);
}
}
and in activity2.class
buttonprevious = (Button) findviewById(R.id.previous);
Bundle b = getIntent().getExtras();
String preitem = b.getString("item");
buttonprevious.setText(preitem);
Is this the correct way or am i doing something wrong??
How can I display product details of next and previous items??
How to use the view flipper in this case?
Thanks ..
You can use getItem(int position) method of the adapter, which return you the list item of the specified position.
(OR)
Get and Store all the database data into one static List.
Set adapter with that list.
In Activity2 access data from that list.

How to create clickable listview in Android using Sqlite3?

I have created listview in Android to show my records. And records are shown as well. Now I want to click on particular record from that listview to show/view particular record. And that record must show another layout for that profile. How to achieve it?
Create a onItemClick() event for the listview. Then, when you click on an item, a new intent will be created that will start a child activity that will display the particular record.
listView1.setOnItemClickListener(new ListView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> listView, View itemView, int itemPosition, long itemId)
{
Intent launchActivity = new Intent(MyActivity.this, OtherActivity.class);
startActivity(launchActivity);
}
});

Categories

Resources