Change TextView text to Normal after the object has been pressed - java

I currently have a ListView which uses a CustomAdapter to display the information from my Firebase Database. Which looks like the following:
Upon creation I want the text in the green boxes (The ListView Items) to be bold. Once the user presses on that ListView item I want the text to go to Normal, unbolded.
I want it to work similar to an email. When you get a new email the title is bold and once that email is opened and you go back to your inbox the text is the Normal, unbolded.
The following is all my relevant code:
MyJobsFragment, SelectItem
// Press the object and display the information and sign the job of with signature pad
jobListViewMyAcJobs.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
ActiveJobDetailsFragment activeJobDetailsFragment = new ActiveJobDetailsFragment();
Bundle bundle = new Bundle();
bundle.putSerializable("Job", adapterActiveJobs.mData.get(position));
bundle.putSerializable("JobId", adapterActiveJobs.mDataKeys.get(position));
activeJobDetailsFragment.setArguments(bundle);
getFragmentManager().beginTransaction().replace(R.id.content, activeJobDetailsFragment).addToBackStack(host.getCurrentTabTag()).commit();
}
});
MyCustomAdapter
public class MyCustomAdapter extends BaseAdapter
{
private ArrayList<JobInformation> mData = new ArrayList<>();
private ArrayList<JobInformation> mDataOrig = new ArrayList<>();
private ArrayList<String> mDataKeys = new ArrayList<>();
private LayoutInflater mInflater;
... Removed Code ...
#Override
public View getView(final int position, View convertView, ViewGroup parent)
{
// Bid on holder
MyJobsFragment.MyCustomAdapter.GroupViewHolderBidOn holderBidOn;
// Accepted holder
final MyJobsFragment.MyCustomAdapter.GroupViewHolderAccepted holderAccepted;
// Completed holder
MyJobsFragment.MyCustomAdapter.GroupViewHolderCompleted holderCompleted;
if (convertView == null)
{
// Bid on
if (host.getCurrentTab() == 0)
{
convertView = mInflater.inflate(R.layout.job_info_bid_on, null);
holderBidOn = new MyJobsFragment.MyCustomAdapter.GroupViewHolderBidOn();
holderBidOn.textViewJobName = convertView.findViewById(R.id.textName);
holderBidOn.textViewJobDescription = convertView.findViewById(R.id.textDesc);
holderBidOn.textViewJobName.setText(mData.get(position).getAdvertName());
holderBidOn.textViewJobDescription.setText(mData.get(position).getAdvertDescription());
convertView.setTag(holderBidOn);
}
// Accepted
else if (host.getCurrentTab() == 1)
{
convertView = mInflater.inflate(R.layout.job_info_accepted, null);
holderAccepted = new MyJobsFragment.MyCustomAdapter.GroupViewHolderAccepted();
holderAccepted.textViewJobName = convertView.findViewById(R.id.textName);
holderAccepted.textViewDescription = convertView.findViewById(R.id.textDesc);
holderAccepted.textViewJobName.setText(mData.get(position).getAdvertName());
if(text != null)
{
text.setTypeface(null, Typeface.BOLD);
}
holderAccepted.textViewDescription.setText(mData.get(position).getAdvertDescription());
// TODO - Was going to add the the users bid into this here, however it's difficult as the bid isnt stored in the jobs table
// TODO - I tried to add it in, looped through the bids table and found the bids with the mDataKeys. But it always displayed the last value.
convertView.setTag(holderAccepted);
}
// Completed
else if (host.getCurrentTab() == 2)
{
convertView = mInflater.inflate(R.layout.job_info_list_completed, null);
holderCompleted = new MyJobsFragment.MyCustomAdapter.GroupViewHolderCompleted();
holderCompleted.textViewJobName = convertView.findViewById(R.id.textName);
holderCompleted.textViewJobName.setText(mData.get(position).getAdvertName());
convertView.setTag(holderCompleted);
}
} else
{
if (host.getCurrentTab() == 0)
{
holderBidOn = (MyJobsFragment.MyCustomAdapter.GroupViewHolderBidOn) convertView.getTag();
} else if (host.getCurrentTab() == 1)
{
holderAccepted = (MyJobsFragment.MyCustomAdapter.GroupViewHolderAccepted) convertView.getTag();
} else if (host.getCurrentTab() == 2)
{
holderCompleted = (MyJobsFragment.MyCustomAdapter.GroupViewHolderCompleted) convertView.getTag();
}
}
return convertView;
}
public class GroupViewHolderBidOn
{
public TextView textViewJobName;
public TextView textViewJobDescription;
}
public class GroupViewHolderAccepted
{
public TextView textViewJobName;
public TextView textViewDescription;
}
public class GroupViewHolderCompleted
{
public TextView textViewJobName;
}
}

