我有一个类Tweet,带有一个get Mentions方法 但我不知道如何将setText设置为ArrayList<>。 这是我的代码
public class Tweet {
public String body;
private long uid;
private String createAt;
private User user;
private String urlImageNews;
private static String url;
public ArrayList<User> mentions;
public ArrayList<User> getMentions()
{
return mentions;
}
public String getUrlImageNews()
{
return urlImageNews;
}
public String getBody() {
return body;
}
public long getUid() {
return uid;
}
public String getCreateAt() {
return createAt;
}
public User getUser() {
return user;
}
public String getUrl()
{
return url;
}
public String getRelativeTimeAgo() {
String twitterFormat = "EEE MMM dd HH:mm:ss ZZZZZ yyyy";
SimpleDateFormat sf = new SimpleDateFormat(twitterFormat, Locale.ENGLISH);
sf.setLenient(true);
String relativeDate = "";
try {
long dateMillis = sf.parse(getCreateAt()).getTime();
relativeDate = DateUtils.getRelativeTimeSpanString(dateMillis,
System.currentTimeMillis(), DateUtils.SECOND_IN_MILLIS).toString();
} catch (ParseException e) {
e.printStackTrace();
}
return relativeDate;
}
public static Tweet fromJSON(JSONObject jsonObject)
{
Tweet twt = new Tweet();
try {
twt.body = jsonObject.getString("text");
twt.uid = jsonObject.getLong("id");
twt.createAt = jsonObject.getString("created_at");
twt.user = User.fromJSON(jsonObject.getJSONObject("user"));
twt.mentions = LoadMentions(jsonObject);
JSONArray media = jsonObject.getJSONObject("entities").optJSONArray("media");
if (media != null) {
for (int i = 0; i < media.length(); i++) {
JSONObject a = media.getJSONObject(i);
if (a.getString("type").equals("photo")) {
twt.urlImageNews = a.getString("media_url_https");
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return twt;
}
private static ArrayList<User> LoadMentions(JSONObject object) throws JSONException{
if (!object.getJSONObject("entities").has("user_mentions")) {
return null;
}
ArrayList<User> mentions = new ArrayList<User>();
JSONArray mentionsJSON = object.getJSONObject("entities").getJSONArray("user_mentions");
for (int i = 0; i < mentionsJSON.length(); i++) {
User user = User.fromJSON(mentionsJSON.getJSONObject(i));
mentions.add(user);
}
return mentions;
}
public static ArrayList<Tweet> fromJSONArray(JSONArray jsonArray)
{
ArrayList<Tweet> tweets = new ArrayList<>();
for ( int i = 0 ; i < jsonArray.length(); i++)
{
try {
JSONObject tweetjson = jsonArray.getJSONObject(i);
Tweet tweet = Tweet.fromJSON(tweetjson);
if(tweet != null)
{
tweets.add(tweet);
}
} catch (JSONException e) {
e.printStackTrace();
continue;
}
}
return tweets;
}
}
这是我的MentionsAdapter,我想在其中设置提示数组
public class MentionsAdapter extends RecyclerView.Adapter<MentionsAdapter.ViewHolder> {
Context context;
View tweetView;
private List<Tweet> lTweets;
public MentionsAdapter(List<Tweet> tweets)
{
lTweets = tweets;
}
public void clear() {
lTweets.clear();
notifyDataSetChanged();
}
// Add a list of items
public void addAll(List<Tweet> list) {
lTweets.addAll(list);
notifyDataSetChanged();
}
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private ImageView ivProfile;
private TextView tvUsername;
private TextView tvMentions;
private TextView tvMentionsDate;
public ViewHolder(View view) {
super(view);
ivProfile = (ImageView) view.findViewById(R.id.ivAva);
tvUsername = (TextView) view.findViewById(R.id.tvName);
tvMentions = (TextView) view.findViewById(R.id.tvMentions);
tvMentionsDate = (TextView) view.findViewById(R.id.tvMentionsDate);
view.setOnClickListener(this);
}
@Override
public void onClick(View v) {
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
tweetView = inflater.inflate(R.layout.item_metions, parent, false);
ViewHolder viewHolder = new ViewHolder(tweetView);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Log.d("debug", "int:: " + position);
Tweet tweet = lTweets.get(position);
holder.tvMentionsDate.setText(tweet.getRelativeTimeAgo());
holder.tvUsername.setText(tweet.getUser().getName());
holder.tvMentions.setText(tweet)
holder.ivProfile.setImageResource(android.R.color.transparent);
Glide.with(context).load(tweet.getUser().getProfileImageUrl()).into(holder.ivProfile);
}
@Override
public int getItemCount() {
if(lTweets == null){
Log.d("debug", "null");
return 0;
}
else {
Log.d("debug", ":: " + lTweets.size());
return lTweets.size();
}
}
}
提前致谢。
答案 0 :(得分:0)
您可以通过以下方式获取推文中的数据
User user = tweet.getMentions();
这样你就可以从.same方式获取用户对象,你可以使用
从这里获取所有用户实例 user.getMethodName()
只需更新onBindViewHolder()中的上述代码。
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Log.d("debug", "int:: " + position);
Tweet tweet = lTweets.get(position);
User user = tweet.getMentions();
holder.tvMentionsDate.setText(tweet.getRelativeTimeAgo());
holder.tvUsername.setText(tweet.getUser().getName());
holder.tvMentions.setText(tweet)
holder.ivProfile.setImageResource(android.R.color.transparent);
Glide.with(context).load(tweet.getUser().getProfileImageUrl()).into(holder.ivProfile);
}
答案 1 :(得分:0)
如果我没有错,你想将数据添加到类型为Tweet的ArrayList中,这可以这样做
yourArrayList.add(new Tweet ("Parameters goes here");
希望这会有所帮助
答案 2 :(得分:0)
如果我理解正确你只想显示提及的数量。 你可以这样做:
wp_enqueue_style
答案 3 :(得分:0)
啊,我只是通过添加一些行来解决我的答案: twt.textMentions = jsonObject.getString(“text”); 无论如何,谢谢大家的答案