我这里有一个代码,允许我将项目添加到顶部。如果我有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;
}
}
答案 0 :(得分:1)
isFavorited
是应该在TweetList
内的信息。
这样在getView
中,您可以更新CheckBox
。
holder.favorite.setChecked(tweetMessageObj.isFavorited);
答案 1 :(得分:1)
试试这种方式,希望这可以帮助您解决问题。
<强> TweetList.java 强>
public class TweetList {
public String twittername;
public String mentionname;
public String tweet;
public String pictureURL;
public String tweetPictureUrl;
public long status_id;
public boolean isFavourite;
public TweetList(long status_id,String twittername,String mentionname,String tweet,String pictureURL,boolean isFavourite) {
this.status_id = status_id;
this.twittername = twittername;
this.tweet = tweet;
this.pictureURL = pictureURL;
this.mentionname = mentionname;
this.isFavourite = isFavourite;
}
public TweetList(long status_id,String twittername,String mentionname,String tweet,String pictureURL,String tweetPictureUrl,boolean isFavourite) {
this.status_id = status_id;
this.twittername = twittername;
this.tweet = tweet;
this.pictureURL = pictureURL;
this.mentionname = mentionname;
this.tweetPictureUrl = tweetPictureUrl;
this.isFavourite = isFavourite;
}
}
<强> TweetArrayAdapter.java 强>
public class TweetArrayAdapter extends BaseAdapter {
private Context context;
public List<TweetList> tweetList;
public void add(TweetList object,int position) {
tweetList.add(position,object);
}
public TweetArrayAdapter(Context context) {
this.context=context;
tweetList = new ArrayList<TweetList>();
}
public int getCount() {
return this.tweetList.size();
}
public TweetList getItem(int position) {
return tweetList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
convertView = LayoutInflater.from(context).inflate(R.layout.tweet_list_item, null);
holder = new ViewHolder();
holder.twitterUser = (TextView)convertView.findViewById(R.id.display_name);
holder.tweet = (TextView) convertView.findViewById(R.id.display_tweet);
holder.twitterMention = (TextView)convertView.findViewById(R.id.display_twitter_mentionname);
holder.profile_picture = (ImageView)convertView.findViewById(R.id.profile_picture);
holder.tweet_picture = (ImageView)convertView.findViewById(R.id.tweet_image);
holder.padding = (View)convertView.findViewById(R.id.view1);
holder.favorite = (CheckBox)convertView.findViewById(R.id.favorite_button);
convertView.setTag(holder);
}else{
holder = (ViewHolder)convertView.getTag();
}
SpannableString hashtag = new SpannableString(tweetList.get(position).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) {
TextView tv = (TextView)widget;
String tags = tv.getText().subSequence(tv.getSelectionStart(),tv.getSelectionEnd()).toString();
Toast.makeText(context, 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) {
TextView tv = (TextView)widget;
String tags = tv.getText().subSequence(tv.getSelectionStart(),tv.getSelectionEnd()).toString();
Toast.makeText(context, 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, TextView.BufferType.SPANNABLE);
holder.tweet.setMovementMethod(LinkMovementMethod.getInstance());
holder.tweet.setHighlightColor(Color.TRANSPARENT);
if(tweetList.get(position).tweet.isEmpty()){
holder.tweet.setVisibility(View.GONE);
}
Typeface tf = Typeface.createFromAsset(context.getAssets(), "fonts/light.ttf");
holder.tweet.setTypeface(tf);
Typeface tf2 = Typeface.createFromAsset(context.getAssets(), "fonts/bold.ttf");
holder.twitterUser.setText(tweetList.get(position).twittername);
holder.twitterUser.setTypeface(tf2);
holder.twitterMention.setText("@" + tweetList.get(position).mentionname);
Picasso.with(context).load(tweetList.get(position).pictureURL).into(holder.profile_picture);
if(tweetList.get(position).tweetPictureUrl != null){
holder.tweet_picture.setVisibility(View.VISIBLE);
holder.padding.setVisibility(View.VISIBLE);
Picasso.with(context).load(tweetList.get(position).tweetPictureUrl).into(holder.tweet_picture);
}else{
holder.tweet_picture.setVisibility(View.GONE);
holder.padding.setVisibility(View.GONE);
}
holder.favorite.setChecked(tweetList.get(position).isFavourite);
return convertView;
}
static class ViewHolder{
TextView tweet,twitterUser,twitterMention;
ImageView profile_picture,tweet_picture;
CheckBox favorite;
View padding;
}
}