【发布时间】:2014-06-17 17:17:54
【问题描述】:
刚接触 Android 编程,只是闲逛。
我已经构建了一个用于填充帖子的 ListView。我已成功加载 JSON,并且可以使用标题、帖子内容和时间戳填充我的自定义布局。
问题是我不知道如何添加标签(类似于下面 Zite 应用程序的屏幕截图,例如“Bosnia”、“Visualization”、“Security”)。我最多有三个要添加的标签(位置、学校、朋友),所以我在布局中放置了三个按钮。当然,我可以使用传入的 JSON 更改按钮文本,但并非每个项目都具有所有三个标签(例如,一个项目只有位置,而另一个有位置和学校)。所以我希望按钮只有在我有文本时才会出现,我会利用重力迫使它们离开。
我的第一个想法是嵌套列表视图。互联网告诉我这很糟糕。尽管我进行了搜索,但我没有找到任何教程或建议。 但这感觉像是一个已知问题,有什么学习资源的建议吗?
下面是我的相关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;
}
【问题讨论】:
-
没有不同的布局,并且根据数据类型你膨胀适当的布局
-
检查这可能会有所帮助stackoverflow.com/questions/18868194/…
标签: android xml json listview tags