Remove data from the list in android - java

Hello every one i have a listview of array data of checkboxes which the user will select as the hobbies below is preview
and the hobbies will be shown upside but i am stuck of removing the hobbies by clicking it on up side and also when i select the 2 hobies then in show list it shows 2 times
bellow is the preview
also if i select the 3rd hobby then it show 3 times so please fix this for me and also if i uncheck the box it should remove from the view list also if i click on any hobby in the show list it still remove and uncheck automatically and if i click on save button all the hobbies should be selected
please fix this for me bellow is my code
Hobbies_Hobbies_List.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if (!Hobbies_Hobbies_List.isItemChecked(i))
{
Hobbies_Hobbies_List.setItemChecked(i,false);
}
else
{
Object object = Hobbies_Hobbies_List.getItemAtPosition(i);
String Hobby = String.valueOf(object);
for (int j=0;j<Hobbies_Hobbies_List.getCount();j++)
{
if (Hobbies_Hobbies_List.isItemChecked(j))
{
TextView text = new TextView(HobbiesActivity.this);
text.setText(Hobby+" ");
text.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
Hobbies_Show.addView(text);
}
}
}
}
});
hobbies_save_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});

It seems you don't have an adapter.
In an adapter you can modify your list (add or remove item, change text, color etc)
If you want to make your own list you will have to make your own adapter for your list. You can remove items in the adapter.
public View getView(final int position, View view, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View rowView = inflater.inflate(resource, parent, false);
TextView textView = rowView.findViewById(R.id.textView);
CheckBox checkBox = rowView.findViewById(R.id.checkBox);
textView.setText(dataSet.get(position).myText);
checkBox.setChecked(dataSet.get(position).myBool);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
// You can remove item here
}
});
return rowView;
}
For this you need your own data type.
Here a project with an example:
https://github.com/Asuki/SimpleListView
You also have to add the remove items to the other one list (maybe from the adapter)

Related

display the word in the selected spinner in the textview

I try to explain by showing as many examples as possible, I chose a word from the spinner, for example it is "cat", after I select it in the spinner i.e. after I click it should also appear in the textview.
Spinner click "cat"
TextView change "cat"
I again click Spinner "dog"
TextView change "dog"
now my not full code:
```Spinner sababspinner = findViewById(R.id.Sababspinner);
sababspinner.setOnItemClickListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}```
all spinner texts in string.xml
Make an ArrayList with your spinner dataset, then in the onItemSelected method write something like textView.setText(dataset.get(position)).

Deselecting the List item and removing the item from `arraylist`

I have a ListView implementing onItemClickListener and on its first click, I am highlighting the list item and adding it to a arrayList (Which I am sending it to next activity). What I want is to remove the ListItem from the arraylist when I click on the listitem second time, That is when deselecting the list item. This is my code that I implemented for adding. Please guide how can I remove elements.
`listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String present =listView.getItemAtPosition(position).toString();
presentStudent.add(present);
}
});`
You need to check if the list contains the object, add it if not and remove it if it's in.
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String present = listView.getItemAtPosition(position).toString();
if (presentStudent.contains(present)) {
presentStudent.remove(present);
} else {
presentStudent.add(present);
}
}
});

Set Background Color of a List Item if it is Clicked

I want to change the background color of the List Item depending on whether the item is clicked or not. How can I achieve this! I did tried the following:
articleListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
// Find the current article that was clicked on
Article currentArticle = mAdapter.getItem(position);
if (currentArticle.getUrl() != null) {
TextView article_TV = (TextView) findViewById(R.id.post_title);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
article_TV.setBackgroundColor(getApplicationContext().getColor(R.color.colorItemClicked));
}
else
article_TV.setBackgroundColor(getResources().getColor(R.color.colorItemClicked));
}
}
});
Update :-
Silly mistake it was. As suggested by ak sacha should be
TextView article_TV = (TextView) view.findViewById(R.id.post_title);
As suggested by ak sacha we can achieve it simply by using
TextView article_TV = (TextView) view.findViewById(R.id.post_title);
This is because we are using that in a adapter, so we need to find the view within listview row.Hence we use view.findviewbyid

Error: java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2

