2014-10-05 67 views
0

我的listview旨在直接從JSONArray中添加結果。有什麼辦法可以讓它只添加那些不存在於listview中的項目嗎?我正在使用自定義適配器。用新的JSON對象刷新listview?

getTweets的AsyncTask

public class getTweets extends AsyncTask<Void, Void, Void>{ 

    int counter; 

    @Override 
    protected Void doInBackground(Void... params) { 
     BufferedReader reader =null; 
     // TODO Auto-generated method stub 
     try{ 
      HttpClient hc = new DefaultHttpClient(); 
      HttpGet get = new HttpGet("https://api.twitter.com/1.1/statuses/home_timeline.json?count=40"); 
      ParseTwitterUtils.getTwitter().signRequest(get); 
      HttpResponse rp = hc.execute(get); 
      String result2 = EntityUtils.toString(rp.getEntity()); 
      JSONArray array = new JSONArray(result2); 
      for(int i=0; i < array.length(); i++){ 
       JSONObject session = array.getJSONObject(i); 
       JSONObject profilePic = session.getJSONObject("user"); 
       addTweet(profilePic.getString("name"),profilePic.getString("screen_name"),session.getString("text"),profilePic.getString("profile_image_url")); 
      } 
      }catch (Exception e) { 
      Log.e("TwitterFeedActivity", "Error loading JSON", e); 

} 
     return null; 

    } 
    @Override 
    protected void onPostExecute(Void result) { 
     // TODO Auto-generated method stub 
     tweetListView.setAdapter(tweetAdapter); 
     return; 
    } 


} 
public void addTweet(String user,String mentionname,String tweet,String profile_picture){ 
    tweetAdapter.add(new TweetList(user,mentionname, tweet,profile_picture)); 
} 

的ListView適配器

public class TweetArrayAdapter extends ArrayAdapter<Object> { 

    private TextView tweet,twitterUser,twitterMention; 
    ImageView profile_picture; 
    private List<TweetList> tweetList = new ArrayList<TweetList>(); 


    public void add(TweetList object) { 
     tweetList.add(object); 
     super.add(object); 
    } 

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

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

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

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

      LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      row = inflater.inflate(R.layout.tweet_list_item, parent, false); 
      twitterUser = (TextView)row.findViewById(R.id.display_name); 
      tweet = (TextView) row.findViewById(R.id.display_tweet); 
      twitterMention = (TextView)row.findViewById(R.id.display_twitter_mentionname); 
      profile_picture = (ImageView)row.findViewById(R.id.profile_picture); 

      TweetList tweetMessageObj = getItem(position); 
      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); 
      } 
      tweet.setText(hashtag, BufferType.SPANNABLE); 
      tweet.setMovementMethod(LinkMovementMethod.getInstance()); 
      tweet.setHighlightColor(Color.TRANSPARENT); 
      Typeface tf = Typeface.createFromAsset(getActivity().getAssets(), "fonts/light.ttf"); 
      tweet.setTypeface(tf); 
      Typeface tf2 = Typeface.createFromAsset(getActivity().getAssets(), "fonts/bold.ttf"); 
      twitterUser.setText(tweetMessageObj.twittername); 
      twitterUser.setTypeface(tf2); 
      twitterMention.setText("@" + tweetMessageObj.mentionname); 
      Picasso.with(getActivity()).load(tweetMessageObj.pictureURL).resize(400,400).into(profile_picture); 
     return row; 
    } 


} 

TweetsList

public class TweetList { 
    public String twittername,mentionname,tweet,pictureURL; 



    public TweetList(String twittername,String mentionname,String tweet,String pictureURL) { 
     super(); 
     this.twittername = twittername; 
     this.tweet = tweet; 
     this.pictureURL = pictureURL; 
     this.mentionname = mentionname; 


    } 

} 

回答

1

瓦時在我能想到的就是讓另一個ArrayList中,

ArrayList<String> mArrayList = new ArrayList<String>(); 

,然後添加,並同時將其過濾,

for (int i = 0; i < array.length(); i++) { 
     JSONObject session = array.getJSONObject(i); 
     JSONObject profilePic = session.getJSONObject("user"); 
     if (mArrayList.contains(profilePic.getString("name"))) { 
      mArrayList.add(profilePic.getString("name")); 
      addTweet(profilePic.getString("name"), 
        profilePic.getString("screen_name"), 
        session.getString("text"), 
        profilePic.getString("profile_image_url")); 
     } 
    } 

您可以過濾你在這些線路所需的任何字符串參數,

  if (mArrayList.contains(profilePic.getString("name"))) 
      mArrayList.add(profilePic.getString("name")); 
+0

效果很好,但由於某種原因,它將新增加到底部。任何線索? – Rhynoboy2009 2014-10-05 08:43:34

+0

你想在哪裏添加新的?或者,如果您不想要以前的數據,則清除適配器和數組列表上的onpreExcute方法的異步類。 – Chitrang 2014-10-05 08:50:30

+0

我希望新數據加載到列表視圖的最頂部,同時保留以前的數據。 – Rhynoboy2009 2014-10-05 08:52:09