Android - Issue with a ListView - java

As a french developper, I apologize about my English.
So, my aim is to create a phone book for my android application. This phone book is made of a ListView. I've populated my ListView with a custom adapter which allows the user to select some TextViews in each rows.
I presume a picture is better than thousand words, so here it is:
As you can see the red parts are the TextViews that I have selected.
The issue I have to face is the following:
When I select a TextView from a row, the row that is 4 places under has its TextView that gets selected too! Is that a common issue or is it due to my code?
I've added a log on the click listener, for each TextView I click, I receive only one log, so I don't think the problem comes from my work.
For example and if some of you don't understand what I said; I select the TextView with the drawableLeft picture in the second row. The logcat returns me the following entry:
"Select: 1" (as defined in my code).
If I scroll down my ListView, I can see that my second row (ie. my TextView) is selected as expected but also my 6th row, which is not expected at all!
Here is the code I use in order to color or not a row:
public View getView(final int position, View convertView, ViewGroup parent)
{
selectedLocations.add(new Boolean(false));
selectedAvailabilities.add(new Boolean(false));
if (convertView == null) convertView = inflater.inflate(R.layout.phonebook_adapter_layout, parent, false);
final LinearLayout root = (LinearLayout) convertView.findViewById(R.id.phonebook_adapter_root);
final ImageView photo = (ImageView) convertView.findViewById(R.id.phonebook_adapter_image);
final TextView firstname = (TextView) convertView.findViewById(R.id.phonebook_adapter_firstname);
final TextView lastname = (TextView) convertView.findViewById(R.id.phonebook_adapter_lastname);
final TextView location = (TextView) convertView.findViewById(R.id.phonebook_adapter_location);
final TextView availability = (TextView) convertView.findViewById(R.id.phonebook_adapter_availability);
Bitmap mBitmap = null;
try
{
mBitmap = Media.getBitmap(context.getContentResolver(), Uri.parse(relations.get(position).getPhoto()));
photo.setImageBitmap(mBitmap);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
firstname.setText(relations.get(position).getFirstName());
lastname.setText(relations.get(position).getLastName());
DBStatus dbStatus = new DBStatus(KramerApplication.getInstance());
Status status = dbStatus.getWithRelation(relations.get(position));
dbStatus.close();
if (status != null)
{
location.setText( status.getLocation() );
availability.setText( status.getAvailability() );
if ( status.getDisplayedAvailability(2).equals("Busy") )
availability.setCompoundDrawablesWithIntrinsicBounds(R.drawable.availability_busy, 0, 0, 0);
else if ( status.getDisplayedAvailability(2).equals("Occupied") )
availability.setCompoundDrawablesWithIntrinsicBounds(R.drawable.availability_busy, 0, 0, 0);
else if ( status.getDisplayedAvailability(2).equals("Free") )
availability.setCompoundDrawablesWithIntrinsicBounds(R.drawable.availability_on, 0, 0, 0);
else
availability.setCompoundDrawablesWithIntrinsicBounds(R.drawable.availability_off, 0, 0, 0);
}
root.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
Intent intent = new Intent(context, ContactDetailsActivity.class);
intent.putExtra("contact_id", relations.get(position).getId());
context.startActivity(intent);
}
});
location.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
if (selectedLocations.get(position).booleanValue())
{
selectedLocations.set(position, new Boolean(false));
location.setBackgroundColor(Color.TRANSPARENT);
}
else
{
selectedLocations.set(position, new Boolean(true));
location.setBackgroundColor(Color.RED);
}
}
});
availability.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
if (selectedAvailabilities.get(position).booleanValue())
{
selectedAvailabilities.set(position, new Boolean(false));
availability.setBackgroundColor(Color.TRANSPARENT);
}
else
{
selectedAvailabilities.set(position, new Boolean(true));
availability.setBackgroundColor(Color.RED);
}
}
});
return convertView;
}
The ArrayLists "selectedAvailabilities" and "selectedLocations" have been properly initialized in the constructor and do their jobs when I use them in another activity (Read only).
I hope you'll be able to help me.
Regards.
V.
################################# SOLUTION
If someone is looking for the solution, here it is. Many thanks to user936414!.
Replace (line 6):
if (convertView == null) convertView = inflater.inflate(R.layout.phonebook_adapter_layout, parent, false);
By:
convertView = inflater.inflate(R.layout.phonebook_adapter_layout, parent, false);
Before:
return convertView;
Add:
if (selectedAvailabilities.get(position).booleanValue())
{
availability.setBackgroundColor(Color.RED);
}
if (selectedLocations.get(position).booleanValue())
{
location.setBackgroundColor(Color.RED);
}

