2014-10-09 146 views
1

我在這裏有一個代碼,允許我將項目添加到頂部。如果我有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

後TweetList代碼。 – 2014-10-09 12:58:47

+0

我編輯了帖子。 – Rhynoboy2009 2014-10-09 13:00:46

回答

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; 
    } 

} 
+0

這是一種工作,每當一個新項目被添加它取消所有他們。這是用這個方法添加項目的正確方法嗎? (intent.getLongExtra(「tweet_id」,1),intent.getStringExtra(「tweet_name」),intent.getStringExtra(「tweet_screenname」),intent.getStringExtra(「tweet」),intent.getStringExtra ( 「tweet_profilepicture」),intent.getStringExtra( 「tweetPicture」),假),0); – Rhynoboy2009 2014-10-09 13:26:04

+0

當我滾動列表時,也發生這種情況。 – Rhynoboy2009 2014-10-09 13:30:52

+0

默認isFavourite爲false,所以當你選中複選框時也會更新值,特別是tweetList項目的位置。 – 2014-10-09 13:32:30

1

isFavorited是,應該是裏面TweetList信息。 以這種方式在getView中,您可以更新CheckBox的值。

holder.favorite.setChecked(tweetMessageObj.isFavorited); 
相關問題