【问题标题】:Android Listview Text AlignmentAndroid Listview 文本对齐
【发布时间】:2011-12-17 07:53:02
【问题描述】:

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

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

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

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

这是视图所基于的类:

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>

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

【问题讨论】:

    标签: java android xml listview layout


    【解决方案1】:

    看起来不错。可能是田地里有空格吗?尝试通过修剪去掉标题,或运行调试器以查看它是否不是您的代码,而是数据。

    item.getTitle().trim();
    

    【讨论】:

      【解决方案2】:

      Sqefv 的回答很好,不过我也建议您查看位于工具目录(对我来说是 C:\AndroidSDK\tools)的 android SDK 中的层次结构查看器工具。

      您可以使用它来检查布局是否正确,以及文本视图是否正确排列。但是,由于您使用的是 android.R.layout.simple_list_item_2,我认为您最好尝试 trim() 项目的标题,如果这不起作用,请尝试层次结构查看器。

      【讨论】:

        猜你喜欢
        • 2016-02-20
        • 2011-12-26
        • 2021-05-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-19
        相关资源
        最近更新 更多