Listview showing previous selection [ANDROID] - android

In my app i am showing some items in list view, each row have one name and one tick image. Tick image only shows up when item is selected(checked), The selection is working fine but the problem is when a new item is selected tick of previously selected item doesn't goes invisible. How this can be fixed? Any help will be appreciated
here is my code
Adapter
public class ListAdapter extends BaseAdapter {
private LayoutInflater inflater;
Context context;
private ArrayList<String> responseList;
private LinearLayout parent_choice_list_container;
private TextView item_name;
private ImageView iv_choice_list_item_selected;
public ListAdapter (Context context, ArrayList<String> responseList) {
this.context = context;
this.responseList = responseList;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void setSelectedIndex(int ind) {
selectedIndex = ind;
notifyDataSetChanged();
}
#Override
public int getCount() {
return responseList.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = inflater.inflate(R.layout.menu_list_item, null);
}
parent_choice_list_container = (LinearLayout) view.findViewById(R.id.parent_choice_list_container);
item_name = (TextView) view.findViewById(R.id.item_name);
iv_choice_list_item_selected = (ImageView) view.findViewById(R.id.iv_choice_list_item_selected);
iv_logo = (ImageView) view.findViewById(R.id.iv_blog_category_logo);
if (responseList.get(position).isSelected()) {
iv_choice_list_item_selected.setVisibility(View.VISIBLE);
} else {
iv_choice_list_item_selected.setVisibility(View.GONE);
} list_item_name.setText(responseList.get(position).getName());
return view;
}
}
FragmentPart (Item Click Listener)
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
response.get(position).setSelected(true);
ListAdapter.notifyDataSetChanged();
}

It sounds like you're using a CheckBox.
Have you tried conditionally updating the Views visibility depending on whether the item isChecked (and not isSelected)?

you should set tag for each image to keep track of its state (0 = not selected/invisible , 1 = selected/visible) , e.g:
if (responseList.get(position).isSelected()) {
iv_choice_list_item_selected.setTag(1);
}
and you can add this :
if (iv_choice_list_item_selected.getTag() == 1) {
iv_choice_list_item_selected.setVisibility(View.VISIBLE);
} else {
iv_choice_list_item_selected.setVisibility(View.GONE);
}

Related

Making gridview items clickable one by one

Level Selection GridView Image
I need to make Level 1 clickable and rest non clickable and when user clicks Level 1 it should make Level 2 clickable and rest non-clickable and so on so forth. Also if user is at Level 5 gridview should be clickable from level 1 to 4
myAdapter = new MyCustomAdapter(getActivity());
gridView.setAdapter(myAdapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
//do something
}
private class MyCustomAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public MyCustomAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
GridObject object = my.get(position);
GridObject revers=reverseobj.get(position);
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item_icon_set, null);
holder = new ViewHolder();
holder.text = (ImageView) convertView.findViewById(R.id.text);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
if (check.get(position).equals("true"))
{
holder.text.setImageResource(revers.getName());
}
else {
holder.text.setImageResource(object.getName());
}
return convertView;
}
#Override
public int getCount() {
return my.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
}
public class GridObject extends ArrayList<GridObject> {
private int image;
private int state;
public GridObject(int name, int state) {
super();
this.image = name;
this.state = state;
}
public int getName() {
return image;
}
public void setName(int name) {
this.image = name;
}
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
}
If I read it correctly, you want a "current level" that allows you to check/uncheck any grid item up to that level and allow you also to click on the item for the next level to make that the new current level.
If so, then it is simple to modify your adapter by adding a currentLevel variable and adjusting getView() to enable clicking on items up to and including this level.
Here is an updated version of your adapter showing this:
private class MyCustomAdapter extends BaseAdapter implements AdapterView.OnItemClickListener {
private LayoutInflater mInflater;
// implement check as boolean array rather than what appears to be string array
private final SparseBooleanArray check = new SparseBooleanArray();
// keep track of the current level
private int currentLevel = 0;
public MyCustomAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
GridObject object = my.get(position);
GridObject revers = reverseobj.get(position);
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item_icon_set, null);
holder = new ViewHolder();
holder.text = (ImageView) convertView.findViewById(R.id.text);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
if (check.get(position)) // used to be ...equals("true")
{
holder.text.setImageResource(revers.getName());
}
else {
holder.text.setImageResource(object.getName());
}
// make items up to current level clickable
convertView.setClickable(currentLevel < position);
// show unclickable items as disabled
convertView.setEnabled(currentLevel >= position);
return convertView;
}
#Override
public int getCount() {
return my.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// increase the current level if the item for the current level is clicked
currentLevel = Math.max(currentLevel, position + 1);
// toggle the checked state for the clicked item
check.put(position, !check.get(position, false));
// refresh the views
notifyDataSetChanged();
}
}
You can then enable this on your GridView in a similar way as before:
myAdapter = new MyCustomAdapter(getActivity());
gridView.setAdapter(myAdapter);
gridView.setOnItemClickListener(myAdapter);
Note that I have implemented the missing check list as a SparseBooleanArray rather than what looks like an array of strings with "true" and "false" values in your version. You can change it back if you prefer.
I have Used a Simple Check list boolean = gridview items and It Works....By Default I have set the first value true and the rest value false.......
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long
id)
{
if (clickcheck.get(position).equals(true)) {
//complete some task then
position=position+1;
clickcheck.remove(position);
clickcheck.add(position,true);
}
else
{
Toast.makeText(getActivity(), "Click On Level "+position, Toast.LENGTH_SHORT).show();
}
`

Hide textview form ArrayAdaptor from specific item postion

I want to hide a TextView which is inside of the item tv_description_of_tution on the btn_chat_room_withdraw_offer click listener. Hide TextView from particular item in ArrayAdaptor. This is my chat layout I want to hide the particular items TextView how to do this ?
In item there are muliple textview but onclick I want to hide one TextView.
class ChatRoomAdapter extends ArrayAdapter<ChatMessage> {
public ChatRoomAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
this.context = context;
notifyDataSetChanged();
}
#Override
public void add(ChatMessage object) {
chatMessageList.add(object);
super.add(object);
}
public int getCount() {
return this.chatMessageList.size();
}
public ChatMessage getItem(int index) {
return this.chatMessageList.get(index);
}
public View getView(final int position, final View convertView, ViewGroup parent) {
final ChatMessage chatMessageObj = getItem(position);
View row = convertView;
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (chatMessageObj.left) {
row = inflater.inflate(R.layout.chat_incoming_msg, parent, false);
offer = "N";
} else {
row = inflater.inflate(R.layout.chat_outgoing_msg, parent, false);
}
/*Offered Message*/
ll_offer = row.findViewById(R.id.ll_offer);
ll_btns = row.findViewById(R.id.ll_btns);
ll_withdrawn = row.findViewById(R.id.ll_withdrawn);
btn_chat_room_withdraw_offer = row.findViewById(R.id.btn_chat_room_withdraw_offer);
btn_chat_room_withdraw_offer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sendData(chatMessageObj.msg_id, position);
}
});
return row;
}
}
You can have a method in ChatRoomAdapter
private void hideMessageAt(int position){
chatMessageList.get(position).setHidden(true);
notifyDataSetChanged();
}
But you will need to add another field to the ChatMessageobject. And eventually, in getView do something like this
final ChatMessage chatMessageObj = getItem(position);
row.setVisibility(chatMessageObj.isHidden() == true ? View.GONE : View.VISIBLE);

Text values has order changes when scroll the listview in android

When I click to edit my EditText in my list-view, the order of items change. I think the problem is on adapter, but I don't know how to fix it.
Ex.: The last item goes to second position.
Adapter:
public class adapterAvaliacao extends BaseAdapter {
private LayoutInflater mInflater;
private List<Nota> itens;
DecimalFormat formatoNota = new DecimalFormat("#0.00");
public adapterAvaliacao(Context context, List<Nota> itens) {
this.itens = itens;
mInflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return itens.size();
}
#Override
public Nota getItem(int position) {
return itens.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#SuppressLint({ "ViewHolder", "InflateParams" }) #Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
Nota item = itens.get(position);
convertView = mInflater.inflate(R.layout.avaliacao_adapter, null);
((TextView) convertView.findViewById(R.id.lbl_nomeAtividade)).setText(item.getTitulo());
if (item.isLancada()) {
((EditText) convertView.findViewById(R.id.txt_notaPostada)).setText(String.valueOf(formatoNota.format(item.getNota() * 10 / item.getPeso())).replace(",", "."));
}
} else {
convertView.setTag(convertView.getTag());
}
return convertView;
}
Yes, it's the normal behaviour of the listView, therefore store your values in arraylist or model as you like so that you can set that whenever scroll.
Because of recycling of listview items, you have to do that.
your line of code to setText should be written after else part:
if (convertView == null) {
//your stuffs like find edit text
}
else {
convertView.setTag(convertView.getTag());
}
editText.setText("hello world");

How to make spinner show full items in Android

I have a problem with Android Spinner. I customised an adapter with a BaseAdapter. But when it show in the first time, it not show all items (I have only 6 items). Below is the screenshot
http://pik.vn/2014999186b9-448d-44ed-bd9b-c37484b368c6.png
And when I tap to the Spinner second time it show normally. Below is the screenshot. How can I show full item like that at the first time?
http://pik.vn/201454ed3e5b-4c67-4e9c-b8c6-feaf90c5dfb4.png
Update code:
Below is the adapter:
public class CategoryAdapter extends BaseAdapter implements SpinnerAdapter {
Context mContext;
List<CategoryModel> data;
int selectedCate;
boolean isSpinner;
Spinner spn;
public CategoryAdapter(Context context, List<CategoryModel> data, boolean isSpinner, Spinner spn) {
mContext = context;
selectedCate = 0;
this.data = data;
this.isSpinner=isSpinner;
this.spn = spn;
}
#Override
public int getCount() {
return data.size();
}
#Override
public Object getItem(int position) {
if (position < data.size()) {
return data.get(position);
} else {
return null;
}
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(R.layout.inflater_category_spinner, parent, false);
}
TextView tvName = (TextView) convertView.findViewById(R.id.inflater_category_spinner_tv);
tvName.setSelected(spn.isSelected());
tvName.setText(data.get(position).getName());
return convertView;
}
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(R.layout.inflater_category, parent, false);
ImageView img = (ImageView) convertView.findViewById(R.id.inflater_cate_image);
if (!data.get(position).getName().equals(parent.getContext().getString(R.string.browse))) {
Log.d("postion", "" + position);
UrlImageViewHelper.setUrlDrawable(img, data.get(position).getImage().getUrl());
} else {
img.setImageResource(R.drawable.ic_everything);
}
TextView tvName = (TextView) convertView.findViewById(R.id.inflater_cate_tv_name);
tvName.setText(data.get(position).getName());
return convertView;
}
public void swapView(int position) {
selectedCate = position;
}
}
and below is the code init Spinner in Activity
categoryAdapter = new CategoryAdapter(HomeActivity.this, result,true, mSpnActionbarCenterTitle);
mSpnActionbarCenterTitle.setAdapter(categoryAdapter);
The problem comes from the height of ImageView, it is wrap content and load via lazy loader, and the height of spinner not change when image loaded. My solution is fix the size of ImageView.

How to get the selected items from listview when clicking on button

I have a custom listview and i am selecting multiple items in the list, now i need to get those item names when clicking on a button the button will calls listfunction() method, you can see this in the below code.
public class Places extends Activity {
private ListView listView;
private static int selectedListItem = -1;
private Handler mHandler = new Handler();
private static Vector<String> data;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.places_custom_list_view);
data = new Vector<String>();
// Add data as per your requirement
data.add("one");
data.add("two");
data.add("three");
data.add("four");
data.add("five");
data.add("six");
listView = (ListView)findViewById(R.id.ListView01);
listView.setDivider(null);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//System.out.println(EfficientAdapter.saveState.get(position));
selectedListItem = position;
((EfficientAdapter)listView.getAdapter()).notifyDataSetChanged();
if(EfficientAdapter.saveState.get(position)=="selected"){
EfficientAdapter.saveState.put(position,"unselected");
}else
EfficientAdapter.saveState.put(position,"selected");
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
// call any new activity here or do any thing you want here
}
}, 200L);
}
});
listView.setAdapter(new EfficientAdapter(getApplicationContext()));
}
private static class EfficientAdapter extends BaseAdapter {
private LayoutInflater mInflater;
static HashMap<Integer,String> saveState=new HashMap<Integer,String>();
public EfficientAdapter(Context context) {
mInflater = LayoutInflater.from(context);
for(int i=0;i<data.size();i++)
{
saveState.put(i,"unselected");
}
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null || convertView.getTag() == null) {
convertView = mInflater.inflate(R.layout.places_custom_row_view, null);
holder = new ViewHolder();
holder.txtName = (TextView) convertView
.findViewById(R.id.name);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if(saveState.get(position).equals("selected"))
holder.txtName.setBackgroundResource(R.drawable.cellbghover);
else
holder.txtName.setBackgroundResource(R.drawable.cellbgnew);
holder.txtName.setText(data.get(position));
return convertView;
}
}
static class ViewHolder {
TextView txtName;
}
public void listfunction(View button) {
System.out.println("listfunction items are::::"+selectedListItem);
}
}
Custom listview should have custom adapter. In custom adapter you can find method named getView() along with view, position and parentgroup. When you are attempting click event, position will represent the index of the currently selected item.
public class MyArrayAdapter extends ArrayAdapter<String> {
#Override
public View getView(final int position, View convertView,ViewGroup parent) {
}
}
Use a global array to store the index of all the items clicked or touched in the list view by using the following:
listview.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> p, View view, int position,long id) {}
}
);
where position will be the index of the item starting from 0.then after clicking on button use the array to retrieve the positions of the items clicked

Categories

Resources