I'm trying to add details of movies to Recyclerview on Button Click. I have 2 Activities. I can add the details of movies in the first activity and I can view the list of movies in the Second Activity. The first activity contains a submit button to add movies.
But the issue I'm facing is that I can add movies to the first row only. If i try to add another movie it replaces the first row. I want to create a list of movies on button click. How do I do this? The below code is the Main Activity (First Activity).
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setActionBarColour();
clickEvents();
}
private void clickEvents()
{
final EditText editTextMovieName = findViewById(R.id.movieName);
final EditText editTextGenre = findViewById(R.id.genre);
final EditText editTextReleaseYear = findViewById(R.id.year);
Button submit = findViewById(R.id.btnSubmit);
Button movieList = findViewById(R.id.btnMovieList);
final Bundle bundle = new Bundle();
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String name = editTextMovieName.getText().toString();
String genre = editTextGenre.getText().toString();
String year = editTextReleaseYear.getText().toString();
bundle.putString("MOVIE_NAME", name);
bundle.putString("GENRE", genre);
bundle.putString("YEAR", year);
}
});
movieList.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, MovieListActivity.class);
intent.putExtras(bundle);
startActivity(intent);
}
});
}
private void setActionBarColour()
{
ActionBar actionBar;
actionBar = getSupportActionBar();
ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#3498DB"));
assert actionBar != null;
actionBar.setBackgroundDrawable(colorDrawable);
}
}
This below code is only a function in the Second Activity to get the data
private void prepareMovieData()
{
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
String movieName = bundle.getString("MOVIE_NAME");
String genre = bundle.getString("GENRE");
String year = bundle.getString("YEAR");
Movie movie = new Movie(movieName,genre,year);
movieList.add(movie);
}
If I edit the above code to
Movie movie = new Movie(movieName,genre,year);
movieList.add(movie);
Movie movie2 = new Movie(movieName,genre,year);
movieList.add(movie2);
I can add only 2 movies. If i want to put 3 movies then I would have to put a third statement like this
Movie movie3 = new Movie(movieName,genre,year);
movieList.add(movie3);
The more movies I want the more lines of code I have to write. So how do I put a large number of movies on click?
As per your question detailing we can conclude that
1) On Submit move to next Activity with movie details and add in adapter.
2) Press back
3) Repeat step 1 with new details.
So here revisiting next screen will not have previous data. For that you have to save data in local storage and display.
As i review your code when you call the second activity its clear your model data and make its easy use parcable data into model class which help you to pass the model class from one activity to another use onActivityresult for this and add this you model into list to add multiple data through intent. or you can save your data into local sql-lite and fetch data into next activity
Related
ok so I'm making an app where I use an edit text to write anything and when I press a button it adds whatever I wrote in the edit to a listView, and it works, but I want the List to be in a different activity than the button and edit text, so I moved it without changing the code.can anyone figure this out,
BTW all the variables are public.
public class MainActivity extends AppCompatActivity {
public ArrayList<String> arrayList2;
public ArrayAdapter<String> adapter,adapter2;
public EditText editText,editText2;
public ArrayList<String> itemList,itemList2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] items = {};
itemList = new ArrayList<String>(Arrays.asList(items));
adapter = new ArrayAdapter<String>(this, R.layout.activity_list_layout,R.id.txtItem,itemList);
ListView listV = (ListView)findViewById(R.id.list_layout);
listV.setAdapter(adapter);
editText = (EditText)findViewById(R.id.thingadd);
Button btAdd = (Button)findViewById(R.id.add);
String[] age = {};
arrayList2 = new ArrayList<String>(Arrays.asList(age));
itemList2 = new ArrayList<String>(Arrays.asList(age));
adapter2 = new ArrayAdapter<String>(this, R.layout.activity_list__layout2,R.id.txtage,itemList2);
ListView listV2 = (ListView)findViewById(R.id.Age);
listV2.setAdapter(adapter2);
editText2 = (EditText)findViewById(R.id.agetxt);
btAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String newItem=editText.getText().toString();
String newItem2=editText2.getText().toString();
itemList.add(newItem);
itemList2.add(newItem2);
adapter.notifyDataSetChanged();
adapter2.notifyDataSetChanged();
}
});
}
}
You can use SQLite Database for that, where you would be saving data in database from one activity and reading it and displaying it in listview from another.
You have 2 options to do so.
You can use any database service. Store the values and display them in the next activity. But this needs Internet service to be enabled in the phone.
You can use intent. Convert those input to string type and pass them to next activity and display them using ids. This type do not require Internet service. To know more about this, Visit this link
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
public class ProductList extends Activity
{
String m_brandName, m_modelName, m_categoryName;
ListView mListView;
private static ProductList actAppChartCatList;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
m_brandName = getIntent().getStringExtra("Brand_Name");
m_modelName = getIntent().getStringExtra("Model_Name");
m_categoryName = getIntent().getStringExtra("Category_Name");
actAppChartCatList = this;
setContentView(R.layout.activity_product_list);
mListView = (ListView) findViewById(R.id.showProductLists);
}
public static ProductList getInstance()
{
return actAppChartCatList;
}
}
There is one Activity in my app named "ApplicationChart" consist of 3 spinners. One for Vehicle Brand, Second for Vehicle Model, and third for Category like Horns, lights etc. I have populated all the records in respective spinners. On Click of Submit Button from first activity, I just want to display 'Product List According to Category' on next Activity named "Product List". And all these I want to achieve using web service. Please guide me.
OnClick Of Submit Button please read all value from spinner.
Suppose you have 3 spinners namely
spinner1,spinner2,spinner3
Then on OnClick of submit get value of three spinner
String text1 = spinner1.getSelectedItem().toString();
String text2 = spinner2.getSelectedItem().toString();
String text3 = spinner3.getSelectedItem().toString();
Pass it to next activity through intent bundle
Intent nextActivity=new Intent(this,NextActivity.class);
nextActivity.putExtra("key1",text1);
and so on.
Then in NextActivity read all these value and do your actions
I have a Listview activity and a Mainactivity.
In my Listview activity i have a array that is
public static final String[] link = {
"http://3313.live.streamtheworld.com/WKRKFM_SC",
"http://3573.live.streamtheworld.com/KXXRFMAAC",
"http://2713.live.streamtheworld.com/KXXRFMAAC",
"http://r16---sn-npo7enel.gvt1.com/videoplayback" };
Im using putExtra and passing this array to my Mainactivity using this.
Intent intent =new Intent(MainActivity.this, Linkview.class);
intent.putExtra("link", link[position]);
startActivity(intent);
And i received my extra using this in Mainactivity.
final Bundle bundle = getIntent().getExtras();
String link = bundle.getString("link");
and i can use it very well but in my MainActivity
I have a next button
next.setOnClickListener(new View.OnClickListener() {
#SuppressWarnings("unused")
#Override
public void onClick(View v) {
//??????
}
});
I want that when i pressed the next button the next array comes from listview activity
and i can show this in my MainActivity.
How i can do that??
So from your question, I assume you want to show the next item in the array.
As long as you keep your array as a static final variable (i.e. a constant array), you can directly access it from your main class. You don't need an instance of the class to access a static variable. For instance, the following should work from your main Activity:
String item = ListviewActivity.link[position];
Thus, you can simply pass the position as an integer, look up the string with the above code, and add one to the position to get the next item.
I have an android app where a listview displays a few items - 'films', and when you click on an item it gives you the option to delete or update the item. The delete works fine, and when you click update it opens up a new fragment with the details of the film correctly populated in edit text boxes. I'm trying to figure out how to get the update button on this new Fragment to work.
Firstly I have it extending FragmentActivity, so is this an Activity, or a Fragment? Should I be using something else? When I go to add my button on click listener like I did in the previous 'ListFragment' it won't let me call 'getActivity()' or to reference the currently selected item in the list 'getListAdapter()'. What should I be calling here/how do I reference the current Fragment/Activity?
Should I have these outside of the onCreate in a separate method? I know there is a lot of questions here, but I am really trying to understand what exactly is going on. Any help would be really appreciated. Here is what my code looks like for the FragmentActivity
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.film_activity);
//adds listeners to buttons
updateButton = (Button) findViewById(R.id.update_button);
filmTitleView = (EditText) findViewById(R.id.textView_title);
filmDirectorView = (EditText) findViewById(R.id.textView_director);
filmYearView = (EditText) findViewById(R.id.textView_year);
//retrieves object from intent sent through from previous activity
//assigns variables
Intent intObj = getIntent();
filmTitle = (String) intObj
.getSerializableExtra("title");
filmDirector = (String) intObj
.getSerializableExtra("director");
filmYear = (String) intObj
.getSerializableExtra("year");
filmID = (Integer) intObj
.getSerializableExtra("id");
//Greeting message text set
filmTitleView.setText(filmTitle);
filmDirectorView.setText(filmDirector);
filmYearView.setText(filmYear);
System.getProperty("line.separator");
final Film f1 = (Film)(getListAdapter()).getItem(filmID);
updateButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(getActivity(),MainActivity.class);
Context context = getActivity();
FilmStoreOpenHelper helper = new FilmStoreOpenHelper(context);
SQLiteDatabase db = helper.getWritableDatabase();
FilmDAO dao = SQLiteFilmDAO.getInstance(db);
FilmStore fs1 = FilmStore.getInstance(context);
fs1.addFilm(f1);
boolean updated = dao.update(f1);
if (deleted){
Toast.makeText(getActivity(), ("Film " + f1.getTitle() + " Updated"), Toast.LENGTH_LONG).show();
db.close();
startActivity(i);
}else{
Toast.makeText(getActivity(), "Not Updated, try again", Toast.LENGTH_LONG).show();
}
}
});
}
Replace any calls to getActivity() with this. A FragmentActivity is a special Activity from the support package that is designed to be used with Fragments. If you're developing for higher than API 11, you can use a regular Activity.