The reason for this behavior is the use of convertview. To solve this, have a HashSet and put all the positions selected in the HashSet. In getView check contains of Hashset and setSelected for the TextView. Hope this helps.
Add
if (selectedLocations.get(position).booleanValue())
{
location.setBackgroundColor(Color.RED);
}
else
{
location.setBackgroundColor(Color.TRANSPARENT);
}
outside the onClickListener also to ensure convertView does not affect your requirement.

Related

List.setAdapter(thadapter); not work always + screen errors

Hello I have a problwm witch Listview.
I use listview for show items in list.
I have activity where is Listview. And If I longpress on row in listview open popup (dialog) where I can edit word in row.
If edit finish I Call this
AddItemInPacage.obnovitem=true;
AddItemInPacage.indexitem=poz;
AddItemInPacage.novyitem=novypacage;
Boolean is for Timer if is true Start This method
public void ZmenItem(int indexzmeny,String nove) {
ListAdapter.clear();
for (int i=1;i<prvy.getPacageItem2(indexpacage)+1;i++) {
ListAdapter.add(String.valueOf(prvy.citajItem(indexpacage,i)));
}
ListAdapter.set(indexzmeny,nove);
Toast.makeText(this, "citam "+ ListAdapter.get(indexzmeny), Toast.LENGTH_SHORT).show();
thadapter = new MyThumbnailAdapter(getApplication(), R.layout.list_row, ListAdapter);
List.setAdapter(thadapter);
thadapter.notifyDataSetChanged();
}
Everything is OK when I while I not open the same row. If edited row for some times in row Program not work good.
Everything is good while this line List.setAdapter(thadapter); after thise line is Variables good too, but listview in display not rewrite.
Why?
I have three times this List.setAdapter(thadapter); on program but on OnCreate method and when Put new row in listview... So when Edited row I call only thise method ↑↑ which sometimes rewrite good and sometimes not.
Because Thise program isn't it 100% good :(
ListView List;
MyThumbnailAdapter thadapter = null;
ArrayList<String> ListAdapter = new ArrayList<String>();
This is my Adapter
public class MyThumbnailAdapter extends ArrayAdapter<String> {
ArrayList<String> arr;
private TextView text;
public MyThumbnailAdapter(Context context, int textViewResourceId, ArrayList<String> objects) {
super(context, textViewResourceId, objects);
this.arr = objects;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = null;
LayoutInflater inflater = getLayoutInflater();
view = inflater.inflate(R.layout.list_row, parent, false);
TextView textnumber = (TextView) view.findViewById(R.id.text);
ImageView delButton = (ImageView) view.findViewById(R.id.btn_del);
Typeface robtoLight = Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Light.ttf");
textnumber.setTypeface(robtoLight);
delButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
arr.remove(position);
thadapter.notifyDataSetChanged();
Toast.makeText(getContext(), "Item deleted", Toast.LENGTH_SHORT).show();
}
});
textnumber.setText(arr.get(position));
textnumber.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
for (int i=ListAdapter.size()-1;i>=0;i--) {
// for (int i = 0; i < ListAdapter.size() ; i++) {
try {
prvy.pridajItemRemove(ListAdapter.get(i), indexpacage, i+1);
} catch(IndexOutOfBoundsException e) {
prvy.Nulak(indexpacage,i);
}
}
prvy.setPacageItem2(ListAdapter.size(),indexpacage);
prvy.setPocItem(ListAdapter.size());
for (int i=ListAdapter.size();i<99;i++) {
prvy.Nulak(indexpacage,i+1);
}
Serializuj(prvy,nazovtripu);
EditItemDialog cdd=new EditItemDialog(AddItemInPacage.this,nazovtripu,position,indexpacage);
cdd.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
cdd.show();
return true;
}
});
return view;
}
}
I put picture
Sometimes if I edit row Toas read good string, but Listview no rewrite.
This may help you
ListAdapter.notifyDataSetChanged()
Hoping ListAdapter is name of your adapter
I need synchronized confirm edit in Dialog with medhod which rewrite Listview.
Or switch sequence from
AddItemInPacage.obnovitem=true;
AddItemInPacage.indexitem=poz;
AddItemInPacage.novyitem=novypacage;
to
AddItemInPacage.indexitem=poz;
AddItemInPacage.novyitem=novypacage;
AddItemInPacage.obnovitem=true;
Because If AddItemInPacage.obnovitem=true; first Activity work with old information in variable novypacage. And when variable novypacage is full from dialog. Listview finish work with old information in this variable.

