Passing ListView to hint - android

I am trying to make an edit feature for my todo list app. What I want to do is to tap an item on the list and then I would be sent to the new page where I can edit the item on my list. I am currently getting a warning :
This text field does not specify an inputType or a hint
Issue: Looks for text fields missing inputType or hint settings
Id: TextFields
I was wondering how can I pass the text in my ListView to my second activity as the first thing to be edited in my EditText?
This is what I have so far:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_to_do);
etNewItem = (EditText) findViewById(R.id.etNewItem);
lvItems = (ListView) findViewById(R.id.lvItems); // now we have access to ListView
readItems(); // read items from file
todoAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, todoItems); //create adapter
lvItems.setAdapter(todoAdapter); // populate listview using the adapter
setupListViewListener();
setupEditItemListener();
}
private void launchEditItem() {
Intent i = new Intent(this, EditItemActivity.class);
startActivity(i);
}
private void setupEditItemListener() { // on click, run this function to display edit page
lvItems.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
launchEditItem();
}
});
}

I can't tell from what you have posted what exactly you are trying to pass. However, there are several things you can do.
(1) You can put a reference to your ListView in a subclass of Application. Application is a good place to put application-scope global variables.
(2) Similar to (1), put a reference to the adapter in your Application subclass.
(3) You can pass the query or other list setup parameters to the edit activity and let it requery.
(4) You can pass the index or other key info from the selected list item using putExtra() to the intent. You can pass updated info back to the main activity by passing back an intent. The main activity will receive the returned intent if it uses StartActivityWithResult().

Related

ListView item gets destroyed while moving to other activity

My first activity contains a listview with textviews in each cell and uses a custom adapter. So if you click on any of the items, it will open up a form activity containing textfields. The user can fill up the details and once they press the save form button the details appear on the listview. Now I am trying to add items to the list dynamically. I have created a button which when clicked adds a new instance item so that more users can register the same way. I have been able to implement these functions. However, my problem now is when i click on the newly added item and go to the form activity and click save, i am not able to see the newly added entry after i come back to the listview activity.All I see is the first entry alone. So i am guessing it gets destroyed as soon as i leave the activity. How to ensure all newly added items are not destroyed when i keep moving between these two activities.
Here is my code of the ListView Activity:
public class FormTableActivity extends Activity {
private PassengerListAdapter adapter;
Button add_passenger;
String mrzdata,ic_data,name_data;
SharedPreferences nPref;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.final_display_listview);
nPref = PreferenceManager.getDefaultSharedPreferences(this);
mrzdata = nPref.getString("MRZ", "");
name_data = nPref.getString("resultData", "");
ic_data = nPref.getString("icdata", "");
final ListView lv1 = (ListView) findViewById(R.id.custom_list);
adapter = new PassengerListAdapter(this);
adapter.add(new CustomerDetails(ic_data, name_data, mrzdata));
add_passenger = (Button) findViewById(R.id.add_user);
add_passenger.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// mrzdata = "";
// name_data = "";
// ic_data = "";
adapter.add(new CustomerDetails(ic_data, name_data, mrzdata));
}
});
lv1.setAdapter(adapter);
lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Intent intent = new Intent(v.getContext(), FirstActivity.class);
startActivity(intent);
}
});
}
The easiest way to pass data between Intent is
Intent intent = new Intent(getBaseContext(), your_list_view_activity.class);
intent.putExtra("String_Key", data1);
//data1 could be an array of string where you have hold the values previously
startActivity(intent);
now on your list_view_activity
Bundle extras = getIntent().getExtras();
if (extras != null) {
String [] value = extras.getString("String_Key");
}
This way you won't get any exception but you get to populate your listView if there is data.
Another way to get data is via SharedPreference but I won't recommend it as it increases the size of the app.
You have to save these newly added items somewhere , e.g. to a SQLite database and retrieve them on create to populate the listview
You can see here if You want, the code is commented ,
here I have a listview with custom adapter with two items
the feed's name and it's url
i add URL and name using a text input dialog (with two edit text), save to DB, and retrieve them on create to populate the listview
https://github.com/enricocid/iven-feed-reader/blob/master-as/project/app/src/main/java/com/iven/lfflfeedreader/mainact/ListActivity.java