Well there multiple ways of achieving that. I would suggest you to add a field in your JobInformation class, something like
public class JobInformation{
.
.
.
.
. // other fields
private boolean isSelected;
public boolean isSelected() {
return isSelected;
}
public void setSelected(boolean selected) {
isSelected = selected;
}
.
.
. // Other getters/setters
}
Now you need to set this field when you click an item, in your onItemClick function do this
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
ActiveJobDetailsFragment activeJobDetailsFragment = new ActiveJobDetailsFragment();
Bundle bundle = new Bundle();
adapterActiveJobs.mData.get(position).setSelected(true); // add this line in your code
bundle.putSerializable("Job", adapterActiveJobs.mData.get(position));
bundle.putSerializable("JobId", adapterActiveJobs.mDataKeys.get(position));
activeJobDetailsFragment.setArguments(bundle);
getFragmentManager().beginTransaction().replace(R.id.content, activeJobDetailsFragment).addToBackStack(host.getCurrentTabTag()).commit();
}
finally in your getView method in your custom adapter, check for isSelected field and style your textview accordingly, add this in your getView method.
if(mData.get(position).isSelected()){
// set your textview typeface to normal
}
else{
// set your texview typeface to bold.
}
and you would need to call adapter.notifyDataSetChanged() whenever you update selected variable so that your getView is called.

Related

update the text of a listView row