programmatically added elements not appearing in fragment

When my fragment is created it calls
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View layout=inflater.inflate(R.layout.fragment_item_fragment1, container, false);
populatePage();
return layout;
}
and within that calls the populatepage() method. Within that method a call to a webservice is made. I have verified that the data exits and everything runs without error.
public void populatePage(){
String globalKey = "key";
String url = "";
//shared preferences
SharedPreferences settings = getActivity().getSharedPreferences(sharedPreferences, 0);
final String SESSION_KEY = settings.getString("SESSION_KEY", "nothing is here");
String FILE_KEY = settings.getString("FILE_KEY", "");
String page = settings.getString("PAGE", "Part");
final String group = settings.getString("FRAG_1", "");
String record = settings.getString("RECORD", "");
//BUILD THE URL WE NEED
url = "https://url.com/"
//loading page
//Declare the headers and add the pairs
Headers header = new Headers();
header.add("X-SESSION-KEY", SESSION_KEY);
header.add("X-GLOBAL-KEY", globalKey);
//Layout for building upon
final LinearLayout ll = new LinearLayout(getActivity());
ll.setOrientation(LinearLayout.VERTICAL);
$.ajax(new AjaxOptions().url(url)
.type("GET")
.dataType("json")
.headers(header)
.contentType("application/json")
.context(getActivity()).success(new Function() {
#Override
public void invoke($ droidQuery, Object... params) {
JSONObject json = (JSONObject) params[0];
try {
Boolean session = json.getBoolean("valid");
if (session) {
JSONArray recordsArray = json.getJSONArray("field");
String groupName = ""; //we compare group to this before adding it as a preference since we dont want dupes
for (int i = 0; i < recordsArray.length(); i++) {
JSONObject record = recordsArray.getJSONObject(i);
//BIG MESSY IF STATEMENT TO PARSE DATA! HOORAY!
if (record.getString("group").equals(group)) {
if (record.getString("valueType").equals("Text")) {
TextView label = new TextView(getActivity());
label.setText(record.getString("label"));
label.setPadding(20, 20, 20, 0);
label.setTextSize(20);
label.setTypeface(null, Typeface.BOLD);
label.setTextColor(Color.BLACK);
ll.addView(label);
TextView value = new TextView(getActivity());
value.setPadding(20, 0, 20, 20);
value.setTextSize(17);
value.setTextColor(Color.BLACK);
value.setText(record.getString("value"));
ll.addView(value);
} else if (record.getString("valueType").equals("Image")) {
TextView label = new TextView(getActivity());
label.setText(record.getString("label"));
label.setPadding(20, 20, 20, 20);
label.setTextSize(20);
label.setTypeface(null, Typeface.BOLD);
label.setTextColor(Color.BLACK);
ll.addView(label);
byte[] decodedString = Base64.decode(record.getString("value"), Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
ImageView image = new ImageView(getActivity());
image.setImageBitmap(decodedByte);
image.setScaleType(ImageView.ScaleType.FIT_START);
}
}
}
progressDialog.dismiss();
progressDialog.cancel();
} else {
droidQuery.toast("Session Expired", Toast.LENGTH_LONG);
Intent intent = new Intent(getActivity(), LoginActivity.class);
startActivity(intent);
}
} catch (JSONException e) {
droidQuery.toast(e.toString(), Toast.LENGTH_SHORT);
}
}
}
).
error(new Function() {
#Override
public void invoke($ droidQuery, Object... params) {
Log.e("$", "broke good");
//DELETE THIS
Context context = getActivity();
CharSequence text = "Could not connect to Server";
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
));
}
}
For some reason when the app launches the fragment is blank as if nothing is added and I can't figure out why. I've tried added the linearlayout to the fragments xml and grabbing it by it's ID but I get a null reference error even though I do just that in another fragment elsewhere in the program and it works.
It looks like you're correctly adding the view to the Linear Layout you've created, but I don't see a spot where you're adding that newly created Layout to the Fragment's View.
Here is the documentation and an example
Taken from the example:
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.your_layout, null);
// fill in any details dynamically here
TextView textView = (TextView) v.findViewById(R.id.a_text_view);
textView.setText("your text");
// insert into main view
ViewGroup insertPoint = (ViewGroup) findViewById(R.id.insert_point);
insertPoint.addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
Modified to your specific situation:
populatePage(layout);
public void populatePage(View v){
...other code here...
RelativeLayout relativeLayout = (RelativeLayout)v. findViewById(R.id.id_that_exists_in_your_xml);
relativeLayout.addView(ll, 0, new ViewGroup.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT));
}
You're right, regarding your most recent edit, I made a mistake. Get reference to the base layout object in your XML file (Relative/Linear/Frame layout etc), and then call the addView method on that.
From the example you gave the ll object just created but never added to fragment. You can return LinearLayout object from populatePage() function and add it to fragment view in onCreateView.

