I've recycler view with 0 items, I've added an option to manually add the items, my stracture is simple :
RV_Item.xml (contains EditText).
MyItem, which is an Object for RV ( contains private String Text; ).
MainActivity.java, where the stuff happen.
// My List<Object>
List<MyItem> Items = new ArrayList<>();
// For Adding, I've added FAB-Button, When Clicked, it does the following :
Items.add(new MyItem());
CheckForEmptyItems();
mAdapter.notifyItemInserted(0);
Now, When the user click the save button, i want to take all the edittext in all the items he had added, i'm doing in the following way :
for(MyItem items : Items){
Log.i("PrintingInfo", items.getText() );
}
The problem is, i'm not getting the text he entered in all EditText fields, and it's returning Null in all of them, What's the issue in this ?
So, i don't know why always i know the answer after posting, but here's how you gonna know what the user typed :
in your Adapter Class, in onBindViewHolder method, add textlistener for the EditText, here's an example :
holder.MyEditText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
itemList.get(position).setText(s.toString());
}
});
Hope that helps you!
Related
I created a layout that has an autoCompleteTextView in it that should show some names of books.
I noticed that it show no results in some cases unless I delete one letter and then it shows the correct results.
For example, I have an array string of books that has the word Potter in them.
When I type Potter, it shows no results:
However, if I then delete one letter it suddenly shows:
The code im using to populate the list is:
atv_Search.addTextChangedListener( new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length() > 0) {
SOME CODE TO GET NAMES
}
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
Set<String> set = new LinkedHashSet<>( autoBookNames );
autoBookNames.clear();
autoBookNames.addAll( set );
// UNTIL THIS POINT IT ALL WORKS GOOD AND I SEE THAT authBookNames IS NOT EMPTY, YET NOT SHOWING UNTIL DELETE
listAutoAdapter = new ArrayAdapter<String>( getApplicationContext(), R.layout.item_auto_search, autoBookNames );
atv_Search.setThreshold( 1 );
atv_Search.setAdapter( listAutoAdapter );
}
} );
As I wrote in the code when I debugged it, I did get all the results even before the deletion but it didn't show it for some reason.
Any reason for this? I mean most users won't think to delete a letter to show the results.
Thank you
try==>
listAutoAdapter = new ArrayAdapter<String>( getApplicationContext(), R.layout.item_auto_search, autoBookNames );
atv_Search.setAdapter( listAutoAdapter );
atv_Search.addTextChangedListener( new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
listAutoAdapter.getFilter().filter(s);
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
} );
let me know if this works
Do you think that your compare strings' index in "SOME CODE TO GET NAMES" is wrong (such as last index is smaller than the correct one 1 character)
Move
listAutoAdapter = new ArrayAdapter<String>( getApplicationContext(), R.layout.item_auto_search, autoBookNames );
atv_Search.setThreshold( 1 );
atv_Search.setAdapter( listAutoAdapter );
into onTextchanged and try once, Ben!
In my project I have 7 EditText field.
User will only need to fill their own value in first two EditText.
I need a series calculation like this...
(after user input) subtract ed1-ed2 the result automatically show in ed3 field.
multiply ed3*2 = result show in ed4 field automatically.
multiply ed3*3= result will show in ed5 field.
multiply ed3*4=result will show in ed6 field.
Finally sum of ed4+ed5+ed6= result will show in ed7 field automatically.
So how to do it any one please help.
Thank you in Advance.
All EditText use a same TextWatcher. A simple solution like this.
EditText et1;
EditText et2;
TextWatcher textWatcher = new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
int num1 = Integer.valueOf(et1.getText().toString()); // Make sure you get a number
int num2 = Integer.valueOf(et2.getText().toString());
// do your calculation
}
#Override
public void afterTextChanged(Editable s) {
}
};
et1.addTextChangedListener(textWatcher);
et2.addTextChangedListener(textWatcher);
I'm kinda new to java programming for android, so if i make stupid mistakes, i'm sorry.
So basically, what i wanna make is an app where if you type in the answer correctly, the next textview is gonna be displayed. And when the next textView is displayed, you're needing to give a answer to that textView, when the answer is given correctly. The textview changes again. And so on.
Does anybody have an idea how to do this?
If you don't undarstand what im saying, here is a example:
public class Game extends AppCompatActivity {
public static EditText editText_ans;
public static TextView textView_1;
String enteredText = editText.getText().toString();
If(enteredText = 3 && textView_1 = #string/1+2){
setText.textView_1(#string/3+4)
}
If(enteredText = 7 && textView_1 = #string/3+4){
setText.textView_1("100 - 23")
I'm really stuck and i hope that you guys wanna help me.
If you wanted to change the view without button you can use method addTextChangeListner() which will notify you when when the text hasbeen change for particular edittext.
edittext.addTextChangedListener(textWatcher);
private final TextWatcher textWatcher = new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
textView.setVisibility(View.VISIBLE);
}
public void afterTextChanged(Editable s) {
if (s.length() == 0) {
textView.setVisibility(View.GONE);
} else{
textView.setText("You have entered : " + editText.getText());
}
}
};
int x=0; //to keep track of qustions
private List<String> mQuestionList=new ArrayList<>(); //array of question
private List<String> mAnswerList=new ArrayList<>(); //array of question answer
displayquestion.settext(mQuestionList.get(x);//displayquestion is textview
//nextquestion is the button when user click it will first check answer and than move to next question if answer is correct
nextquestion.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String answer=editText.getText().toString();
if(answer.equal(mAnswerList.get(x)){
x=x+1;
displayquestion.settext(mQuestionList.get(x); //answer is correct display next quesion
}else{
//wrong answer
}
}
});
I know that there are multiple posts in Stackoverflow addressing this query. However, for some reason I am still failing to extract the string from AutoCompleteTextView. I tried using the onItemClickListener for this purpose. I am unable to identify where I am going wrong.
The Code :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
addPurchaseItemName = (AutoCompleteTextView) findViewById(R.id.addPurchaseProductName);
vivzHelper = new VivzDatabaseAdapter(this);
String[] autoCompleteName = vivzHelper.getInventoryNameFilterBySupplierName(vivzHelper.getSupplierID(param1));
ArrayAdapter<String> NameAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, autoCompleteName);
addPurchaseItemName.setThreshold(1);// starts working from first char
addPurchaseItemName.setAdapter(NameAdapter);
addPurchaseItemName.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
itemName = String.valueOf(arg0.getItemAtPosition(arg2));
}
});
}
I want to assign the value of the extracted string to itemName which is initialized at the beginning of the activity. Can some one point out where am I going wrong? I have surfed multiple resources. Maybe, I am missing something.
Note :
This code was already posted to address a an issue on IllegalArgumentException in StackOverFlow a couple of days ago. Since, the topic of that question doesn't point more specifically the problem posted here, I thought that posting a new question will make sense. Hence I hope, my question won't be down-voted or edited as duplicate
Update 01 : #Deividi Cavarzan forgot including the below line of code when editing this question
ArrayAdapter<String> NameAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, autoCompleteName);
Update 02 : Declaring the itemName
public class AddPurchase extends ActionBarActivity {
AutoCompleteTextView addPurchaseItemName;
String itemName;
try to get the itemName on addTextChangedListener callback
addPurchaseItemName.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
itemName = s.toString();
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
if you just want to get the selected item string from auto complete, then
itemName = addPurchaseItemName.getText();
or even better-
itemName = addPurchaseItemName.getText().toString().trim();
Updated answer
instead of setting onItemClickListener , set OnItemSelectedListener , this should definitely work.
addPurchaseItemName.setOnSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected (AdapterView<?> parent, View view, int position, long id) {
//... your stuff
itemName = addPurchaseItemName.getText().toString().trim();
Toast.makeText(getApplicationContext(), "selected value - "+itemName, Toast.LENGTH_SHORT);
}
});
Im having a lot of problems with searching for part of a word, and then making it find the full result afterwards.
Like if im searching "droid", it looks through my list for anything that contains "droid", if you look at my arraylist underneath it should find "Android"
Arraylist code:
private List<String> getModel() {
List<String> list = new ArrayList<String>();
list.add("Linux");
list.add("Windows7");
list.add("Suse");
list.add("Eclipse");
list.add("Ubuntu");
list.add("Solaris");
list.add("Android");
list.add("iPhone");
return list;
}
Here's the rest of my code:
public class idchart extends ListActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.idchart);
/*Resources res = getResources();
ListView listView=(ListView)findViewById(R.id.list);*/
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
android.R.id.text1,
getModel());
setListAdapter(adapter);
EditText filterEditText = (EditText) findViewById(R.id.srcBox);
filterEditText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
adapter.getFilter().filter(s.toString());
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
}
As suggested, using Regex, as far I know you would have to iterate the list and try to match every entry.
If you want to avoid iteration, you can use a inverted index approach for each character (like NGram) of the word. If you want to search text starting with what the user sends, you can do it with Java Map (Commons MultiMap). If you want to search by "droid", so you would have to index the position of each character and retrieve the relative results. I don't recommend doing that without a Library like Lucene.