Displaying updated ArrayList items properly within a fragment

basically my problem is this. I am trying to display a list view within a fragment set within the xml file, this list view gets its data from an ArrayList that gets its data from a form I created in another activity using Edit Texts which are put into an ArrayList and passed as a Parcelable Array List to the fragment class.
At the start of the fragment activity there of course are no values stored so I bring up the form activity for data input upon checking that there is no intent stored with the id I specify the passed Parcelable ArrayList as. The problem I have is that whenever I try populate the list view with more than one item (having the first item showing up, returning to the form to add another item in), it seems to only display the one item I add each time and not the items I added previously.
public class MasterFragment extends ListFragment {
private DetailFragment ingredDetails;
private ArrayList<Ingrediants> ingredList = new ArrayList<Ingrediants>();
private ArrayList<String> test2 = new ArrayList<String>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getActivity().getIntent();
Bundle extras = intent.getExtras();
if (extras != null) { //If extras are present
boolean data = extras.getBoolean("ingrediant", true);
if (data) {//Data present from form, retrieve from ArrayList<Ingrediants> and populate the ArrayList<String>
ingredList = getActivity().getIntent().getParcelableArrayListExtra("ingrediant");
test2.add(ingredList.get(i).ingrediantName);
setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, test2));
}//END OF IF DATA
}//END OF IF EXTRAS
else {//No data found, bring up the FormActivity
Intent intent3 = new Intent(getActivity(), FormActivity.class);
startActivity(intent3);
}//END OF ELSE
}//END OF onCreate()
I think it has something to do with how I am creating the list view and populating it with the ArrayList with the data got from the Parcelable ArrayList intent as it could be recreating the list view each time and only displaying the data supplied to it which in this case will be one item at a time. I've been stuck on this for a while now and was wondering if anyone has any ideas ? Thanks much.
The ArrayList you use (test2) to keep the data does not persist. You can either save the ingredients in sqlite db and call all items onCreate in your fragment, or move the test2 to the main activity and populate items before passing it with Intent bundle.

ListView items not getting populated when activity is launched

Here is my scenario:
I have a MainActivity and a CustomListViewActivity. In my MainActivity, I have 1 button and 1 spinner. On click of the button, I pass the selected spinner value to the CustomListViewActivity via Bundle and using Intents.
Now, in my CustomListViewActivity, I have a ListView that uses ArrayAdapter for populating it. I send a ArrayList from MainActivity, say for example
items = [abc]
In my CustomListViewActivity, I receive the same and use it to populate my ListView. The first time I do this, my value gets populated. The second time I do the same, the value existing is now replaced with the new one and the ListView shows one item instead of showing two.
So basically the problem is that the ListView is not updating and not showing both the items. Instead it shows me a single item always.
Here are snippets of my code
MainActivity.java
//code inside button click
..
{
items.add(spinner1.getSelectedItem().toString());
Bundle bundle =new Bundle();
bundle.putStringArrayList("data",items);
Intent i = new Intent(MainActivity.this,MyListViewActivity.class);
i.putExtras(bundle);
startActivity(i);
finish();
}
protected void onSaveInstanceState(Bundle icicle) {
super.onSaveInstanceState(icicle);
Log.i("App","onSave");
icicle.putStringArrayList("data",items);
}
#Override
protected void onRestoreInstanceState(Bundle icicle2) {
// TODO Auto-generated method stub
super.onRestoreInstanceState(icicle2);
Log.i("App","onRestore");
icicle2.putStringArrayList("data",items);
}
MyListViewActivity.java
private ArrayList<String> myItems;
private static String[] titles;
CustomListViewAdapter adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_main);
listView = (ListView) findViewById(R.id.list1);
rowItems = new ArrayList<RowItem>();
adapter = new CustomListViewAdapter(this,R.layout.list_item, rowItems);
listView.setAdapter(adapter);
b=getIntent().getExtras();
if(b!=null)
{
myItems=b.getStringArrayList("data");
titles= new String[myItems.size()];
titles = myItems.toArray(myItems);
}
...
int i=0;
while(i<titles.length) {
item = new RowItem(titles[i]);
//rowItems.add(item);
Log.i("ROW", ""+rowItems.size());
i++;
adapter.add(item);
listView.setAdapter(adapter);
}
adapter.setNotifyOnChange(true);
adapter.notifyDataSetChanged();
}
}
How to make the ListView maintain the current data as well reflect the added data?
Thanks in advance
EDIT : Forgot to mention one more thing. In my MyListViewActivity class I have a button above the ListView that on clicked takes me to my MainActivity so that I can add a new Item again. So when I go back to MainActivity and try and add a new Item , it's the new Item that get displayed rather than showing both the previous and the new one
You need to persist and restore your items list in MainActivity's onSaveInstanceState and onRestoreInstanceState methods.
You also probably don't need to close the MainActivity by calling finish() every time you start the ListView activity but that depends on your application.
Basically, the problem you are having is that items is being recreated with each new instance of MainActivity. Since you finish MainActivity, a new instance is used every time you access that activity (and I assume you thought that items would just keep getting items added to it).