How to remove the seperator line in footerLayout? I have a footerLayout below the listView, used to display the totalAmount as shown below. If I click the seperator line in footerLayout, my app crashed.
My MainActivity
AllAdapter obj = new AllAdapter(getApplication(), search, listview,imageView,text,button);
footerLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.under_listview, null);
totalAmount = (TextView) footerLayout.findViewById(R.id.amount);
LogCat error
java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
at java.util.ArrayList.get(ArrayList.java:304)
at com.example.tony.monthlyexpenses.adapter.AllAdapter.getItem(AllAdapter.java:61)
at com.example.tony.monthlyexpenses.QuickExpenses$1.onItemClick(QuickExpenses.java:88)
at android.widget.AdapterView.performItemClick(AdapterView.java:301)
The error pointed to listView onClickListener
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> listView, View view, final int position, long id) {
mClickedPosition = position;
Expenses o = (Expenses) obj.getItem(position);
String day = o.getDate();
}
});
AllAdapter
public Expenses getItem(int position) {
return search.get(position);
}
The footerLayout is supposed to be displayed outside the listView, not inside. How can I get rid of this ?
I also have activity_main.xml, AllAdapter class, all_adapter.xml for ListView and also under_listview.xml for the footerLayout.
activity_main
AllAdapter
under_listview
How to move the footerLayout out from the ListView ?
I add android:footerDividersEnabled="false" now become like this
But still clickable !!!
The footerLayout is supposed to be displayed outside the listView, not
inside.
Actually, the footer is also part of the ListView and it adds up to the number of items the list has. Neverhteless, there are a few approaches how to ignore the click events on a footer view.
One option is instead of adding your view using:
addFooterView(footerLayout);,
use:
addFooterView(footerLayout, null, false);
The third parameter specifies whether the footer view should be selectable or not.
Another option would be to ignore the click when the position parameter is bigger than the size of your adapter dataset:
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> listView, View view, final int position, long id) {
if(position < adapter.getCount()){
Expenses expenses = (Expenses) adapter.getItem(position);
String day = expenses.getDate();
}
}
});
Try to set the following line in your listview.
android:footerDividersEnabled="false"
I change this code and it works fine now.
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> listView, View view, final int position, long id) {
Expenses o = (Expenses) listView.getAdapter().getItem(position);
if(o != null){
mClickedPosition = position;
//Expenses o = (Expenses) obj.getItem(position);
String day = o.getDate();
}
}
});
just a quick tutorial, if you are new to custom ListViewAdapters.
I would have an Object Expense like this:
class Expense {
Date date = new Date();
Double spendMoney = 5.0;
Bitmap image;
...
/** Continue with POJO **/
}
and an expenses_list_item.xml layout. (if you need help with that, i can add it later...)
In my MainActivity I would have an ArrayList<Expense> expensesList
and my custom ArrayListAdapter would look something like this:
class expenseListAdapter extends ArrayAdapter<Expense> {
expenseListAdapter() {
super(context, R.layout.expenses_list_item, expensesList);
}
#Override
public View getView(int pos, View view, ViewGroup parent) {
if (view == null) {
view = getActivity().getLayoutInflater().inflate(R.layout.list_item_series, parent, false);
}
ShowListItem current = expensesList.get(pos);
TextView expens = (TextView) view.findViewById(R.id.expenseText);
expens.setText(current.getSpendMoney());
... (all other needed values of your Expense object)
return view;
}
}
And finally, I could just set
ArrayAdapter<Expense> adapter = new expenseListAdapter();
expenseListView.setAdapter(adapter);
That would be it. Maybe a little bit complicated on the first look, but this method is very powerfull. If you need help, don't be afraid to ask ;)
Greets
Malik

How to get character from number of buttons that buttons added in GridView using Tag and position in Android

Buttons added in GridView programmatically the total number of buttons depend on word length. Each button contains a letter of word, i want to get that letter when click the button using tag,
how can i get?
here is the code:
public View getView(final int position, View convertView, ViewGroup arg2) {
View v = convertView;
if (v == null)
{
LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.buttonlist, null);
}
final Button btn= (Button)v.findViewById(R.id.letterbtn);
btn.setText(word[position]+"");
btn.setOnClickListener(new OnClickListener(){
public void onClick(View v)
{
btn.setTag(btn);
Object tag=btn.getTag();
btn.setVisibility(View.GONE);
function(position);
}
});
use
String tag=btn.getTag().toString();
or
String tag=(String)btn.getTag();
instead of
Object tag=btn.getTag();
hope it will help you. And basically you can use getText() method instead of tag which returns text from view.

Categories

Resources