Brand new to Android programming and just dinking around.
I've built a ListView that is populating posts. I've successfully loaded a JSON and can populate my custom layout with title, post content, and timestamp.
The problem is I can't figure out how to add tags (akin to the screenshot below of Zite's app, eg "Bosnia", "Visualization", "Security"). I have up to three tags that I want to add (location, school, friend) so I put three buttons in the layout. I can change the button text with the incoming JSON of course, but not every item has all three tags (for instance one item has only location, while another has location & school). So I want buttons to only appear when I have text for them, and I'll use gravity to force them left.
My first thought was a nested listview. The internet told me that is bad. Despite my searching, I found no tutorials or advice. But this feels like a known problem, any suggestions for learning resources?
Below is my relevant XML and 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;
}
