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)).
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);
}
}
});
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
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
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.