Reputation: 940
I am working on an RSS reader and I can't seem to figure out why me text is not lining up properly.
I have looked at posts similar in nature such as:
I just can't understand why mine won't work right.
Can anyone explain why my text is not lining up properly inside of my listview?
This is the class that the view is based on:
public class RSSReader extends Activity implements OnItemClickListener
{
public final String RSSFEEDOFCHOICE = "http://app.calvaryccm.com/mobile/android/v1/devos";
public final String tag = "RSSReader";
private RSSFeed feed = null;
private static final DateFormat PARSING_PATTERN =
new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
private static final DateFormat FORMATTING_PATTERN =
new SimpleDateFormat("EEEE, MMMM dd, yyyy");
/** Called when the activity is first created. */
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
// go get our feed!
feed = getFeed(RSSFEEDOFCHOICE);
// display UI
UpdateDisplay();
}
private RSSFeed getFeed(String urlToRssFeed)
{
try
{
// Setup the URL
URL url = new URL(urlToRssFeed);
// Create the factory
SAXParserFactory factory = SAXParserFactory.newInstance();
// create a parser
SAXParser parser = factory.newSAXParser();
// Create the reader (scanner)
XMLReader xmlreader = parser.getXMLReader();
// Instantiate our handler
RSSHandler theRssHandler = new RSSHandler();
// Assign our handler
xmlreader.setContentHandler(theRssHandler);
// Get our data via the url class
InputSource is = new InputSource(url.openStream());
// Perform the synchronous parse
xmlreader.parse(is);
// Get the results - should be a fully populated RSSFeed instance, or null on error
return theRssHandler.getFeed();
}
catch (Exception ee)
{
// If we have a problem, simply return null
System.out.println(ee.getMessage());
System.out.println(ee.getStackTrace());
System.out.println(ee.getCause());
return null;
}
}
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
menu.add(Menu.NONE, 0, 0, "Refresh");
Log.i(tag,"onCreateOptionsMenu");
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()) {
case 0:
Log.i(tag,"Set RSS Feed");
return true;
case 1:
Log.i(tag,"Refreshing RSS Feed");
return true;
}
return false;
}
private void UpdateDisplay()
{
ListView itemlist = (ListView) findViewById(R.id.itemlist);
//ArrayAdapter<RSSItem> adapter = new ArrayAdapter<RSSItem>(this,android.R.layout.simple_list_item_1,feed.getAllItems());
List<Map<String, String>> data = new ArrayList<Map<String, String>>();
for (RSSItem item : feed.getAllItems()) {
Map<String, String> datum = new HashMap<String, String>(2);
datum.put("title", item.getTitle());
String outputDate;
try {
Date date = PARSING_PATTERN.parse(item.getPubDate());
outputDate = FORMATTING_PATTERN.format(date);
} catch (ParseException e) {
outputDate = "Invalid date"; // Date parse error
}
datum.put("date", outputDate);
data.add(datum);
}
SimpleAdapter adapter = new SimpleAdapter(this, data,
android.R.layout.simple_list_item_2,
new String[] {"title", "date"},
new int[] {android.R.id.text1,
android.R.id.text2});
itemlist.setAdapter(adapter);
itemlist.setOnItemClickListener(this);
itemlist.setSelection(0);
}
@Override
public void onItemClick(AdapterView parent, View v, int position, long id)
{
//Log.i(tag,"item clicked! [" + feed.getItem(position).getTitle() + "]");
Intent itemintent = new Intent(this,ShowDescription.class);
Bundle b = new Bundle();
b.putString("title", feed.getItem(position).getTitle());
b.putString("description", feed.getItem(position).getDescription());
b.putString("link", feed.getItem(position).getLink());
b.putString("pubdate", feed.getItem(position).getPubDate());
b.putString("enclosure", feed.getItem(position).getEnclosure());
itemintent.putExtra("android.intent.extra.INTENT", b);
startActivity(itemintent);
}
}
This is my XML for the previous class:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/itemlist" android:layout_gravity="left"
/>
</LinearLayout>
This a visual of what is happening:
Upvotes: 1
Views: 2001
Reputation: 5522
Sqefv's answer is good, though I also recommend you take a look at the hierarchy viewer tool in the android SDK located in the tools directory (for me it's C:\AndroidSDK\tools).
You can use it to check that the layout is correct, and the text views are lined up properly. However, as you are using android.R.layout.simple_list_item_2, I think that you would be better trying to trim() the the title of the item, and if that doesn't work, try the hierarchy viewer.
Upvotes: 0
Reputation: 1966
Looks good. Could it be that the fields are coming in with spaces? Try pulling the title off with a trim, or running the debugger to see if it's not your code but rather the data.
item.getTitle().trim();
Upvotes: 3