向ListView添加可变数量的标签

时间:2014-06-17 17:17:55

标签: android xml json listview tags

全新的Android编程,只是四处乱逛。

我已经构建了一个填充帖子的ListView。我已成功加载了JSON,可以使用标题,帖子内容和时间戳填充我的自定义布局。

问题是我无法弄清楚如何添加标签(类似于Zite应用程序的屏幕截图,例如“波斯尼亚”,“可视化”,“安全性”)。我最多有三个标签要添加(位置,学校,朋友),所以我在布局中放了三个按钮。我当然可以使用传入的JSON更改按钮文本,但并非每个项目都包含所有三个标记(例如,一个项目只有位置,而另一个项目只有位置和学校)。所以我希望按钮仅在我有文字时出现,我会用重力迫使它们离开。

我的第一个想法是嵌套的listview。互联网告诉我这很糟糕。尽管我搜索,但我没有找到任何教程或建议。 但这感觉就像一个已知的问题,对学习资源的任何建议?

以下是我的相关XML和java。

   

<TextView
    android:id="@+id/feed_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="25dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:textSize="20dp"
    android:text="@string/fTitle"
    />

<TextView
    android:id="@+id/feed_content"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="25dp"
    android:layout_marginRight="25dp"
    android:layout_marginTop="2dp"
    android:layout_below="@+id/feed_title"
    android:textSize="15dp"
    android:text="@string/fContent"
    />

<TextView
    android:id="@+id/feed_time"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="25dp"
    android:layout_marginTop="2dp"
    android:layout_below="@+id/feed_content"
    android:textSize="10dp"
    android:text="@string/fTime"/>

<ImageView
    android:id="@+id/feed_like"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="2dp"
    android:layout_marginRight="25dp"
    android:layout_below="@+id/feed_content"
    android:layout_alignParentRight="true"
    android:layout_alignBottom="@+id/feed_time"
    android:src="@drawable/ic_launcher"
    />

<ImageView
    android:id="@+id/feed_flag"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/feed_content"
    android:src="@drawable/ic_launcher"
    android:layout_alignBottom="@+id/feed_like"
    android:layout_toLeftOf="@+id/feed_like" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="left"
    android:layout_below="@+id/feed_time"
    >

    <Button
        android:id="@+id/feed_tag_Locale"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:text="@string/fTagLocal"
        android:textSize="15dp"
        />

    <Button
        android:id="@+id/feed_tag_Network"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/fTagNetwork"
        android:textSize="15dp"
        />

    <Button
        android:id="@+id/feed_tag_Friend"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/fTagFriend"
        android:textSize="15dp"
        />

    </LinearLayout>

public View getView(int i, View view, ViewGroup viewGroup) {
    ViewHolder holder;

    if(view == null) {
        view = mInflater.inflate(R.layout.row_feed, null);

        holder = new ViewHolder();
        holder.titleTextView = (TextView) view.findViewById(R.id.feed_title);
        holder.postTextView = (TextView) view.findViewById(R.id.feed_content);
        holder.timeTextView = (TextView) view.findViewById(R.id.feed_time);
        holder.localeButton = (Button) view.findViewById(R.id.feed_tag_Locale);
        holder.networkButton = (Button) view.findViewById(R.id.feed_tag_Network);
        holder.friendButton = (Button) view.findViewById(R.id.feed_tag_Friend);

        view.setTag(holder);
    } else {
        holder = (ViewHolder) view.getTag();
    }

    JSONObject jsonObject = (JSONObject) getItem(i);
    JSONObject subObject;
    JSONObject traitsObject;

    String postTitle = "";
    String postContent = "";
    String postTimestamp = "";
    String postLocale = "";
    String postNetwork = "";
    String postFriend = "";

    if (jsonObject.has("postID")) {
        //postTitle = jsonObject.optString("title");
        postTimestamp = jsonObject.optString("timePosted");
        try  {
            subObject = jsonObject.getJSONObject("Content");
            postTitle = subObject.optString("title");
            postContent = subObject.optString("content");

            traitsObject = jsonObject.getJSONObject("Poster");
            postLocale = traitsObject.optString("location");
            postNetwork = traitsObject.optString("network");
            postFriend = traitsObject.optString("friends");


        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    //if (jsonObject.has(""))

    holder.titleTextView.setText(postTitle);
    holder.postTextView.setText("I see you " + postContent);
    holder.timeTextView.setText(postTimestamp);
    holder.localeButton.setText(postLocale);
    holder.networkButton.setText(postNetwork);
    holder.friendButton.setText(postFriend);

    return view;

}

enter image description here

1 个答案:

答案 0 :(得分:0)

在getView结束时,请更改:

holder.localeButton.setText(postLocale);
holder.networkButton.setText(postNetwork);
holder.friendButton.setText(postFriend);

到此:

if(!postLocale.matches("")){
    holder.localeButton.setText(postLocale);
}
else{
    holder.localeButton.setVisibility(View.GONE);
}

if(!postNetwork.matches("")){
    holder.networkButton.setText(postNetwork);
}
else{
    holder.networkButton.setVisibility(View.GONE);
}

if(!postFriend.matches("")){
    holder.friendButton.setText(postFriend);
}
else{
    holder.friendButton.setVisibility(View.GONE);
}
相关问题