I need a quick solution for my problem. I have a ListView, and I want to search from that. Im a kind a starter with the filtering and the adapters, so this is my Fragment's code:
View v;
DB mydb;
ListView listView;
final ArrayList<String> thelist = new ArrayList<>();
final ArrayList<String> deletelist= new ArrayList<>();
boolean torles=false;
String delete; int id;
private ListAdapter listadapter;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
v = inflater.inflate(R.layout.fragment_numberone, container, false);
listView = (ListView) v.findViewById(R.id.list);
mydb = new DB(getActivity());
loadlist();
EditText filter = (EditText)v.findViewById(R.id.filter);
filter.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) {
getActivity().listadapter.getFilter().filter(s);
//Here is the problem
}
#Override
public void afterTextChanged(Editable s) {
}
});
listView.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
id = Integer.parseInt(deletelist.get(i));
delete=thelist.get(i);
AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
alert.setTitle("Title");
alert.setMessage("Message");
alert.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
mydb.grade_delete(id);
listView.removeAllViewsInLayout();
thelist.removeAll(thelist);
deletelist.removeAll(deletelist);
loadlist();
}
});
alert.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
alert.create().show();
}
}
);
return v;
}
public void loadlist() {
Cursor data = mydb.Jegynevlekerdezes();
if (data.getCount() == 0) {
} else {
while (data.moveToNext()) {
thelist.add(data.getString(0)+": "+data.getString(1));
deletelist.add(data.getString(2));
listadapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, android.R.id.text1, thelist);
listView.setAdapter(listadapter);
}
}
}
}
My problem is with this:
getActivity().listadapter.getFilter().filter(s);
The listadapter is wrong, it's show as red. Like I sad, I'm new with the adapter and the searching.
Related
When I add spinner in a recycler view (spinner dropdown contains edit texts) dynamically with button as the counter, spinner old dropdown values set to zero).value refreshes always when count increase.
public class MainActivity extends AppCompatActivity {
private Button btn;
private RecyclerView recyclerView;
private CustomAdapter customAdapter;
public ArrayList<EditModel> editModelArrayList;
int count=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.recycler);
btn = (Button) findViewById(R.id.btn);
Button btnadd = (Button) findViewById(R.id.button);
btnadd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
count= (int) (count+1);
arrayadd();
}
});
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,NextActivity.class);
startActivity(intent);
}
});
}
private void arrayadd() {
int position=0;
// customAdapter.notifyDataSetChanged();;
editModelArrayList = populateList();
customAdapter = new CustomAdapter(this,editModelArrayList);
recyclerView.setAdapter(customAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false));
}
private ArrayList<EditModel> populateList()
{
ArrayList<EditModel> list = new ArrayList<>();
for(int i = 0; i < (count); i++){
EditModel editModel = new EditModel();
editModel.setEditTextValue("");;
editModel.setEditttt("");
editModel.setChequedate("");
editModel.setChequeamount("");
list.add(editModel);
}
return list;
}
}
Custom adapter class
public CustomAdapter(Context ctx, ArrayList<EditModel> editModelArrayList){
inflater = LayoutInflater.from(ctx);
this.editModelArrayList = editModelArrayList;
}
#Override
public CustomAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.rv_item, parent, false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(final CustomAdapter.MyViewHolder holder, final int position) {
holder.editText.setText(editModelArrayList.get(position).getEditTextValue());
holder.editTexts.setText(editModelArrayList.get(position).getEditttt());
holder.edchequedate.setText(editModelArrayList.get(position).getChequedate());
holder.edchequeamount.setText(editModelArrayList.get(position).getChequeamount());
Log.d("print","yes");
}
#Override
public int getItemCount() {
return editModelArrayList.size();
}
class MyViewHolder extends RecyclerView.ViewHolder{
protected EditText editText;
protected EditText editTexts;
protected EditText edchequedate;
protected EditText edchequeamount;
/* protected EditText editTexts;
protected EditText editTexts;*/
public MyViewHolder(final View itemView) {
super(itemView);
final MaterialSpinner mts;
mts=itemView.findViewById(R.id.et_amounts);
mts.setItems("Cheque","DD","MO");
mts.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener() {
#Override
public void onItemSelected(MaterialSpinner view, int position, long id, Object item) {
L11 = (CardView) itemView.findViewById(R.id.L11);
L22 = (CardView) itemView.findViewById(R.id.L22);
L33 = (CardView) itemView.findViewById(R.id.L33);
System.out.println("itemj"+item);
if (position==0){
L11.setVisibility(View.VISIBLE);
L22.setVisibility(View.GONE);
L33.setVisibility(View.GONE);
}
else
if(position==1){
// L22.setVisibility(View.VISIBLE);
L11.setVisibility(View.GONE);
L22.setVisibility(View.VISIBLE);
L33.setVisibility(View.GONE);
}
else if (position==2){
L33.setVisibility(View.VISIBLE);
L11.setVisibility(View.GONE);
L22.setVisibility(View.GONE);
}
}
});
editText = (EditText) itemView.findViewById(R.id.editidaaaaaa);
editTexts = (EditText) itemView.findViewById(R.id.editids);
edchequedate = (EditText) itemView.findViewById(R.id.edt_chqdate);
edchequeamount = (EditText) itemView.findViewById(R.id.edt_chequeamnt);
editText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
editModelArrayList.get(getAdapterPosition()).setEditTextValue(editText.getText().toString());
}
#Override
public void afterTextChanged(Editable editable) {
}
});
editTexts.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
editModelArrayList.get(getAdapterPosition()).setEditttt(editTexts.getText().toString());
}
#Override
public void afterTextChanged(Editable editable) {
}
});
edchequedate.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
editModelArrayList.get(getAdapterPosition()).setChequedate(edchequedate.getText().toString());
}
#Override
public void afterTextChanged(Editable editable) {
}
});
edchequeamount.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
editModelArrayList.get(getAdapterPosition()).setChequeamount(edchequeamount.getText().toString());
}
#Override
public void afterTextChanged(Editable editable) {
}
});
}
}
}
This happens because the recyclerview gets a new adapter and list every increment. Move out your initialization of the adapter and list. Then in every increment add something to the list then call notifyDataSetChanged
int count=0;
private Button btn;
private RecyclerView recyclerView;
private CustomAdapter customAdapter;
public ArrayList<EditModel> editModelArrayList = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
customAdapter = new CustomAdapter(this, editModelArrayList);
recyclerView = (RecyclerView) findViewById(R.id.recycler);
recyclerView.setAdapter(customAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false));
btn = (Button) findViewById(R.id.btn);
Button btnadd = (Button) findViewById(R.id.button);
btnadd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
count= (int) (count+1);
arrayadd();
}
});
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, NextActivity.class);
startActivity(intent);
}
});
}
private void arrayadd() {
// Only add the difference of the count and the length
// the loop will only occur once since count increment is 1
for(int i = 0; i < (editModelArrayList.size() - count); i++){
EditModel editModel = new EditModel();
editModel.setEditTextValue("");;
editModel.setEditttt("");
editModel.setChequedate("");
editModel.setChequeamount("");
editModelArrayList.add(editModel);
}
customAdapter.notifyDataSetChanged();
}
In my AlertDialog, I have a Spinner and an EditText. I am trying to disable the positive button of the dialog if
selected item at Spinner is "Select currency" ("Select currency" is the first item in my Array and Spinner is set to return String currencyName as "") or
EditText is empty.
In the code below, if I have input in EditText before selecting item in Spinner, the positive button remains disabled. I have no idea how to make these two work together.
Any help will be much appreciated. Thanks.
private void openDialog(){
LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
View subView = inflater.inflate(R.layout.dialog_layout, null);
final EditText subEditTextExchangeRate = subView.findViewById(R.id.textInputExchangeRate);
final Spinner subSpinner = subView.findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.currency, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
subSpinner.setAdapter(adapter);
subSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if(parent.getItemAtPosition(position).equals("Select currency")){
currencyName = "";
}else {
currencyName = parent.getItemAtPosition(position).toString();
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(subView);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
exchangeRate = subEditTextExchangeRate.getText().toString();
st2 = exchangeRate;
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
final AlertDialog alertDialog = builder.create();
alertDialog.show();
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
subEditTextExchangeRate.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) {
String exchangeRateInput = subEditTextExchangeRate.getText().toString();
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(!exchangeRateInput.isEmpty() && !currencyName.isEmpty());
}//This is the logic that I am looking for but the method is watching only EditText
#Override
public void afterTextChanged(Editable s) {
}
});
}
Please try below code with only one change, put below line in subSpinner.setOnItemSelectedListener and also please declare alertDialog globally.
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(!exchangeRateInput.isEmpty() && !currencyName.isEmpty());
Here is Your Code.
private void openDialog(){
LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
View subView = inflater.inflate(R.layout.dialog_layout, null);
final EditText subEditTextExchangeRate = subView.findViewById(R.id.textInputExchangeRate);
final Spinner subSpinner = subView.findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.currency, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
subSpinner.setAdapter(adapter);
subSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if(parent.getItemAtPosition(position).equals("Select currency")){
currencyName = "";
}else {
currencyName = parent.getItemAtPosition(position).toString();
}
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(!exchangeRateInput.isEmpty() && !currencyName.isEmpty());
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(subView);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
exchangeRate = subEditTextExchangeRate.getText().toString();
st2 = exchangeRate;
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog = builder.create();
alertDialog.show();
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
subEditTextExchangeRate.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) {
String exchangeRateInput = subEditTextExchangeRate.getText().toString();
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(!exchangeRateInput.isEmpty() && !currencyName.isEmpty());
}//This is the logic that I am looking for but the method is watching only EditText
#Override
public void afterTextChanged(Editable s) {
}
});
}
You should invoke button enable/disable logic Spinner.onItemSelected of your spinner. As of now you are only checking for logic in EditText.onTextChanged.
Declare your AlertDialog before setting listener on Spinner and EditText.
In Spinner.onItemSelected add your logic to enable or disable button.
In EditText.onTextChanged add your logic to enable or disable button.
you can make work your code if you change your code like,
Button button = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
button.setEnabled(false);
subEditTextExchangeRate.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) {
if(s.length>0 && !TextUtils.isEmpty(currencyName )){
button.setEnabled(true);
// your logic
}else{
button.setEnabled(false);
// your logic
}
#Override
public void afterTextChanged(Editable s) {
}
});
->this will do your work.
Good Day, I'm new to android development and i practice CRUD operation but i got an error in deleting the the selected item on my List View and i want to Update the list view after deleting.
Here's my code for deleting in Database Helper
public void deletegroce(int groceId){
String groceid[] = {String.valueOf(groceId)};
SQLiteDatabase sqdb = this.getWritableDatabase();
sqdb.delete(TABLE_NAME, KEY_GROCE_ID + " = ?", groceid);
sqdb.close();
}
}
And this is my code for my list
public class Grocery_list extends Activity {
ListView lvGorc;
DatabaseHelper dbhelper;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grocery_list);
lvGorc = (ListView) findViewById(R.id.lvGroc);
dbhelper = new DatabaseHelper(Grocery_list.this);
final ArrayList<String> aList = dbhelper.getAllGroceries();
ArrayAdapter<String> La = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, aList);
lvGorc.setAdapter(La);
lvGorc.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(final AdapterView<?> adapterView, View view, int i, long l) {
AlertDialog.Builder adb = new AlertDialog.Builder(Grocery_list.this);
adb.setTitle("Option");
adb.setMessage("What do you want to do?");
adb.setPositiveButton("Update", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
adb.setNegativeButton("Delete", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dbhelper.deletegroce(i);
}
});
adb.show();
}
});
La.notifyDataSetChanged();
lvGorc.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
AlertDialog.Builder adb = new AlertDialog.Builder(Grocery_list.this);
adb.setTitle("Delete?");
adb.setMessage(aList.get(position));
adb.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dbhelper.deletegroce(aList.get(position));
}
});
adb.show();
return true;
}
});
}
}
THANK YOU SO MUCH
Your deleteGroce() method expects an int (groceId), but your ArrayAdapter get() method will return a String value, you need to use Int.parseInt(aList.get(position)) instead of just aList.get(position) in the call method :)
I'm trying to do a search function in the list view and I'm using Fundapter and not normal Adapter. However when I try to type something into the edit text, I get:
Attempt to invoke virtual method 'void android.widget.Filter.filter(java.lang.CharSequence)' on a null object reference
This is my code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_view, container, false);
final ArrayList<Participants> participants = new ArrayList<>();
//then i hardcoded the participants data...
//participants.add(p1);
BindDictionary<Participants> dictionary = new BindDictionary<>();
dictionary.addStringField(R.id.tvName, new StringExtractor<Participants>() {
#Override
public String getStringValue(Participants participants, int position) {
return participants.getName();
}
});
dictionary.addStringField(R.id.tvCompany, new StringExtractor<Participants>() {
#Override
public String getStringValue(Participants participants, int position) {
return participants.getCompanyName();
}
});
dictionary.addStringField(R.id.tvNRIC, new StringExtractor<Participants>() {
#Override
public String getStringValue(Participants participants, int position) {
return participants.getNRIC();
}
});
final FunDapter adapter = new FunDapter(ViewFragment.this.getActivity(), participants, R.layout.participants_layout, dictionary);
ListView lvParticipants = (ListView) view.findViewById(R.id.lvParticipants);
lvParticipants.setAdapter(adapter);
etSearch = (EditText) view.findViewById(R.id.etSearch);
etSearch.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
adapter.getFilter().filter(s);
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
lvParticipants.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Participants selectedParticipants = participants.get(position);
Toast.makeText(ViewFragment.this.getActivity(), selectedParticipants.getName(), Toast.LENGTH_SHORT).show();
}
});
return view;
}
Shouldn't it be onTextChnaged(), you are doing your operation in beforeTextChnaged()... look into it.
The problem that I am having is that once I perform a search on my ListView, I am unable to delete or add anything else to my List. Additionally, I am getting an IndexOutofBounds error. My code is as follows:
public class MainActivity extends AppCompatActivity {
public ArrayList<String> array;
public ArrayAdapter<String> adapt;
public ListView list;
public EditText search;
#Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.list_item);
array = new ArrayList<>();
adapt = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, array);
list.setAdapter(adapt);
setupListViewListener();
search();
}
public void onAddItem(View v) {
EditText edit = (EditText) findViewById(R.id.edit);
String item = edit.getText().toString();
adapt.add(item);
edit.setText("");
}
public void setupListViewListener() {
list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View v, int i, long l) {
array.remove(i);
adapt.notifyDataSetChanged();
return true;
}
});
}
public void search()
{
search = (EditText) findViewById(R.id.inputSearch);
search.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
adapt.getFilter().filter(cs);
adapt.notifyDataSetChanged();
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
#Override
public void afterTextChanged(Editable arg0) {
}
});
}
public void deleteAll(View view) {
adapt.clear();
adapt.notifyDataSetChanged();
}
}