java - Android Listview 文本对齐

标签 java android xml listview layout

我正在开发一个 RSS 阅读器,但我似乎无法弄清楚为什么我的文本没有正确排列。

我看过性质相似的帖子,例如:

我就是不明白为什么我的不能正常工作。

谁能解释为什么我的文本在我的 ListView 中没有正确排列?

这是 View 所基于的类:

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);

 }

这是我上一个类的 XML:

<?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>

这是正在发生的事情的视觉效果:

enter image description here

最佳答案

看起来不错。会不会是字段中有空格?尝试通过修剪去掉标题,或运行调试器以查看它是否不是您的代码而是数据。

item.getTitle().trim();

关于java - Android Listview 文本对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7930191/

相关文章:

java - 将 SharedPreferences 传递给 doInBackground()

XmlSlurper - 列出 xhtml 文档的文本和常规节点

java - 如何使用 Gson 在我的 POJO 类上使用动态 json 值?

android - 不幸的是,应用程序已停止 - 在 if-else 条件下显示 Toast

java - Kony FFI 整合问题

xml - XPath位置过滤无法按预期工作

javascript - 访问同名的不同节点

java - 如何使用java邮件API在邮件中发送多个信息

java - servlet 转发未通过模型后的 Spring 重定向

java - 使用 logback 通过自定义编码器限制消息大小