How can one fetch user input from EditText from list of fragmens?
Clash Royale CLAN TAG#URR8PPP
How can one fetch user input from EditText from list of fragmens?
My code is structured such that the "drawer activity" calls upon one of several fragment drawers that each contains a list of fragment items that utilize ItemAdapter
to create a fragment list.
ItemAdapter
My main problem is that i have been trying to retrieve EditText
with afterTextChanged()
in item adapter but always get a NullPointerException
and I want to be able to retrive the value that the user has just put in.
EditText
afterTextChanged()
NullPointerException
Any and all suggestions in a civil manner are much appreciated.
Drawer activity
public class Drawers extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener
public static boolean isSealBroken;
public static int DrawerChosen;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
System.out.println(savedInstanceState);
setContentView(R.layout.activity_drawers);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.screen_area, new drawer_top_Fragment());
ft.commit();
@Override
public void onBackPressed()
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START))
drawer.closeDrawer(GravityCompat.START);
else
super.onBackPressed();
@Override
public boolean onCreateOptionsMenu(Menu menu)
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.drawers, menu);
return true;
@Override
public boolean onOptionsItemSelected(MenuItem item)
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings)
return true;
return super.onOptionsItemSelected(item);
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item)
// Handle navigation view item clicks here.
Fragment fragment = null;
int id = item.getItemId();
//Find ID for buttons in Drawers Navigation bar and connect it to the correct fragment
if (id == R.id.nav_drawer_top)
fragment = new drawer_top_Fragment();
else if (id == R.id.nav_drawer_med)
if (isSealBroken == false)
isSealBroken_Message();
if (isSealBroken == true)
fragment = new drawer_med_Fragment();
DrawerChosen=1;
else if(id == R.id.nav_drawer1)
if(isSealBroken==false)
isSealBroken_Message();
if(isSealBroken==true)
fragment = new drawer1_Fragment();
DrawerChosen=2;
else
fragment = new drawer1_Fragment();
DrawerChosen=2;
else if(id == R.id.nav_drawer2)
if(isSealBroken==false)
isSealBroken_Message();
if(isSealBroken==true)
fragment = new drawer2_Fragment();
DrawerChosen=3;
else
fragment = new drawer2_Fragment();
DrawerChosen=3;
// more of the same until nav 8
else if(id == R.id.nav_drawer8)
if(isSealBroken==false)
isSealBroken_Message();
if(isSealBroken==true)
fragment = new drawer8_Fragment();
DrawerChosen=9;
else
fragment = new drawer8_Fragment();
DrawerChosen=9;
else if (id == R.id.drawer_continue)
final Intent intent = new Intent(this, WagonSummary.class);
new AlertDialog.Builder(this)
.setTitle("Er Skráningu hluta á vagni lokið?")
.setNegativeButton("Nei",null)
.setPositiveButton("já",new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
// FinishComputeList(); // calls upon class to compute value from list
startActivity(intent);
).create().show();
else if (id == R.id.drawer_comment)
Intent intent = new Intent(this, CommentSummary.class);
startActivity(intent);
if(fragment != null)
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.screen_area, fragment);
ft.commit();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
public void isSealBroken_Message()
new AlertDialog.Builder(this)
.setTitle("Er innsigli rofið?")
.setNegativeButton("Nei",null)
.setPositiveButton("já",new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
isSealBroken=true;
ListProcessing d = new ListProcessing();
d.sealBroken();
).create().show();
One of several drawer fragments as an example
public class drawer3_Fragment extends Fragment
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
View rootView = inflater.inflate(R.layout.word_list, container, false);
getActivity().setTitle(R.string.drawer_three);
ArrayList<Item> items = new ArrayList<Item>();
String itemArr = getResources().getStringArray(R.array.drawer_three_items);
int quantityArr = getResources().getIntArray(R.array.drawer_three_quantity);
String typeArr = getResources().getStringArray(R.array.drawer_three_type);
String item;
int quantity;
String type;
for (int i = 0; i < itemArr.length; i++)
item = itemArr[i];
quantity = quantityArr[i];
type = typeArr[i];
items.add(new Item(item, quantity, type));
ItemAdapter adapter = new ItemAdapter(getActivity(), items);
ListView listView = (ListView) rootView.findViewById(R.id.list);
listView.setAdapter(adapter);
return rootView;
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState)
super.onViewCreated(view, savedInstanceState);
Item adapter
public class ItemAdapter extends ArrayAdapter<Item> {
public ItemAdapter(Activity context, ArrayList<Item> items)
super(context, 0, items);
@Override
public View getView(final int position, final View convertView, final ViewGroup parent)
// Check if the existing view is being reused, otherwise inflate the view
View listItemView = convertView;
if(listItemView == null)
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
// Get the @link Item object located at this position in the list
final Item currentItem = getItem(position);
final Item currentposs = getItem(position);
// Find the TextView in the list_item.xml layout with the ID version_name
TextView itemTextView = (TextView) listItemView.findViewById(R.id.item);
// Get the version name from the current object and
// set this text on the name TextView
itemTextView.setText(currentItem.getItem());
TextView infoTextView = (TextView) listItemView.findViewById(R.id.info);
infoTextView.setText(currentItem.getInfo());
TextView doseTextView = (TextView) listItemView.findViewById(R.id.dose);
doseTextView.setText(currentItem.getDose());
TextView quantityTextView = (TextView) listItemView.findViewById(R.id.quantity);
quantityTextView.setText(currentItem.getQuantity());
TextView typeTextView = (TextView) listItemView.findViewById(R.id.type);
typeTextView.setText(currentItem.getType());
CheckBox check = listItemView.findViewById(R.id.checkbox);
//reset and Populating list
final String itemName= currentposs.getItem();
String itemInfo = currentposs.getInfo();
String itemDose = currentposs.getDose();
String itemQuantity = currentposs.getQuantity();
String itemType = currentposs.getType();
// populateList(position,itemName,itemInfo,itemDose,itemQuantity,itemType);
final View listItemView1 = listItemView;
// when chekbox is ticked then list is amended
check.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
CheckBox checkBox = (CheckBox) v;
if (checkBox.isChecked())
// to see if correct location is beaing chosen each time
System.out.println("currentposs : "+currentposs);
System.out.println("position : "+position);
System.out.println("Parent : "+parent);
String itemName= currentposs.getItem();
System.out.println("getItem() : "+itemName);
String itemInfo = currentposs.getInfo();
System.out.println("getInfo() : "+ itemInfo);
String itemDose = currentposs.getDose();
System.out.println("getDose() : "+ itemDose);
String itemType = currentposs.getType();
System.out.println("getType() : "+ itemType);
String itemQuantity = currentposs.getQuantity();
System.out.println("getQuantity() : "+ itemQuantity);
int wagonNr = 0;
String status = "false";
ListProcessing d = new ListProcessing();
d.itemStatusChange( wagonNr,Drawers.DrawerChosen,position,status);
);
//TextView.setContentView(R.layout.list_item);
//String yourEditText = (EditText) currentposs.findViewById(R.id.quantity);
final View finalListItemView = listItemView;
quantityTextView.addTextChangedListener(new TextWatcher()
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
//nothing has worked
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
//nothing has worked
@Override
public void afterTextChanged(Editable s)
//nothing has worked but here are some exaples of what i have tried
//TextView quantityTextViewEdited = (TextView) convertView.findViewById(R.id.quantity);
// String n = (String) currentItem.getText();
// View listItemView2 = convertView;
// EditText name=(EditText)listItemView2.findViewById(R.id.quantity);
// String a=name.getText().toString().trim();
//
View listItemView2 = listItemView1;
TextView quantityTextView2 = (TextView) listItemView2.findViewById(R.id.quantity);
int numberInserted = Log.e("input text", quantityTextView2.getText().toString());
System.out.println("Changed qunatity: "+ numberInserted);
);
if (currentItem.isCheckBox())
check.setVisibility(View.VISIBLE);
else
check.setVisibility(View.GONE);
if (currentItem.isInfo())
infoTextView.setVisibility(View.VISIBLE);
else
infoTextView.setVisibility(View.GONE);
if (currentItem.isDose())
doseTextView.setVisibility(View.VISIBLE);
else
doseTextView.setVisibility(View.GONE);
if (currentItem.isQuantity())
quantityTextView.setVisibility(View.VISIBLE);
else
quantityTextView.setVisibility(View.GONE);
return listItemView;
Item
public class Item {
private String item;
private String info;
private String dose;
private String type;
private int quantity;
private boolean isInfo = false;
private boolean isDose = false;
private boolean isQuantity = false;
private boolean isCheckBox = false;
/**
* @param wItem
*/
public Item(String wItem)
item = wItem;
isCheckBox = true;
/**
* @param wItem is the name of the item/medicine
* @param wQuantity is how many items there should be total
* @param wType ...
*/
public Item(String wItem, int wQuantity, String wType)
item = wItem;
quantity = wQuantity;
type = wType;
isCheckBox = true;
isQuantity = true;
public Item(String wItem, String wInfo, String wDose, int wQuantity, String wType)
item = wItem;
info = wInfo;
dose = wDose;
quantity = wQuantity;
type = wType;
isCheckBox = true;
isQuantity = true;
isDose = true;
isInfo = true;
public String getItem()
return item;
public String getInfo()
return info;
public String getDose()
return dose;
public String getType()
return type;
public String getQuantity()
return Integer.toString(quantity);
public boolean isCheckBox()
return isCheckBox;
public boolean isInfo()
return isInfo;
public boolean isDose()
return isDose;
public boolean isQuantity()
return isQuantity;
public void findViewById (int quantity)
Update
After entering Log.e("input text", quantityTextView2.getText().toString()); inside "afterTextChanged" when user inputs a new number then in the terminal it prints the correct user input. When trying to encase this within a int variable it only ever returns the number 14, no matter the user input.
example from the run terminal
D/InputMethodManager: SSI - flag : 0 Pid : 16988 view : com.example.notandi.hospitalwagons
D/ViewRootImpl@be26588[PopupWindow:b7449a5]: Relayout returned: old=[622,841][712,949] new=[655,841][745,949] result=0x1 surface=valid=true 493809848320 changed=false
D/ViewRootImpl@3142bca[Drawers]: ViewPostIme key 0
I/System.out: !!!!!!!!!!!!!!!!!!!!!!afterTextChanged!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
I/System.out: Changed qunatity: 13
I/System.out: !!!!!!!!!!!!!!!!!!!!!!afterTextChanged!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
I/System.out: Changed qunatity: 13
D/ViewRootImpl@3142bca[Drawers]: ViewPostIme key 1
D/OpenGLRenderer: eglDestroySurface = 0x73093dc320
D/ViewRootImpl@be26588[PopupWindow:b7449a5]: dispatchDetachedFromWindow
D/InputEventReceiver: channel '9bf6f1 PopupWindow:b7449a5 (client)' ~ Disposing input event receiver.
D/InputEventReceiver: channel '9bf6f1 PopupWindow:b7449a5 (client)' ~NativeInputEventReceiver.
I/System.out: !!!!!!!!!!!!!!!!!!!!!!afterTextChanged!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
E/input text: 2
I/System.out: Changed qunatity: 14
I/System.out: !!!!!!!!!!!!!!!!!!!!!!afterTextChanged!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
E/input text: 2
I/System.out: Changed qunatity: 14
Update 2
It LIIIIVES!!
Because of helpful reccomendations from user Nirav Bhavsar i explored the console log approch and inserted into "onTextChanged" the following code
String numberInserted ;
final View listItemView2 = listItemView1;
final TextView quantityTextView2 = (TextView) listItemView2.findViewById(R.id.quantity);
String userinput = quantityTextView2.getText().toString();
System.out.println("user input: "+userinput);
It works and i am able to retrive the EditText value that the user inserted. Many thanks to the stack overflow community.
afterTextChanged
Wooooo!... It prints the number that the user inserted, but when i try to put it into an int variable it only returns the number 14 every time, irrelevant to the actual value inserted.
– Olaf
Aug 8 at 17:37
can you update the post now, after try above and let me know what you enter like eg and let me know so that i can check on my side
– Nirav Bhavsar
Aug 8 at 17:42
and also try to show the value to user which is inserted inside
afterTextChanged
– Nirav Bhavsar
Aug 8 at 17:45
afterTextChanged
it has been updated to reflect latest developments.
– Olaf
Aug 9 at 22:03
1 Answer
1
I want to be able to retrieve the value that the user has just put in
Here you go,
final View finalListItemView = listItemView;
quantityTextView.addTextChangedListener(new TextWatcher()
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
//nothing has worked
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
//here "s" is the latest text
Toast.makeText(getApplicationContext(),s,Toast.LENGTH_SHORT).show();
@Override
public void afterTextChanged(Editable s)
//don't do anything here, instead
//get the lastest text in onTextChanged()
);
Now, why are you getting NullPointerException
?
NullPointerException
Because you have to find view R.id.quantity
in listItemView
instead of convertView
and listItemView2
in afterTextChanged()
.
R.id.quantity
listItemView
convertView
listItemView2
afterTextChanged()
//e.g
EditText name=(EditText)listItemView2.findViewById(R.id.quantity);
Also, I'm wondering why don't you make quantityTextView
final
and use in afterTextChanged()
?
quantityTextView
final
afterTextChanged()
Because i am a filthy muggle. On another note, "getApplicationContext()" is that built into android or a getter one must place?
– Olaf
Aug 9 at 17:01
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
just write Log.e("input text", quantityTextView.getText().toString()); inside
afterTextChanged
and check– Nirav Bhavsar
Aug 8 at 16:16