How to modify a single list item after Asynctask is done in a BaseAdapter to

In my main activity I display a ListView which uses a custom BaseAdapter (ThoughtListAdapter).
listView = (ListView) findViewById(R.id.list);
adapter = new ThoughtListAdapter(this, resultingThoughts);
listView.setAdapter(adapter);
Every item in the ListView has a custom layout containing a TextView and two Button.
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.list_item_thought, null);
}
thoughtText = (TextView) convertView.findViewById(R.id.thought_text_view);
likeButton = (Button) convertView.findViewById(R.id.thought_like_button);
dislikeButton = (Button) convertView.findViewById(R.id.thought_dislike_button);
When a Button is clicked an AsyncTask (AsyncPost) is called which connects to my database and makes some changes.
likeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
System.out.println("LIKE CLICKED");
Thought t = thoughtItems.get(position);
thoughtId = t.getId();
opinion = 1;
AsyncPost asyncPost = new AsyncPost(activity,ThoughtListAdapter.this);
asyncPost.execute(SHARE_THOUGHT_URL,
TAG_PERSON_EMAIL, "m#b.it",
TAG_THOUGHT_ID, thoughtId.toString(),
TAG_OPINION, opinion.toString());
}
});
What I need is making both Button-s of a list item disappear after the AsyncTask is done with a successful outcome. I have a method onComplete(JSONObject json) which elaborates the JSONObject returned by the AsyncTask. I try to make the buttons non visible inside the onComplete method, but this doesn't work because onComplete() doesn't know which exact button has been clicked.
How can I pass an instance of the exact clicked button inside onComplete() and make disappear only the Like and Dislike buttons of the concerned list item?
AsyncPost is a global AsyncTask used by all my other activities. I would strongly prefer to leave it alone. The onComplete() method functions as the onPostExecute() method of the AsyncTask.
Here are the getView() and onComplete() methods inside my BaseAdapter, which contain all the code shown above.
Thank you.
public View getView(final int position, View convertView, ViewGroup parent) {
if (layoutInflater == null) {
layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.list_item_thought, null);
}
thoughtText = (TextView) convertView.findViewById(R.id.thought_text_view);
likeButton = (Button) convertView.findViewById(R.id.thought_like_button);
dislikeButton = (Button) convertView.findViewById(R.id.thought_dislike_button);
//thoughtItems is a list of custom ojbects (Thought)
Thought t = thoughtItems.get(position);
//Here i set the content of the current TextView
thoughtText.setText(t.getText());
//the two buttons do basically the same thing when get clicked
likeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Thought t = thoughtItems.get(position);
thoughtId = t.getId();
opinion = 1;
AsyncPost asyncPost = new AsyncPost(activity,ThoughtListAdapter.this);
asyncPost.execute(SHARE_THOUGHT_URL,
TAG_PERSON_EMAIL, "m#b.it",
TAG_THOUGHT_ID, thoughtId.toString(),
TAG_OPINION, opinion.toString());
}
});
dislikeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Thought t = thoughtItems.get(position);
thoughtId = t.getId();
opinion = 0;
AsyncPost asyncPost = new AsyncPost(activity,ThoughtListAdapter.this);
asyncPost.execute(SHARE_THOUGHT_URL,
TAG_PERSON_EMAIL, "m#b.it",
TAG_THOUGHT_ID, thoughtId.toString(),
TAG_OPINION, opinion.toString());
}
});
return convertView;
}
#Override
public void onComplete(JSONObject json) {
if (json != null) {
try {
if (json.getInt(TAG_SUCCESS) == 0) {
Toast.makeText(activity, "Operazione non riuscita.", Toast.LENGTH_LONG).show();
} else {
//if everything is good i try to make the buttons of that particular list item disappear
likeButton.setVisibility(View.GONE);
dislikeButton.setVisibility(View.GONE);
}
}
catch (JSONException e) {
Log.e(TAG_LOG, "JSONException", e);
}
}
else Toast.makeText(activity, "Errore connessione!", Toast.LENGTH_LONG).show();
}
One solution to this would be to have something on your Thought object to indicate whether or not to show the buttons.
So in your getView() method you check this
likeButton = (Button) convertView.findViewById(R.id.thought_like_button);
dislikeButton = (Button) convertView.findViewById(R.id.thought_dislike_button);
Thought t = thoughtItems.get(position);
if (t.hideButtons()) {
likeButton.setVisibility(View.GONE);
dislikeButton.setVisibility(View.GONE);
}
else {
likeButton.setVisibility(View.VISIBLE);
dislikeButton.setVisibility(View.VISIBLE);
}
Then you would need to have your onComplete method return the id of the Thought object that it related to. Then inside your onComplete you could do
int id = //get your id from your JSON response
for(Thought t : thoughtItems) {
if (t.getId() == id) {
t.setHideButtons(true);
notifyDataSetChanged();
break;
}
}
By calling notifyDataSetChanged() it will redraw your list and when it does the check for whether it should show the buttons or not it will not show them because it was set on that thought item