I want my listView to be updated after clicking on a row (or any event, but let's focus on click).
I did something, but it updates more than one row (maybe it updates the first visible row and the one after the last visible...).
Here is the full code
Activity code
DatabaseHandler colisageBase;
ListView listView;
List<Site> sites;
String id_tournee;
SiteAdapter siteAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_site_choice);
Intent intent = getIntent();
id_tournee = intent.getStringExtra("idTourneeSelectionnee");
this.listView = findViewById(R.id.list_view_site);
this.colisageBase = new DatabaseHandler(this);
sites = colisageBase.selectAllSite(id_tournee);
siteAdapter = new SiteAdapter(SiteChoiceActivity.this, sites);
listView.setAdapter(siteAdapter);
colisageBase.closeDB();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Site selectedSite = sites.get(position);
selectedSite.setIsBarred(true);
sites.set(position, selectedSite);
siteAdapter.notifyDataSetChanged();
//goToOperationActivity(selectedSite.SiteOut());
}
});
Adapter code
public class SiteAdapter extends ArrayAdapter<Site> {
public SiteAdapter(Context context, List<Site> sites) {
super(context, 0, sites);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = LayoutInflater.from(getContext()).inflate(R.layout.row_site,parent, false);
}
SiteViewHolder viewHolder = (SiteViewHolder) convertView.getTag();
if(viewHolder == null){
viewHolder = new SiteViewHolder();
viewHolder.heure_supposee = convertView.findViewById(R.id.heure_supposee);
viewHolder.libelle_site = convertView.findViewById(R.id.libelle_site);
viewHolder.logo_telephone = convertView.findViewById(R.id.logo_phone);
convertView.setTag(viewHolder);
}
Site site = getItem(position);
viewHolder.heure_supposee.setText(site.getHeure_supposee());
viewHolder.libelle_site.setText(site.getLibelle_site());
viewHolder.logo_telephone.setVisibility(View.INVISIBLE);
if (site.getSur_appel().equals("O")) viewHolder.logo_telephone.setVisibility(View.VISIBLE);
if (site.isBarred()) viewHolder.libelle_site.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG);
return convertView;
}
#Override
public void notifyDataSetChanged()
{
super.notifyDataSetChanged();
}
private class SiteViewHolder{
public TextView heure_supposee;
public TextView libelle_site;
public ImageView logo_telephone;
}
}
Please suggest what's wrong with the code.
The answer was given in the comments by I_A_Mok, but i have to add more details:
In the case of a cell, when you do an action in an "if" condition , you usually have to do the opposite in an "else" condition.
In my case, after my condition where I strike through text, I had to add an else condition where I don't strike through text.
if (site.isBarred()){
viewHolder.libelle_site.setPaintFlags(viewHolder.libelle_site.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
}else {
viewHolder.libelle_site.setPaintFlags(viewHolder.libelle_site.getPaintFlags() & (~ Paint.STRIKE_THRU_TEXT_FLAG));
}

Handling "int java.util.List.size()" Error ListView

I have data that I receive from a server and the receiving part goes well. But when there is no data is gives this error int java.util.List.size() which I know means that there is no data.
I tried handling with get Count but it still gives me an error, so I decided to ask help from here. What could I be doing wrong?
Thanks in advance for any help!
This is my code:
private void DataFound(){
final ArrayAdapter<DataObject> adapter = new ArrayAdapter<DataObject>(getApplicationContext(), R.layout.data_item, searchResults){
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = getLayoutInflater().inflate(R.layout.video_item, parent, false);
}
TextView title = convertView.findViewById(R.id.video_title);
DataObject searchResult = searchResults.get(position);
title.setText(searchResult.getTitle());
return convertView;
}
};
if(adapter.getCount() == 0){
emptyView.setVisibility(View.VISIBLE);
}else {
ListView.setAdapter(adapter);
}
}
If you are checking what seems to be a java.util.List with the search results, you should do this in a defensive and bullet proof manner. There should be two checks:
Check, if the results list is not null.
Check, if the results list is not empty.
If it is empty, provide an understandable message with some suggestions to the end user on how the user can improve the search results, ideally with some examples.
Here is an dummy example on how you could do it:
private void dataFound(){
List<SearchResult> searchResults = ...
final ArrayAdapter<DataObject> adapter = new ArrayAdapter<DataObject>(getApplicationContext(), R.layout.data_item, searchResults){
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = getLayoutInflater().inflate(R.layout.video_item, parent, false);
}
TextView title = convertView.findViewById(R.id.video_title);
if(searchResults != null && !searchResults.isEmpty()) {
DataObject searchResult = searchResults.get(position);
title.setText(searchResult.getTitle());
}
else {
title.setText("No search results available. Please try again with another input, like e.g: 'foo' or 'bar'");
}
return convertView;
}
};
if(adapter.getCount() == 0){
emptyView.setVisibility(View.VISIBLE);
}else {
ListView.setAdapter(adapter);
}
}
private static class SearchResult {
private String title;
public SearchResult(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
}
Define your adapter like below:
public class Adapter extends ArrayAdapter<DataObject> {
private List<DataObject> dataObjectList;
public Adapter(#NonNull Context context, int resourceId) {
super(context, resourceId);
this.dataObjectList= new ArrayList<>();
}
public void setDataObjectList(List<DataObject> dataObjectList) {
this.dataObjectList= dataObjectList;
notifyDataSetChanged();
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
if(convertView == null){
convertView = getLayoutInflater().inflate(R.layout.video_item, parent, false);
}
TextView title = convertView.findViewById(R.id.video_title);
DataObject searchResult = dataObjectList.get(position);
title.setText(searchResult.getTitle());
return convertView;
}
#Override
public int getCount() {
return dataObjectList.size();
}
}
Initialize and set data to adapter like below:
private void dataFound() {
List<SearchResult> searchResults = ...
if (searchResults != null && !searchResults.isEmpty()) {
Adapter adapter = new Adapter(getApplicationContext(), R.layout.your_layout);
adapter.setDataObjectList(searchResults);
} else {
// give error message
}
}
That way you don't have to worry about list with zero items.
Please set condition like this
if(searchResults!=null && searchResults.size()>0){
set adapter
}
else{
show toast
}

how to save state of view in listview

In my app, I display a check list using a ListView. Every row in my check list is represented by a model, which is an object of the ChecklistenPunkt class. There are three different types of rows:
a headline
an additional text
a question
The third type has two Buttons in it which are linked by two OnClickListeners: The first Button is for a positive answer, the second one for a negative answer. When the view is build, both of them are gray, indicating that this question is not yet answered. If the left Buttonis clicked it turns green and the second one is getting a dark gray background. If the right Button is clicked it turns red and the first one is getting a dark gray background. When a Button is clicked the answering state is saved as an ChecklistenPunktStatus. This is an Enum which has three entries (okay, not okay, not answered).
Here is a little image of the Buttons in three rows, showing the different states:
Here is my adapter code:
public class ChecklisteAdapter extends ArrayAdapter<Object> {
private List<ChecklistenPunkt> list;
private SparseArray<ChecklistenPunktStatus> sparseArray;
private Context context;
public ChecklisteAdapter(Context context, List<ChecklistenPunkt> objects) {
super(context, 0);
list = objects;
sparseArray = new SparseArray<ChecklistenPunktStatus>();
for (int i = 0; i < list.size(); i++) {
sparseArray.put(i, ChecklistenPunktStatus.NICHT_SELEKTIERT);
}
this.context = context;
}
#Override
public int getCount() {
return list.size();
}
#Override
public ChecklistenPunkt getItem(int position) {
return list.get(position);
}
#Override
public int getItemViewType(int position) {
ChecklistenPunkt p = (ChecklistenPunkt) list.get(position);
return p.getTyp();
}
#Override
public int getViewTypeCount() {
return 3;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v = null;
int type = getItemViewType(position);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final ViewHolder viewHolder = new ViewHolder();
switch (type) {
case Util.CHECKLISTE_FRAGE:
v = inflater.inflate(R.layout.pruefung_durchfuehren_list_item_frage, null);
viewHolder.nummer = (TextView) v.findViewById(R.id.check_nummer);
viewHolder.komponente = (TextView) v.findViewById(R.id.check_komponente);
viewHolder.funktion = (TextView) v.findViewById(R.id.check_funktion);
viewHolder.kriterium = (TextView) v.findViewById(R.id.check_kriterium);
viewHolder.erlaeuterung = (TextView) v.findViewById(R.id.check_erlaeuterung);
viewHolder.io = (Button) v.findViewById(R.id.button_i_o);
viewHolder.nio = (Button) v.findViewById(R.id.button_n_i_o);
viewHolder.io.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
viewHolder.io.setBackgroundColor(Color.GREEN);
viewHolder.io.setEnabled(false);
viewHolder.nio.setBackgroundColor(Color.GRAY);
viewHolder.nio.setEnabled(true);
sparseArray.put(position, ChecklistenPunktStatus.IN_ORDNUNG);
}
});
viewHolder.nio.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
viewHolder.nio.setBackgroundColor(Color.RED);
viewHolder.nio.setEnabled(false);
viewHolder.io.setBackgroundColor(Color.GRAY);
viewHolder.io.setEnabled(true);
sparseArray.put(position, ChecklistenPunktStatus.NICHT_IN_ORDNUNG);
}
});
ChecklistenPunktStatus status = sparseArray.get(position);
if (status == ChecklistenPunktStatus.IN_ORDNUNG) {
viewHolder.io.setBackgroundColor(Color.GREEN);
viewHolder.io.setEnabled(false);
viewHolder.nio.setBackgroundColor(Color.GRAY);
viewHolder.nio.setEnabled(true);
} else if (status == ChecklistenPunktStatus.NICHT_IN_ORDNUNG) {
viewHolder.nio.setBackgroundColor(Color.RED);
viewHolder.nio.setEnabled(false);
viewHolder.io.setBackgroundColor(Color.GRAY);
viewHolder.io.setEnabled(true);
} else if (status == ChecklistenPunktStatus.NICHT_SELEKTIERT) {
viewHolder.nio.setBackgroundColor(Color.parseColor("#cccccc"));
viewHolder.nio.setEnabled(true);
viewHolder.io.setBackgroundColor(Color.parseColor("#cccccc"));
viewHolder.io.setEnabled(true);
}
v.setTag(viewHolder);
break;
case Util.CHECKLISTE_UEBERSCHRIFT:
v = inflater.inflate(R.layout.pruefung_durchfuehren_list_item_head, null);
viewHolder.headline = (TextView) v.findViewById(R.id.check_headline);
viewHolder.headnummer = (TextView) v.findViewById(R.id.check_headnummer);
v.setTag(viewHolder);
break;
case Util.CHECKLISTE_ZUSATZTEXT:
v = inflater.inflate(R.layout.pruefung_durchfuehren_list_item_text, null);
viewHolder.text = (TextView) v.findViewById(R.id.check_text);
viewHolder.textnummer = (TextView) v.findViewById(R.id.check_textnummer);
v.setTag(viewHolder);
break;
default:
throw new IllegalArgumentException("ViewType " + type + " unbekannt!");
}
} else {
v = convertView;
}
ChecklistenPunkt clp = getItem(position);
ViewHolder viewHolder = (ViewHolder) v.getTag();
if (clp != null) {
switch (type) {
case Util.CHECKLISTE_FRAGE: {
viewHolder.nummer.setText(clp.getNummer());
viewHolder.komponente.setText(clp.getKomponente());
viewHolder.funktion.setText(clp.getFunktion());
viewHolder.kriterium.setText(clp.getKriterium());
viewHolder.erlaeuterung.setText(clp.getErlaeuterung());
ChecklistenPunktStatus status = sparseArray.get(position);
if (status == ChecklistenPunktStatus.IN_ORDNUNG) {
viewHolder.io.setBackgroundColor(Color.GREEN);
viewHolder.io.setEnabled(false);
viewHolder.nio.setBackgroundColor(Color.GRAY);
viewHolder.nio.setEnabled(true);
} else if (status == ChecklistenPunktStatus.NICHT_IN_ORDNUNG) {
viewHolder.nio.setBackgroundColor(Color.RED);
viewHolder.nio.setEnabled(false);
viewHolder.io.setBackgroundColor(Color.GRAY);
viewHolder.io.setEnabled(true);
} else if (status == ChecklistenPunktStatus.NICHT_SELEKTIERT) {
viewHolder.nio.setBackgroundColor(Color.parseColor("#cccccc"));
viewHolder.nio.setEnabled(true);
viewHolder.io.setBackgroundColor(Color.parseColor("#cccccc"));
viewHolder.io.setEnabled(true);
}
break;
}
case Util.CHECKLISTE_UEBERSCHRIFT: {
viewHolder.headline.setText(clp.getHeadline());
viewHolder.headnummer.setText(clp.getNummer());
break;
}
case Util.CHECKLISTE_ZUSATZTEXT: {
viewHolder.text.setText(Html.fromHtml(clp.getZusatz()));
viewHolder.textnummer.setText(clp.getNummer());
break;
}
default:
throw new IllegalArgumentException("ViewType " + type + " unbekannt!");
}
}
return v;
}
public SparseArray<ChecklistenPunktStatus> getSparseArray() {
return sparseArray;
}
static class ViewHolder {
protected TextView nummer;
protected TextView komponente;
protected TextView kriterium;
protected TextView funktion;
protected TextView erlaeuterung;
protected TextView text;
protected TextView textnummer;
protected TextView headline;
protected TextView headnummer;
protected Button io;
protected Button nio;
}
}
EDIT:
When the convertView is null, I create a new View by inflating my layout. Then I create a new ViewHolder, find the different TextViews and Buttons according to the Views viewType and put them into the ViewHolder. After that, I set this ViewHolder as the tag of the View. I distinguish according to the viewType again and set the text and Listeners of the Views inside the ViewHolder. Then I return the view.
When the convertView is not null I reuse the View. I get the ViewHolder with the getTag() method, set the text and Button state according to the model class and return the view at the end of the method.
My problem is that the answering state is correctly saved in the SparseArray, but it is not displayed correctly. When I answer a question in the first rows of my list and then scroll down, answered questions appear at the end of the list. By scrolling up and down I can mess up the answering states completely. But while this is happening, the states in the sparseArray are always correct.
Am I missing something here?
I suppose there is something wrong with the code. When you use the same cell and when the view is not null you should reuse the same holder and not the convertView. Something like that:
ViewHolder viewHolder = new ViewHolder();
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
.
.
.
}
else {
viewHolder = convertView.getTag();
}