Can't handle Intent properly in 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.

Update ListView from user input after setContentView call

I want to show an empty list view, which is then populated by user input. I have the UI flow working, and I populate a list of my custom objects after the user enters some information via a view which is invoked through setContentView (i.e. no a new Activity).
I take the input and add it to a list, which I want to be summarised on the ListView. However, whenever I add to the list and/or the ArrayAdapter and call adapter.notifyDataSetChanged() it does not do what I want. The ListView is still empty. Argh! It's driving me insane!
#Override
public void onCreate(Bundle blah) {
ListView listView = (ListView) findViewById(R.id.results_list);
listView.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, list));
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.mnu_add:
final Activity act = this;
setContentView(R.layout.record_details);// the sub-view that takes the user input
// the button on the form to 'add' details:-
((Button) findViewById(R.id.recored_details_add_btn))
.setOnClickListener(
new OnClickListener() {
public void onClick(View v) {
// get input from widgets
list.add(someObject);
((ArrayAdapter<Object>) listView.getAdapter()).notifyDataSetChanged();
setContentView(R.layout.list_view);
}
}
);
((ArrayAdapter<Object>) listView.getAdapter()).notifyDataSetChanged();
break;
}
return true;
}
Please, save me from my misery and inform me of my stupidty?
Thanks in advance.
public void onClick(View v) {
// get input from widgets
list.add(someObject);
((ArrayAdapter<Object>) listView.getAdapter()).notifyDataSetChanged();
setContentView(R.layout.list_view);
Is it possible that this setContentView in the onClick handler is creating a new instance of the list view widget (with no adapter) or reinitializing the list view (clearing the adapter)?
Try putting something in the list initially in onCreate and then see if it disappears when you hit the button.
I haven't seen any code (although I'm a relative newbie) that switches views within the activity's lifetime to bring up essentially bring up different pages - most use a separate activity.
Edit:
OP asks:
Thanks...So how can I get what I want? The list I'm backing the adapter with is static; should I just use activities instead and rely on onCreate loading from the static field?
Some options:
Use separate activities
Re-associate the adapter (call setAdapter again) - probably a bad idea
Declare both layouts in the same file. You'll hide one and unhide the other to switch between views rather can calling setContentView. This is similar to how ListView layout works (one for when the list is empty and one for when it is not). I think I've seen an example of this somewhere on the net, but I don't have a reference right now.
You could relaunch the same activity by using Intent.FLAG_ACTIVITY_SINGLE_TOP flag while creating the intent and override the onNewIntent() method.
Inside the onNewIntent() you create the adapter with updated data and call setAdapter.
I think this will give you the intended behaviour.

Categories

Resources