how to get textview text

In my android app, I have a activity that contain a listview. And within the each row of the listview, i have another listview let called it lv. Inside this lv, it contain some textview. My question is how can i get the text from the listview (A)?
I know that if there is only one listview and in order to get the text of each row when i click on a button, i can use the following code
textview.getText().toString();
However, when i try with this code to get the text in lv, it always give me the last value (n row) in lv. I am not able to get the text of 1st row to n-1 row of lv.
UPDATE:
The follow are the code where i want to get the text, the ordered[] is passed from a custom adapter to another custom adapter (which is the following code)
public View getView(int position,View view,ViewGroup parent) {
LayoutInflater inflater=((Activity) context).getLayoutInflater();
View rowView=inflater.inflate(layoutResourceId, null,true);
txt1 = (TextView)rowView.findViewById(R.id.textView1);
txt2 = (TextView)rowView.findViewById(R.id.textView2);
txt3 = (TextView)rowView.findViewById(R.id.textView3);
txt4 = (TextView)rowView.findViewById(R.id.textView4);
txt5 = (TextView)rowView.findViewById(R.id.textView5);
txt6 = (TextView)rowView.findViewById(R.id.textView6);
txtoid = (TextView)rowView.findViewById(R.id.textView7);
final Button btnGet = (Button)rowView.findViewById(R.id.btnGet);
tmp = position;
String tmp2 = String.valueOf(ordered[position]);
txtoid.setText(tmp2);
btnGet.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(btnconfirm.getText().toString().equals("Get")){
String tmp1 = txtq.getText().toString();
System.out.println("tmp1: "+tmp1);
}
}
});
....
Anyone help will be nice!
Thanks
rowView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View rootView) {
// TODO Auto-generated method stub
TextView myText = (TextView)rootView.findViewById(R.id.textView7);
Log.d("","myText:" + myText.getText()); //you can get each row values.
}
});

ListView list is reversed when selection made