ListView Viewholder checkbox state

I've some problems with my ListView custom adapter (and its newly implemented viewHolder). I have a ListView with a checkbox for each item (nothing new here). The problem is, if there is more than 9 items in my list, when I check the first checkbox, the tenth will be automatically checked (same for the second with the eleventh) just like if there were one listener for both item (and I beleive it's the case in some way).
I read about the position issue with listView, view recycling and the ViewHolder way to solve it here: How can I make my ArrayAdapter follow the ViewHolder pattern?
But I probably made something wrong because it's not working...
public class PresenceListAdapter extends SimpleAdapter {
private LayoutInflater inflater;
private List<Integer> ids;
private List<String> statuts;
public PresenceListAdapter (Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to, List<Integer> ids, List<String> statuts)
{
super (context, data, resource, from, to);
inflater = LayoutInflater.from (context);
this.ids = ids;
this.statuts= statuts;
}
#Override
public Object getItem (int position)
{
return super.getItem (position);
}
#Override
public View getView (int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
if (convertView == null)
{
convertView = inflater.inflate (R.layout.list_text_checkbox, null);
holder = new ViewHolder();
holder.btn = (Button) convertView.findViewById(R.id.btnRetard);
holder.check = (CheckBox) convertView.findViewById(R.id.checkPresent);
if (statuts.get(position).equals("P")) {
Drawable img = inflater.getContext().getResources().getDrawable(android.R.drawable.presence_online);
holder.btn.setCompoundDrawablesWithIntrinsicBounds( img, null, null, null );
holder.btn.setEnabled(true);
holder.check.setChecked(true);
}
else if(statuts.get(position).equals("R"))
{
Drawable img = inflater.getContext().getResources().getDrawable(android.R.drawable.presence_away);
holder.btn.setCompoundDrawablesWithIntrinsicBounds( img, null, null, null );
holder.btn.setEnabled(true);
holder.check.setChecked(true);
}
else
{
Drawable img = inflater.getContext().getResources().getDrawable(android.R.drawable.presence_invisible);
holder.btn.setCompoundDrawablesWithIntrinsicBounds( img, null, null, null );
holder.check.setChecked(false);
}
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
int id = ids.get(position);
if(id != 0)
{
holder.check.setTag(id);
holder.btn.setTag(id);
}
return super.getView (position, convertView, parent);
}
static class ViewHolder {
Button btn;
CheckBox check;
}
And my listener:
public void changerPresent(View v) {
CheckBox checkPresent = (CheckBox) v;
int idPersonne = (Integer) checkPresent.getTag();
View parent = (View)v.getParent();
Button btn = (Button) parent.findViewById(R.id.btnRetard);
if(checkPresent.isChecked()) {
gestion.updatePresence(idPersonne, idSeance, "P");
btn.setEnabled(true);
setBtnRetardPresent(btn);
}
else
{
gestion.updatePresence(idPersonne, idSeance, "A");
btn.setEnabled(false);
setBtnRetardAbsent(btn);
}
}
I would appreciate any help at this point, I'm working on this for hours now.
Thank you very much.
Here's how I made it work:
First, you need a separate array for your checked state. It has to be the same size as your adapter's getCount().
Then on your getView, your checkbox's setOnCheckedChangedListener MUST PRECEED your checkbox.setChecked statements.
example:
holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
isChecked[position] = isChecked;
}
});
holder.checkBox.setChecked(isChecked[position]);
You should set CheckedBox state outside the initialization of ViewHolder, like the following code:
if (convertView == null) {
viewHolder = new ViewHolder();
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.checkedBox.setChecked();
BTW: use SparseBooleanArray instead of two list to store CheckedBox state.
The problem is because of the fact that listview recycles it views
so in getView() method
if (convertView == null)
{
........
}
else
{
holder = (ViewHolder) convertView.getTag();
}
// Uncheck needed boxes here... You need to implement your logic
if( 'position' is checked earlier)
holder.check.setChecked(true);
else
holder.check.setChecked(false);
You need to write the code to manage the state of view if the convert is not null, because it is a already used view which may be having checked check boxes.

Change ListView background - strange behaviour

I have a problem with changing the background of a view in a ListView.
What I need:
Change the background image of a row onClick()
What actually happens:
The background gets changed (selected) after pressing e.g. the first entry. But after scrolling down the 8th entry is selected too.
Scroll back to the top the first isn't selected anymore. The second entry is selected now.
Continue scrolling and it continues jumping...
What i'm dong in the Code:
I have channels, and onClick() I toggle an attribute of channel boolean selected
and then I change the background.
I'm doing this only onClick() thats why I don't get why it's actuelly happening on other entries too.
One thing I notices is: It seems to be only the "drawing"-part because the item which get selected "by it self" has still the selected value on false
I think it seems to have something to do with the reuse of the views in the custom ListAdapters getView(...)
Code of onClick() in ListActivity:
#Override
protected ViewHolder createHolder(View v) {
// createHolder will be called only as long, as the ListView is not
// filled
TextView title = (TextView) v
.findViewById(R.id.tv_title_channel_list_adapter);
TextView content = (TextView) v
.findViewById(R.id.tv_content_channel_list_adapter);
ImageView icon = (ImageView) v
.findViewById(R.id.icon_channel_list_adapter);
if (title == null || content == null || icon == null) {
Log.e("ERROR on findViewById",
"Couldn't find Title, Content or Icon");
}
ViewHolder mvh = new MyViewHolder(title, content, icon);
// We make the views become clickable
// so, it is not necessary to use the android:clickable attribute in
// XML
v.setOnClickListener(new ChannelListAdapter.OnClickListener(mvh) {
public void onClick(View v, ViewHolder viewHolder) {
// we toggle the enabled state and also switch the the
// background
MyViewHolder mvh = (MyViewHolder) viewHolder;
Channel ch = (Channel) mvh.data;
ch.setSelected(!ch.getSelected()); // toggle
if (ch.getSelected()) {
v.setBackgroundResource(R.drawable.row_blue_selected);
} else {
v.setBackgroundResource(R.drawable.row_blue);
}
// TESTING
Log.d("onClick() Channel", "onClick() Channel: "
+ ch.getTitle() + " selected: " + ch.getSelected());
}
});
return mvh;
}
Code of getView(...):
#Override
public View getView(int position, View view, ViewGroup parent) {
ViewHolder holder;
// When view is not null, we can reuse it directly, there is no need
// to reinflate it.
// We only inflate a new View when the view supplied by ListView is
// null.
if (view == null) {
view = mInflater.inflate(mViewId, null);
// call own implementation
holder = createHolder(view);
// TEST
// we set the holder as tag
view.setTag(holder);
} else {
// get holder back...much faster than inflate
holder = (ViewHolder) view.getTag();
}
// we must update the object's reference
holder.data = getItem(position);
// <EDIT SOLUTION>
if(getItem(position).get_id() == channelList.get(position).get_id()){
if(getItem(position).getSelected())
{
view.setBackgroundResource(R.drawable.row_blue_selected);
}
else{
view.setBackgroundResource(R.drawable.row_blue);
}
}
// </EDIT SOLUTION>
// call the own implementation
bindHolder(holder);
return view;
}
I really would appreciate any idea how to solve this! :)
If more information is needed please tell me.
Thanks in advance!
Let me show you the code that I use for every ListView and properly controlling the click event for changing the background and doing anything further
public class Offices extends Activity {
private ListView listView;
/* selectedListItem will contain the number of items to be selected.
* Your list item OnOlickListener will simply change this variable
* to the position of the clicked item. The Adapter will do the rest
* because you need to refresh the ListView.
*/
private int selectedListItem = -1;
private Handler mHandler = new Handler();
private Vector<String> data;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.officeslayout);
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");
data.add("Seven");
data.add("Eight");
data.add("Nine");
data.add("Ten");
listView = (ListView)findViewById(R.id.ListView01);
listView.setDivider(null);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
selectedListItem = position;
((EfficientAdapter)listView.getAdapter()).notifyDataSetChanged();
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 class EfficientAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public EfficientAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
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.officeslistitemlayout, null);
holder = new ViewHolder();
holder.backgroundView = (ImageView) convertView
.findViewById(R.id.OfficesBackground);
holder.officesTitle = (TextView) convertView
.findViewById(R.id.OfficesName);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if(position == selectedListItem) {
holder.backgroundView.setBackgroundResource(R.drawable.and_gray_bg_listing_selected);
} else {
holder.backgroundView.setBackgroundResource(R.drawable.and_gray_bg_listing);
}
holder.officesTitle.setText(data.get(position));
return convertView;
}
}
static class ViewHolder {
TextView officesTitle;
ImageView backgroundView;
}
}
officeslistitemlayout.xml file will be like following add drawable and design it according to you put the following code in RelativeLayout
<ImageView android:id="#+id/OfficesBackground" android:layout_width="fill_parent"
android:layout_height="45dip"
android:layout_alignParentTop="true"
android:background="#drawable/and_gray_bg_listing"
android:scaleType="fitXY"
></ImageView>
<TextView android:id="#+id/OfficesName" android:layout_width="wrap_content"
android:text="Offices Name"
android:textColor="#000000" android:textStyle="bold"
android:layout_height="wrap_content"
android:layout_centerVertical="true" android:layout_marginLeft="5dip"
></TextView>
Hope it will help :)

Categories

Resources