Android : Android: ListView - ListItem Button and Text Change Effecting Other ListItems

on Wednesday, October 29, 2014


Here is my list view adapter



public class MyArrayAdapter extends ArrayAdapter<MyDataModel> {

Context mContext;
LayoutInflater mInflater;
Set<Integer> complete;


public MyArrayAdapter(Context context, int resource, List<MyDataModel> activityList) {
super(context, resource, activityList);
mContext = context;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
complete = new HashSet<Integer>();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
final Holder holder;
if(view==null)
{
view = mInflater.inflate(R.layout.activity_list_item, parent, false);
holder = new Holder();
holder.heading= (TextView)view.findViewById(R.id.heading);
holder.quantity = (EditText)view.findViewById(R.id.quantity);
holder.category = (TextView)view.findViewById(R.id.category);
holder.time = (TextView)view.findViewById(R.id.time);
holder.circle = (ImageView)view.findViewById(R.id.circle);
view.setTag(holder);
}
else
{
holder = (Holder) view.getTag();
}
final int pos = position;
if(complete.contains(pos))
{
holder.circle.setImageResource(R.drawable.check_mark);
holder.heading.setPaintFlags(holder.heading.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
holder.category.setPaintFlags(holder.category.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
holder.time.setVisibility(View.GONE);
holder.circle.setFocusable(false);
holder.circle.setClickable(false);
return view;
}
else
{
try
{
final MyDataModel h = getItem(position);
/...get my required values from the model above..
holder.heading.setText(heading);
holder.category.setText(category);
holder.time.setText(time);
holder.circle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
NetworkCallAsync async = new NetworkCallAsync(param,h.getScheduledEventId().toString(),"Y",param,
holder.heading,holder.category,holder.quantity,holder.circle,holder.time, pos, holder.holderComplete);
async.execute();
}
});

}
catch(Exception e)
{
e.printStackTrace();
}
}

return view;
}



private static final class Holder
{
public ImageView circle;
public TextView heading;
public EditText quantity;
public TextView time;
public TextView category;
}

class NetworkCallAsync extends AsyncTask<String,String,String>
{
....
//all init variables here



public NetworkCallAsync(Integer p1, String p2,Stringp3, String p4,
TextView heading,TextView category, EditText quantity, ImageView circle, TextView time, final int pos)
{
..
...
.....
.....
this.heading = heading;
this.quantity = quantity;
this.circle = circle;
this.time = time;
this.category = category;
this.pos = pos;

}

@Override
protected String doInBackground(String... params) {

//do a network call here and after a sucess , below happens
.............
return "success";
}

@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if(result.equals("success"))
{
//make UI updates
circle.setImageResource(R.drawable.check_mark);
heading.setPaintFlags(heading.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
category.setPaintFlags(category.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
time.setVisibility(View.GONE);
complete.add(pos);
notifyDataSetChanged();
}
}

}
}


To just shorted it up for you reader, upon a click of an image view inside a list item (or when I check it), I am popping up an Async Task ( a really short network call) and if I get a succesful result, I make UI updateds for that particluar list item and strike off the text in the list item, please see my onPostExecute(). At that point i store the position in a HashSet and in getView(....) check if position is contained, and if contained then display that the particluar list item has already been checked. But the issue is , even other rows are getting effected, despite this. Is there any way else ? Please help me out.


0 comments:

Post a Comment