将HTML添加到TextView

时间:2014-03-29 14:25:48

标签: java android eclipse listview

到目前为止,我有一个ListView,其中包含来自英国广播公司的新闻。在ListView中的每个项目上都有标题和新闻的小描述。但是,当我点击其中一个项目时,我会调整网址。我不希望这种情况发生,所以当点击一个项目时,我希望它带我到另一个布局,其中新闻显示标题为图像和新闻的主体。

这就是我所拥有的:

 public class Main extends Activity implements OnItemClickListener {

private class RowItem {
    String title = null;
    String description = null;
    String url = null;

    public RowItem(String title, String description, String url) {
        this.title = title;
        this.description = description;
        this.url = url;
    }
}

private class DownloadXMLTask extends AsyncTask<String, Void, String> {

    ArrayList<RowItem> items = null;

    public DownloadXMLTask(ArrayList<RowItem> items) {
        this.items = items;
    }

    // local helper method
    private String getNodeValue(Element entry, String tag) {
        Element tagElement = (Element) entry.getElementsByTagName(tag).item(0);
        return tagElement.getFirstChild().getNodeValue();
    }

    private String downloadAndParseXML(String url) {
        try {
            InputStream in = new java.net.URL(url).openStream();

            // build the XML parser
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            // parse and get XML elements
            Document dom = db.parse(in);
            Element documentElement = dom.getDocumentElement();

            // we want all XML children called 'item'
            NodeList nodes = documentElement.getElementsByTagName("item");

            // for each 'item' do the following
            for (int i = 0; i < nodes.getLength(); i++) {
                Element entry = (Element) nodes.item(i);

                // get the nodes from 'item' that you need
                String title = getNodeValue(entry, "title");
                String description = getNodeValue(entry, "description");
                String link = getNodeValue(entry, "link");
                // add them to your list
                items.add(new RowItem(title, description, link));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    protected String doInBackground(String... urls) {
        if (urls.length <= 0)
            return null;
        return downloadAndParseXML(urls[0]);
    }

    protected void onPostExecute(String result) {
        updateList(items);
    }
}

public void updateList(ArrayList<RowItem> items) {
    CustomArrayAdapter adapter = new CustomArrayAdapter(this,
            R.layout.item, items);
    listView.setAdapter(adapter);
}

// class used to have custom view for the list item
private class CustomArrayAdapter extends ArrayAdapter<RowItem> {
    Context context;
    List<RowItem> items;

    private class ViewHolder {
        public TextView title;
        public TextView description;
    }

    public CustomArrayAdapter(Context context, int resource,
            List<RowItem> items) {
        super(context, resource, items);
        this.context = context;
        this.items = items;
    }

    @Override
    public View getView(int position, View rowView, ViewGroup parent) {
        // reuse the rowView if possible - for efficiency and less memory consumption
        if (rowView == null) {

LayoutInflater inflater = (LayoutInflater)   
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            rowView = inflater.inflate(R.layout.item, null);
            // configure view holder
            ViewHolder viewHolder = new ViewHolder();
            viewHolder.title = (TextView) rowView.findViewById(R.id.title);
            viewHolder.description = (TextView) rowView.findViewById(R.id.description);
            rowView.setTag(viewHolder);
        }
        // set the view
        ViewHolder holder = (ViewHolder) rowView.getTag();
        RowItem item = items.get(position);
        holder.title.setText(item.title);
        holder.description.setText(item.description);

        return rowView;
    }
}

ListView listView;
ArrayList<RowItem> items;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listview);

    // get the listView and set this to be the onItemClickListener
    listView = (ListView) findViewById(R.id.list);
    listView.setOnItemClickListener(this);

    // create the RowItem list - used for storing one news feed
    items = new ArrayList<RowItem>();

    // start the background task to get the news feed
    new DownloadXMLTask(items).execute("http://feeds.bbci.co.uk/news/rss.xml");
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
    RowItem item = items.get(position);
    // start browser with the url.

    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(item.url));
    startActivity(browserIntent);
}
}

这是布局中的我的代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/row_view" >
<TextView
    android:id="@+id/title"
    android:inputType="none"
    android:ellipsize="end"
    android:maxLines="2"
    android:minLines="1"
    android:textStyle="bold"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="5dp"
    android:textColor="#000000"
    android:textSize="16sp" />

<TextView
    android:id="@+id/description"
    android:inputType="none"
    android:maxLines="2"
    android:minLines="1"
    android:ellipsize="end"
    android:gravity="left"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="20dp"
    android:paddingBottom="5dp"
    android:textColor="#000000"
    android:textSize="14sp" />

这是ListView布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

 <ListView
    android:id="@+id/list"
    android:focusableInTouchMode="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
 />


</LinearLayout>

请帮助

1 个答案:

答案 0 :(得分:0)

如果您不想打开浏览器,则需要删除行

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(item.url));
startActivity(browserIntent);

用这样的东西代替:

Intent viewintent= new Intent(Main.this, ViewNews.class);
viewintent.putExtra("title",title.title);
viewintent.putExtra("description",item.description);
startActivity(viewintent);

您需要像这样创建一个新的Activity:

public class ViewNews extends Activity  {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout);
Intent intent = getIntent();
String title= intent.getStringExtra("title");
String description= intent.getStringExtra("description");
TextView titleview =(TextView)findViewById(R.id.title);
titleview.setText(title); 
TextView descriptionview =(TextView)findViewById(R.id.description);
descriptionview .setText(description); 
}

你的XML mylayout可能是这样的

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/tile"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/description"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/tile"
    android:layout_alignParentLeft="true"
    android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>