Android : Adding Items to top of listview with checkbox

on Thursday, October 9, 2014


I have a code here that allows me to add items to the top. If I had 5 items,the way it would work is like this.



0 New Item

1

2

3

4 First item


Then if something is new is added it becomes this.



0 New Item

1

2

3

4

5 First Item


As you can see,whenever a new item is added,it become position 0. This is a problem because if I set a checkbox on that's on position 0 and then a new item is added,the new item becomes position 0 which checks the new item and unchecks the item that was there before. I want it to work like this while still add items to the top.



4 New item

3

2

1

0 First Item


When a new item is added.



5 New Item

4

3

2

1

0 First Item


Here is my ListView Adapter.



public class TweetArrayAdapter extends ArrayAdapter<Object> implements OnClickListener {


TweetList tweetMessageObj;
ViewHolder holder = null;


public List<TweetList> tweetList = new ArrayList<TweetList>();

public void add(TweetList object,int position) {
tweetList.add(position,object);

}

public TweetArrayAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}

public int getCount() {
return this.tweetList.size();
}

public TweetList getItem(int position) {
return tweetList.get(position);
}

@SuppressLint("ViewHolder")
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
tweetMessageObj = getItem(position);

if(convertView == null){

LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.tweet_list_item, parent, false);
holder = new ViewHolder();
holder.twitterUser = (TextView)row.findViewById(R.id.display_name);
holder.tweet = (TextView) row.findViewById(R.id.display_tweet);
holder.twitterMention = (TextView)row.findViewById(R.id.display_twitter_mentionname);
holder.profile_picture = (ImageView)row.findViewById(R.id.profile_picture);
holder.tweet_picture = (ImageView)row.findViewById(R.id.tweet_image);
holder.padding = (View)row.findViewById(R.id.view1);
holder.favorite = (CheckBox)row.findViewById(R.id.favorite_button);
//holder.retweet = (CheckBox)row.findViewById(R.id.retweet_button);
//holder.reply = (CheckBox)row.findViewById(R.id.reply_button);

holder.favorite.setTag(holder);

row.setTag(holder);
}else{
holder = (ViewHolder)row.getTag();
}


SpannableString hashtag = new SpannableString(tweetMessageObj.tweet);
Matcher matcher = Pattern.compile("@([A-Za-z0-9_-]+)").matcher(hashtag);
Matcher matcher2 = Pattern.compile("#([A-Za-z0-9_-]+)").matcher(hashtag);
while (matcher.find())
{
hashtag.setSpan(new ForegroundColorSpan(Color.rgb(79, 120, 216)), matcher.start(), matcher.end(), 0);
hashtag.setSpan(new ClickableSpan() {

@Override
public void onClick(View widget) {
// TODO Auto-generated method stub
TextView tv = (TextView)widget;
String tags = tv.getText().subSequence(tv.getSelectionStart(),tv.getSelectionEnd()).toString();
Toast.makeText(getActivity(), tags, Toast.LENGTH_LONG).show();
}
public void updateDrawState(TextPaint ds) {// override updateDrawState
ds.setUnderlineText(false); // set to false to remove underline
}
},matcher.start(), matcher.end(), 0);
}
while (matcher2.find())
{ hashtag.setSpan(new ForegroundColorSpan(Color.rgb(79, 120, 216)), matcher2.start(), matcher2.end(), 0);
hashtag.setSpan(new ClickableSpan() {

@Override
public void onClick(View widget) {
// TODO Auto-generated method stub
TextView tv = (TextView)widget;
String tags = tv.getText().subSequence(tv.getSelectionStart(),tv.getSelectionEnd()).toString();
Toast.makeText(getActivity(), tags, Toast.LENGTH_LONG).show();
}
public void updateDrawState(TextPaint ds) {// override updateDrawState
ds.setUnderlineText(false); // set to false to remove underline
}
},matcher2.start(), matcher2.end(), 0);
}
holder.tweet.setText(hashtag, BufferType.SPANNABLE);
holder.tweet.setMovementMethod(LinkMovementMethod.getInstance());
holder.tweet.setHighlightColor(Color.TRANSPARENT);
if(tweetMessageObj.tweet.isEmpty()){
holder.tweet.setVisibility(View.GONE);
}
Typeface tf = Typeface.createFromAsset(getActivity().getAssets(), "fonts/light.ttf");
holder.tweet.setTypeface(tf);
Typeface tf2 = Typeface.createFromAsset(getActivity().getAssets(), "fonts/bold.ttf");
holder.twitterUser.setText(tweetMessageObj.twittername);
holder.twitterUser.setTypeface(tf2);
holder.twitterMention.setText("@" + tweetMessageObj.mentionname);

Picasso.with(getActivity()).load(tweetMessageObj.pictureURL).into(holder.profile_picture);
if(tweetMessageObj.tweetPictureUrl != null){
holder.tweet_picture.setVisibility(View.VISIBLE);
holder.padding.setVisibility(View.VISIBLE);
Picasso.with(getActivity()).load(tweetMessageObj.tweetPictureUrl).into(holder.tweet_picture);
}else{
holder.tweet_picture.setVisibility(View.GONE);
holder.padding.setVisibility(View.GONE);
}

return row;
}


}



public static class ViewHolder{
TextView tweet,twitterUser,twitterMention;
ImageView profile_picture,tweet_picture;
CheckBox reply,retweet,favorite;
View padding;
boolean isFavorited = false;
}

0 comments:

Post a Comment