So I had my listview working perfectly then I decided to add a context menu. As soon as I did that whenever I normal clicked an item in my listview, the entire list gets inverted on the first click. Subsequent clicks do nothing to the order, but when the first item is de-selected again the list returns to normal. When I take out the context menu logic that I added, the list view problem does not go away.
I've attached a debugger and the elements in my list adapter are never reordered, and the ListView itself is never set to reverse with .setStackFromBottom()
Here is my onClick listener registered to handle the click events of the list view items:
public void onClick(View v) {
ViewHolder holder = (ViewHolder) v.getTag();
CheckBox b = holder.box;
Boolean check = b.isChecked();
b.setChecked(!check);
if (!check) {
mChecked.add(holder.fu);
if (mChecked.size() == 1) {
buttonLayout.setVisibility(View.VISIBLE);
}
} else {
mChecked.remove(holder.fu);
if (mChecked.size() == 0) {
buttonLayout.setVisibility(View.GONE);
}
}
}
The viewholder class just holds references to objects I use in the listview for optimizations. I cannot figure out why this is causing my list to invert when displayed, I've tried moving the listener to a different view in the layout, I've tried re-writing the listener, nothing seems to work! Any advice would be appreciated.
Edit: here is the code for the view holder
/** Class to provide a holder for ListViews. Used for optimization */
private class ViewHolder {
TextView date;
TextView gallons;
TextView cost;
TextView cpg;
TextView mpg;
CheckBox box;
FillUp fu;
}
as well as the adapter:
public class FillUpAdapter extends BaseAdapter {
ArrayList<FillUp> mElements;
ArrayList<FillUp> mChecked;
Context mContext;
public FillUpAdapter(Context c, ArrayList<FillUp> data) {
mContext = c;
mElements = data;
mChecked = new ArrayList<FillUp>();
}
public void clearChecked() {
mChecked.clear();
}
public ArrayList<FillUp> getChecked() {
return mChecked;
}
public boolean remove(FillUp f) {
mChecked.remove(f);
return mElements.remove(f);
}
#Override
public int getCount() {
return mElements.size();
}
#Override
public Object getItem(int arg0) {
return mElements.get(arg0);
}
#Override
public long getItemId(int arg0) {
return mElements.get(arg0).getId();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout layout;
ViewHolder holder;
if (convertView != null) {
layout = (LinearLayout) convertView;
holder = (ViewHolder) layout.getTag();
} else {
layout = (LinearLayout) LayoutInflater.from(mContext).inflate(
R.layout.fillup_list_item, parent, false);
holder = new ViewHolder();
holder.cost = (TextView) layout
.findViewById(R.id.fillUpListTotalValue);
holder.cpg = (TextView) layout
.findViewById(R.id.fillUpListCostPerGal);
holder.gallons = (TextView) layout
.findViewById(R.id.fillUpListGalValue);
holder.mpg = (TextView) layout
.findViewById(R.id.fillUpMPGText);
holder.date = (TextView) layout
.findViewById(R.id.fillUpListDate);
holder.box = (CheckBox) layout
.findViewById(R.id.fillUpListCheckBox);
holder.fu = (FillUp) getItem(position);
layout.setTag(holder);
}
holder.date.setText(holder.fu.getDate());
holder.gallons.setText(holder.fu.getGallonsText());
holder.cpg.setText(holder.fu.getCostText());
holder.cost.setText(holder.fu.getTotalText());
holder.mpg.setText(String.format("%03.1f MPG",holder.fu.getMPG()));
if (convertView != null) {
holder.box.setChecked(mChecked.contains(holder.fu));
}
layout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
ViewHolder holder = (ViewHolder) v.getTag();
CheckBox b = holder.box;
Boolean check = b.isChecked();
b.setChecked(!check);
if (!check) {
mChecked.add(holder.fu);
if (mChecked.size() == 1) {
buttonLayout.setVisibility(View.VISIBLE);
}
} else {
mChecked.remove(holder.fu);
if (mChecked.size() == 0) {
buttonLayout.setVisibility(View.GONE);
}
}
}
});
return layout;
}
}
UPDATE:
Ok, so I've narrowed it down to the visibility change on the buttonLayout view, which is a linear layout of buttons on the bottom of the Activity's layout, underneath the ListView. Whenever I change that view's visibility to View.VISIBLE (which happens when the first item is checked) the list's order is reversed. The order is restored when the view's visibility is set to View.GONE
I have no idea what would cause that though :(
After narrowing the scope a bit more, I discovered the problem was not the changing of the visibility of my button bar, but actually the passing around of FillUp objects in holder.fu of my ViewHolder class. By changing that to instead reference the adapter's getItem(position) method, everything seemed to work out. Quite an odd bug, since the adapter itself was not having the order of the elements changed, but passing around a reference to the object made it very unhappy.
If your listview background color changes when you click on it, I think it is about your theme. Just play with the cache color parameters of your listview, here is an example:
<ListView
android:layout_width="fill_parent"
android:id="#android:id/list"
android:scrollingCache="true"
android:persistentDrawingCache="all"
android:cacheColorHint="#0000"
android:layout_height="wrap_content"
android:fastScrollEnabled="true"
android:stackFromBottom="true"
android:smoothScrollbar="true"
android:paddingTop="115dip">
</ListView>

Categories

Resources