I have a spinner in a toolbar, I also have replaced the toolbar with an icon and when the user selects the first option and the last option in the spinner I do not want it to show, or in other words do not want to replace the Spinner with text but the rest for the rest between the first and last option. I want them to show. How can I do this?
public void onItemSelected(AdapterView<?> adapterView,
View view, int i, long l) {
int total = adapterView.getCount();
if(i == 0){
}
else if(i == total -1){
}
// declare it inside class
private int prev_pos=0; // initially it zero, you can set your desire position
if(i =! 0 && i!= adapterView.getCount()-1 )
{
// do what you want
prev_pos=i; // store the previous position if it's not last or first
}
else{
// display previous position
yourSpinnerObject.setSelection(prev_pos);
}
Related
Everything is working. i.e. Data is read from Firebase and displayed in the FirebaseListAdapter.
Here is the code in question
int urg = model.getUrgency();
if(urg == 1) v.setBackgroundColor(Color.RED);
Problem is it randomly colors more rows red than it should. i.e For my test I made only 1 record have urg = 1. Yet when I scroll, more than 1 rows turn red. How do I fix?
private void refreshListView(){
fbAdapter = new FirebaseListAdapter<Post>(this, Post.class,R.layout.post_message, query) {
#Override
protected void populateView(View v, Post model, int position) {
TextView messageText = (TextView)v.findViewById(R.id.message_text);
TextView messageTime = (TextView)v.findViewById(R.id.message_time);
messageText.setText(model.getMessageText());
messageTime.setText(shortTime.format(model.getPostTimeStamp()));
int urg = model.getUrgency();
if(urg == 1) v.setBackgroundColor(Color.RED);
}
};
mListView.setAdapter(fbAdapter);
mListView.requestFocus();
}
The reason for red color in more than one row is, there is no else block. Add an else block will fix this issue.
int urg = model.getUrgency();
if(urg == 1){
v.setBackgroundColor(Color.RED);
}else{
v.setBackgroundColor(Color.WHITE);
}
I have a editText and a search button, Now it just shows the first results.
But I want to be able to press the search button again and then show the next indexOf. How can I do this ?
public void searchMethod(final String search) {
int i = mTextCurrentArticle.toLowerCase().indexOf(search.toLowerCase().trim());
if (i == -1) {
mEditTextSearch.setTextColor(Color.RED);
// String cap = search.substring(0, 1).toUpperCase() + search.substring(1);
//i = mTextCurrentArticle.indexOf(cap);
} else {
mEditTextSearch.setTextColor(Color.BLACK);
}
// int line = mTextViewCurrentArticle.getLayout().getLineForOffset(i);
Layout layout = mTextViewCurrentArticle.getLayout();
mScrollView.scrollTo(0, layout.getLineTop(layout.getLineForOffset(i)));
You can use the overloaded version of indexOf which takes a starting index for the search and pass the original index like this:
int i = mTextCurrentArticle.toLowerCase().indexOf(search.toLowerCase().trim());
int nextPosition = mTextCurrentArticle.toLowerCase().indexOf(search.toLowerCase().trim(), i+1);
EDIT:
If you need to cycle through subsequent positions then just make the index i a global variable and initialize it to -1
then inside your searchMethod use
i = mTextCurrentArticle.toLowerCase().indexOf(search.toLowerCase().trim(), i+1);
This will ensure that it returns the first position when called for the first time and then gives the next position when called subsequently
I have a ListActivity and In my list I have highly complex listitems with multiple ImagesViews TextViews, and Buttons. When I click a button i want to edit some of the textviews and change some background colors. My implementation works but only if the button that I click is within the first row visible. I'm using getChildAt() to grab one of the visible rows but I need to know which one to grab.
public void onClick(View v){
System.out.println("Something got clicked");
if(v.getId() == R.id.lovebutton){
MainListItem i = mainAdapter.getItem(listView.getFirstVisiblePosition());
i.loved=true;
i.loves++;
View view;
view = listView.getChildAt(0);
//view = listView.getChildAt(1);
((TextView) view.findViewById(R.id.lovecount)).setText(String.valueOf(i.loves));
view.findViewById(R.id.lovebutton).setBackgroundColor(Color.parseColor(i.brandLoveColor));
((ImageView)view.findViewById(R.id.lovebutton)).setImageResource(R.drawable.lovewhite);
}}
There are a lot of ways in which you can do this. Saving the states in the pojo, updating them in onClick and calling #notifyDataSetChanged().
Alternatively,
You can add the position as a tag to the button in getView of the adapter. In OnClick you can get the tag. This way you will know which position the button belongs to.
With a little help from Joe - Android: Access child views from a ListView
public void onClick(View v){
System.out.println("Something got clicked");
if(v.getId() == R.id.lovebutton){
int wantedPosition = Integer.parseInt(view.getTag());
int firstPosition = listView.getFirstVisiblePosition() - listView.getHeaderViewsCount(); // This is the same as child #0
int wantedChild = wantedPosition - firstPosition;
// Say, first visible position is 8, you want position 10, wantedChild will now be 2
// So that means your view is child #2 in the ViewGroup:
if (wantedChild < 0 || wantedChild >= listView.getChildCount()) {
Log.w(TAG, "Unable to get view for desired position, because it's not being displayed on screen.");
return;
}
// Could also check if wantedPosition is between listView.getFirstVisiblePosition() and listView.getLastVisiblePosition() instead.
View wantedView = listView.getChildAt(wantedChild);
MainListItem i = mainAdapter.getItem(wantedPosition);
i.loved=true;
i.loves++;
((TextView) view.findViewById(R.id.lovecount)).setText(String.valueOf(i.loves));
view.findViewById(R.id.lovebutton).setBackgroundColor(Color.parseColor(i.brandLoveColor));
((ImageView)view.findViewById(R.id.lovebutton)).setImageResource(R.drawable.lovewhite);
}
}
In a listView to get the clicked row , You have to use "OnItemClickListener".
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int pos, long arg3) {
HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(pos);
System.out.println(pos);//This will return your position
}
});
I have to set focus on listview item. ListView consist of TextView.
point where i want to set focus on listview item, at that point i have the position of that item.
I have tried setSelectio(pos), where pos is the position of listview item, but this results in setting the position of item at position on the top of the screen.
please suggest me how to proceed.
builder.setPositiveButton(R.string.save,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Log.d("#gaurav", "changeText is not null");
if (null != changeText) {
Log.d("#gaurav", "changeText is not null");
if (!(changeText.getText().toString().equalsIgnoreCase(""))) {
myList
.set(pos, changeText.getText()
.toString());
adapter.notifyDataSetChanged();
setListAdapter(adapter);
}
} else {
Log.d("#gaurav", "changeText is null");
}
getListView().setFocusableInTouchMode(true);
getListView().setSelection(pos);
getListView().requestFocus();
}
});
I think you want to highlight a particular list item based on your requirement. So you are not able to set the focus if the item is not visible in the screen. Am I right ? If that is your problem you can use following method public void smoothScrollToPosition (int position)
Added in API level 8 to scroll to that particular position and then use setSelection() method
If you want to set focus/color on particular index of ListView, you should try this, it works well.
list.post(new Runnable() {
#Override
public void run() {
list.setSelected(true);
list.getChildAt(0).setBackgroundColor(Color.BLACK);
list.getChildAt(1).setBackgroundColor(Color.BLUE);
firstListItemPosition = firstListItemPosition +1;
Log.v("firstListItemPosition", "firstListItemPosition ");
}
});
firstListItemPosition = list.getFirstVisiblePosition();
I want to draw a check mark for the image view I click on and uncheck the imageview I clicked on before using the following code snip. I store last checked position in mDeviceAdapter. When I try to uncheck old position, the image view always gives null even for the partial visible image view. I am really confused because I thought only invisible one is recycled... Newbie in Android and any comment is appreciated.
public void CheckableImageView#setChecked(boolean checked) {
if (mChecked != checked) {
mChecked = checked;
invalidate();
}
}
mDeviceGallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
CheckableImageView viewToCheck = (CheckableImageView) view;
if (!viewToCheck.isChecked()) {
int oldCheckedPosition = mDeviceAdapter
.getCheckedPosition();
mDeviceAdapter.setCheckedPosition(position);
View checkedView = mDeviceGallery
.getChildAt(oldCheckedPosition);
Log.d(TAG, "old position="+oldCheckedPosition + "old view="+checkedView);
if (checkedView != null) {
((CheckableImageView) checkedView)
.setChecked(false);
Log.d(TAG, "uncheck position="
+ oldCheckedPosition);
}
viewToCheck.setChecked(true);
That's not the right approach.
You need to add to your data type a boolean field (i.e mIsChecked).
On the onItemClick method set the value of that variable to true and keep its INDEX as a member of the adapter. When another item is clicked set the value of that item to true and set the value of the saved one to false (change the value of the datatype in you ArrayList in the INDEX you stored in the previous click).
Now, in the getView() method, you must have if/else statement. Something like:
if (item.isChecked())
{
checkedView.setChecked(true);
}
else
{
checkedView.setChecked(false);
}
Example to the onClick method: (just a general direction)
if (item.isChecked())
{
checkedView.setChecked(false);
yourList.get(position).setChecked(true);
yourList.get(mLastCheckedIndex).setChecked(false);
mLastCheckedIndex = position;
notifyDataSetChanged();
}
else
{
//same but opposite.
}
Hope this helps!