I have RecyclerView in fragment and i want when user press button below "Add New Row" it will redirect to new fragment till here there is no problem i can achieve it but now i want the details filled in form will show as new row in recyclerview
As you can see in image above user can add row in RecyclerView dynamically and also there is cross top right of every row which delete row from list.
So,What i want to do is
1) How i can add row to the list with form inputs as soon as user press Save Event then show user newly added row with his entered details in form
In short i want to add row dynamically from list with user inputs in form and also delete it from list when user press cross button at top right of row item
I solved this issue by myself what i did to get data back into first fragment from second fragment
First i create interface to send result to fragment_1(Which has recyclerview) from fragment_2(Which has form)
Here is my form code
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.ppl_details_form,container,false);
PPL_Title_Spinner= (Spinner) view.findViewById(R.id.PPL_Spinner);
Title= (EditText) view.findViewById(R.id.ppl_title);
Address= (AutoCompleteTextView) view.findViewById(R.id.ppl_address);
Year= (EditText) view.findViewById(R.id.ppl_passing_year);
Description= (EditText) view.findViewById(R.id.ppl_description);
btn1= (Button) view.findViewById(R.id.ppl_map_button);
Save= (Button) view.findViewById(R.id.ppl_save);
Save_PPL.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
title=Title.getText().toString();
address= Address.getText().toString();
year=Year.getText().toString();
description=Description.getText().toString();
//PPL Wrapper
PPL_list_wrapper list=new PPL_list_wrapper(title,year,address);
Add_PPL=(PPL_transfer_data_to_list)getActivity();
Add_PPL.addLocation(list);
fragment_1 fragment1=new fragment_1();
FragmentTransaction fragmentTransaction=getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.Navigation_Main_Layout,fragment1);
fragmentTransaction.commit();
}
});
#Override
public void onAttach(Context context) {
super.onAttach(context);
try{
Add_PPL= (PPL_transfer_data_to_list) context;
}
catch (ClassCastException e){
throw new ClassCastException(getActivity().toString()+" Must Implement Interface");
}
}
And then i implement infterface into Parent Activity and then call fragment_1 method from activity like this
#Override
public void addEvent(PPL_list_wrapper PPL_DATA) {
fragment_1 recycler=new fragment_1();
recycler.PPL_eve(PPL_DATA);
}
Then i get data into fragment_1 and have to make arraylist in fragment static cause without static list will lost its reference and data too and it will stop showing data into recyclerview
public void PPL_eve(final PPL_list_wrapper ppl_list_wrapper){
PPL_wrapper=ppl_list_wrapper;
event_details.add(ppl_list_wrapper);//Static ArrayList
adapter=new ppl_Recycler_Adapter(getActivity(),event_details);
adapter.notifyDataSetChanged();
}
I have to make sure that my list is static
static List<PPL_list_wrapper> event_details=new ArrayList<PPL_list_wrapper>();
Related
I have RecyclerView having an ImageView and a TextView for each row layout.In ViewHolder in RecyclerViewAdapter, I have click listener as
v.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Images i=list.get(getAdapterPosition());
i.setFlag(!i.getFlag());
list.set(getAdapterPosition(),(i));
notifyDataSetChanged();
}
});
In that click listener I am changing Boolean flag such that it can show either an item is selected or not.Depending on it's value I want to Traverse entire ArrayList by checking
for(int i=0; i<images.size(); i++){
if(images.get(i).getFlag()){
// there I want to get view from LayoutManager for that item
//but how to get view for that item and get Bitmap form imageview for that item
//end of geting view
}
}
Please help to get view at specific adapter position from Recyclerview Image to reflect my problem is as follows
Try this use recyclerView.getLayoutManager().findViewByPosition(int position) to get particular row of recyclerView
View row = recyclerView.getLayoutManager().findViewByPosition(0);
TextView textView = row.findViewById(R.id.YourTextviwID);
textView.setText("Nilu");
ImageView textView = row.findViewById(R.id.YourImageViewID);
imageView.setBackgroundResource(R.mipmap.ic_launcher);
NOTE :- only item that is display in screen when you scroll recyclerView than item will removed from memory
EDIT :- you can write a public method in adapter to get your arraylist in your activity where you can check your flag
I am trying to retrieve data from firebase DB and save data to a string inside my program. All I come across is that I can only view data in a list but find no good guide how to get data without showing all data for the user first. I want it to be run in the background without the user having to see when data is retrieved. In the image below I show what data I want to store inside a string and then use that string to do what I want it to do.
This is the data I want to store inside my string
You can see the blue rings. The data inside the blue rings is what I want to store inside a string or in a string array.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view);
firebaseAuth = firebaseAuth.getInstance();
listan = (ListView) findViewById(R.id.listView);
DatabaseReference datarefere = FirebaseDatabase.getInstance().getReferenceFromUrl(My URL to database");
FirebaseListAdapter<String> firebaselist = new FirebaseListAdapter<String>(
this,
String.class,
android.R.layout.simple_expandable_list_item_1,
datarefere
) {
#Override
protected void populateView(View v, String model, int position) {
TextView textview = (TextView) v.findViewById(android.R.id.text1);
textview.setText(model);
}
};
listan.setAdapter(firebaselist);
listan.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView listan = (TextView) view;
String plats = listan.getText().toString();
}
});
toolbar = (Toolbar) findViewById(R.id.tool_bar); // Attaching the layout to the toolbar object
setSupportActionBar(toolbar);
}
In this case i did use listview. The user click on an item inside the list and I save the data to a string. But i dont want to do like that all the time. I want to save data into a string in the background. The user dont need to know or see that it happens.
Maybe something that can help you is this google developer link about how to setup Firebase and other DBs. I hope this helps.
Trudging my way through my introduction to Java and Android on a simple app and have run into an issue with ListView item selection. For one of my activities, I have a layout with two buttons, one of which is a "Delete" button, and a ListView of "passages" which are essentially timestamps for when a device has passed a sensor.
I have implemented the ability to click on an item to select it, which then enables the "Delete" button. A click of the "Delete" button removes the "passage" but I still end up with a selected item, which I don't want.
To implement selection, I added the following property to the ListView:
android:id="#+id/passagesListView"
android:choiceMode="singleChoice"
android:listSelector="#666666"
Selection is supported in OnCreate via a an OnItemClickListener:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_passages);
passagesViewAdapter = new PassagesViewAdapter(this, R.layout.passages_row_layout, passages);
final ListView passagesListView = (ListView) findViewById(R.id.passagesListView);
assert passagesListView != null;
final Button deleteButton = (Button) findViewById(R.id.deleteButton);
deleteButton.setEnabled(false);
buildPassageList();
passagesListView.setAdapter(passagesViewAdapter);
passagesListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View view, int position,long arg3) {
Toast.makeText(ViewPassagesActivity.this, "position is " + position,
Toast.LENGTH_LONG).show();
view.setSelected(true);
passagesViewAdapter.notifyDataSetChanged();
selectedItemPos = position;
deleteButton.setEnabled(true);
}
});
}
This part works. However, there is some issue with deletion. As you can see in the comments, I have tried several methods that I found on StackOverflow that seemed to apply but, although I am able to delete the correct item from the list, I am still ending up with a selected item after the call to delete().
public void delete (View view)
{
final Button deleteButton = (Button) findViewById(R.id.deleteButton);
ListView passagesListView = (ListView) findViewById(R.id.passagesListView);
if(selectedItemPos != -1)
{
Toast.makeText(ViewPassagesActivity.this, "remove " + selectedItemPos,
Toast.LENGTH_LONG).show();
// This did not work, which is strange since it worked similarly for selection when clicked
// View itemView = passagesListView.getChildAt(selectedItemPos);
View itemView = passagesViewAdapter.getView(selectedItemPos, null, passagesListView);
itemView.setSelected(false);
// This was also recommended in various posts on StackOverflow.
// Not clear whether clearChoices applies only to checkBoxes?
// passagesListView.clearChoices();
// passagesListView.requestLayout();
passages.remove(selectedItemPos);
deleteButton.setEnabled(false);
selectedItemPos = -1;
passagesViewAdapter.notifyDataSetChanged();
}}
}
I also ran into some issues trying to track which item is selected via setSelected() and getSelectedItemPosition() and punted by just tracking the index myself. So, as I am new to this, I'm sure there is something I am not understanding about Views or maybe something else such as a misunderstanding of how selection works?
How can I clear the selection?
Thanks!
I don't know what your PassagesViewAdapter class looks like. Maybe you can try
passagesViewAdapter.remove(passages.get(selectedItemPos));
passages.remove(selectedItemPos);
deleteButton.setEnabled(false);
selectedItemPos = -1;
passagesViewAdapter.notifyDataSetChanged();
I targeted the items in an action bar using ShowcaseView, but I can't target elements of a ListView! I tried that and it didn't work:
ShowcaseView sv = new ShowcaseView.Builder(this,true)
.setTarget(new ViewTarget(lv.getChildAt(1).findViewById(R.id.heart)))
// Here is where you supply the id of the action bar item you
// want to display
.setContentTitle("Heart")
.setContentText("Check the venues of your city")
.hideOnTouchOutside()
.build();
Very simple. Take the element in your ViewHolder then place the code for showing the ShowCaseView inside your bindView.
Don't forget to put different a different ID for each element in the list.
So you may try something like this.
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView button;
public ViewHolder(final View itemView) {
super(itemView);
button = (Button) itemView.findViewById(R.id.list_button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new MaterialShowcaseView.Builder(getActivity())
.setTarget(verifyButton)
.setDismissText("GOT IT")
.setContentText("Your text goes here")
.setDelay(100) // optional but starting animations immediately in onCreate can make them choppy
.singleUse(id + " define an unique id for each item of the list") // provide a unique ID used to ensure it is only shown once
.show();
}
});
}
}
}
To get more information you can check the library here.
I have a listview. In it, each row had a text saying 0:00. But now, I added a button on my actionbar but then I got stuck. I don't know how to make the button create a new row, displaying 0:00
This is my code for the data in a row.
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
RowData rowdata_data[] = new RowData[]
{
new RowData("0:00")
};
RowdataAdapter adapter = new RowdataAdapter(this,
R.layout.listview_item_row, rowdata_data);
listView1.setAdapter(adapter);
listView1 = (ListView)findViewById(R.id.listView1);
}
this is my RowData class:
public class RowData {
String title;
public RowData(String title) {
this.title = title;
}
}
So how should I implement a button click to add another row?
Under addtionbutton: should be the method.
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle presses on the action bar items
switch (item.getItemId())
{
case R.id.additionbutton:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I don't know how to make the button create a new row
With your current solution it's not possible (in efficient way do it) because you're using static array of objects - it means array has fixed size (it means that you're assuming that size won't be changed at runtime).
But your goal is "slightly" different. You want after each click on Button increase number of objects by one. It means you don't know exactly how many rows you can have (or can be changed at runtime).
Due to things mentioned above you should (have to) use dynamic array with changeable size. For this reason you need to use as source of data List that is representation of dynamic array.
Basic algorithm:
Create new List with zero (or you can have by default one row at
start).
Then create public method in your adapter that will add new item to
collection and send request that Adapter should be refreshed (after you added new row into collection).
Assign OnClickListener to your Button and in onClick() method you'll use created method for adding new row into ListView.
Solution:
How to initialise ListAdapter:
// Activity-level variable scope
private List<RowData> items = new ArrayList<RowData>();
private RowdataAdapter adapter;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView1 = (ListView)findViewById(R.id.listView1);
// adding first item to List, it's optional step
items.add(new RowData("0:00"));
adapter = new RowdataAdapter(this, R.layout.listview_item_row, items);
listView1.setAdapter(adapter);
}
Method for adding new row into ListAdapter:
public void addRow(RowData newRow) {
// items represents List<RowData> in your Adapter class
this.items.add(newRow);
// sends request to update ListAdapter
notifyDataSetChanged();
}
How to update Adapter after click on Button:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// add new row to Adapter
adapter.addRow(new RowData("0:00"));
}
});
Hope i helped you to solve your problem.
you need to create a dataset that you can change so you need to make your array a class wide variable so you can add to it when you need to
when you add the new row you need to notify the adapter that something changed so you do this
adapter.notifyDatasetChanged();
and that should update your list