简体   繁体   English

Android,ListView,ArrayAdapter和格式文本

[英]Android, ListView, ArrayAdapter and formatting text

I'm trying to accomblish the following task: 我正在尝试完成以下任务:

  • A ListView, with textview rows 具有Textview行的ListView
  • When I click on an item, the text changes to italic 当我单击一个项目时,文本变为斜体

The change to italic can be something else - the point is to change the text in some way. 更改为斜体可以是其他内容-重点是以某种方式更改文本。

I thought to use Html.fromHtml, since that's pretty easy to deal with when formatting text. 我认为使用Html.fromHtml,因为格式化文本时很容易处理。 However, I cannot get it to work accross my design. 但是,我无法使其在整个设计中都能正常工作。

In my main activity, I create and access the ListView and ArrayAdapter like this: 在我的主要活动中,我这样创建和访问ListView和ArrayAdapter:

lv = (ListView) this.findViewById(R.id.itemslist);

I have a method for creating/updating the adapter, so that I can update the contents of the list: 我有一种用于创建/更新适配器的方法,以便可以更新列表的内容:

private void displayList () {
   adapter = new ItemArrayAdapter(getApplicationContext(), R.layout.item_listitem, items);
    lv.setAdapter(adapter);
    adapter.setNotifyOnChange(true);

}

My OnItemClickListener: 我的OnItemClickListener:

lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Toast.makeText(ShoppingListApp02Activity.this, "List item selected:" + 
        items.get(position).getId(), Toast.LENGTH_LONG).show();
    items.get(position).setName("<i>" + items.get(position).getName() + "</i>");
    displayList();
});

As you can see, on select/click of an item, I send the item's name surrounded by the html tags, to the Item class' setName() method. 如您所见,在选择/单击项目时,我将被html标记包围的项目名称发送到Item类的setName()方法。 The name (type String) of the item object is now "<i>name</i>" 项目对象的名称(字符串类型)现在为“ <i>名称</ i>”

The setName() method in the Item class: Item类中的setName()方法:

public void setName (String nname) { this.name = nname; }

Now, in the ArrayAdapter class (ItemArrayAdaper.java), the name is set in the TextView row of the ListView: 现在,在ArrayAdapter类(ItemArrayAdaper.java)中,在ListView的TextView行中设置名称:

itemName.setText(item.name);

If I leave the code above as it is, when clicking the item in the list, the name changes to this: "<i>name</i>. 如果按原样保留上面的代码,则单击列表中的项目时,名称将更改为:“ <i>名称</ i>”。

So far, it's as I expected. 到目前为止,这是我所期望的。

However, from there I need to convert the string "<i>name</i> to the html result, namely " name ". But when I try that like this: 但是,从那里我需要将字符串“ <i> name </ i>转换为html结果,即” name “。但是当我这样尝试时:

itemName.setText(Html.fromHtml(item.name).toString());

it simply skips the html tags and displays the name string normally. 它只是跳过html标签并正常显示名称字符串。

Why isn't the fromHtml working here? 为什么fromHtml在这里不起作用?

To compare, I tried just setting the contents of another TextView in my application (the footer) in the same way, with just: 为了进行比较,我尝试以相同的方式仅在我的应用程序(页脚)中设置另一个TextView的内容,即:

footerview.setText(Html.fromHtml("<i><font color='#123456'>text</font></i>"));

And that worked just fine. 而且效果很好。 But somewhere along the path, the tags surrounding the item name is lost. 但是沿着路径的某个地方,围绕物品名称的标签丢失了。 And that's strange, because in the ItemArrayAdapter class when I don't try to convert from html, the tags are there. 这很奇怪,因为在ItemArrayAdapter类中,当我不尝试从html转换时,标记在那里。 They just aren't honored when using the fromHtml method at the same place. 在同一位置使用fromHtml方法时,它们只是不被接受。

Any ideas? 有任何想法吗?

And, I know there's probably a better way to do this, and I'd be interested in that too :-) 而且,我知道可能有更好的方法可以做到这一点,对此我也很感兴趣:-)

How about a different approach? 如何使用另一种方法?

As you stated 如你所说

I'm trying to accomblish the following task: 我正在尝试完成以下任务:

  • A ListView, with textview rows. 一个具有textview行的ListView。

  • When I click on an item, the text changes to italic 当我单击一个项目时,文本变为斜体

Simply, initialize : 简单地,初始化:

lv = (ListView) this.findViewById(R.id.itemslist);
private void displayList () {
   adapter = new ItemArrayAdapter(getApplicationContext(), R.layout.item_listitem, items);
    lv.setAdapter(adapter);
    adapter.setNotifyOnChange(true);

}

And in your onItemClickListener() 并在您的onItemClickListener()中

lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Toast.makeText(ShoppingListApp02Activity.this, "List item selected:" + 
        items.get(position).getId(), Toast.LENGTH_LONG).show();

        ((TextView)view).setTypeface(null, Typeface.ITALIC);


});

Simple enough? 很简单?

Easy solution: Remove the toString(). 简单的解决方案:删除toString()。 I didn't try that because I had problems with it before, thinking that the data passed to a TextView HAS to be a pure string. 我没有尝试过,因为我以前遇到过问题,认为传递给TextView的数据是纯字符串。 However, it works like a charm without the toString() :-) 但是,它的工作原理就像没有toString()的魅力:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM