我在這裏有一個代碼,允許我將項目添加到頂部。如果我有5件物品,它的工作方式就是這樣。用複選框將項目添加到列表視圖頂部
0 New Item
1
2
3
4 First item
然後,如果有新東西被添加它就成了這個。
0 New Item
1
2
3
4
5 First Item
正如你所看到的,無論何時添加了一個新的項目,它成爲位置0。這是一個問題,因爲如果我設置複選框上的對位0,然後添加一個新的項目,新項目成爲位置0,它檢查新的項目並取消選中之前的項目。 我希望它能夠像這樣工作,同時仍然將項目添加到頂部。
4 New item
3
2
1
0 First Item
添加新項目時。
5 New Item
4
3
2
1
0 First Item
這是我的ListView適配器。
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;
}
TweetList
public class TweetList {
public String twittername,mentionname,tweet,pictureURL,tweetPictureUrl;
long status_id;
public TweetList(long status_id,String twittername,String mentionname,String tweet,String pictureURL) {
super();
this.status_id = status_id;
this.twittername = twittername;
this.tweet = tweet;
this.pictureURL = pictureURL;
this.mentionname = mentionname;
}
public TweetList(long status_id,String twittername,String mentionname,String tweet,String pictureURL,String tweetPictureUrl) {
super();
this.status_id = status_id;
this.twittername = twittername;
this.tweet = tweet;
this.pictureURL = pictureURL;
this.mentionname = mentionname;
this.tweetPictureUrl = tweetPictureUrl;
}
}
後TweetList代碼。 – 2014-10-09 12:58:47
我編輯了帖子。 – Rhynoboy2009 2014-10-09 13:00:46