0

Hi guys I m new in android ı have a proje and ı take data news from website but when ı take data,all news seen in my screen How can I take just the first news and one more thing how can ı put in listview with order.

Web site:

<table width="100%" cellpadding="0" cellspacing="0" border="0" align="center" class="contentpane">
<tbody><tr>
	<td valign="top" class="contentdescription" colspan="2">
		</td>
</tr>
<tr>
	<td>
	<script language="javascript" type="text/javascript">

</script>
<form action="http://www.izmir.edu.tr/tr/genel-haberler.html" method="post" name="adminForm">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tbody><tr class="sectiontableentry1"> **FİRST NEWS**
	<td align="right">
		1	</td>
			<td>
		<a href="/tr/genel-haberler/4711-Cocuk-Korumada-Kanita-Dayali-Degerlendirme-ve-Analiz-Cercevesi---IZMIR.html">
			Çocuk Korumada Kanıta Dayalı Değerlendirme ve Analiz Çerçevesi - İZMİR</a>
				</td>
					</tr>
<tr class="sectiontableentry2"> **SECOND NEWS**
	<td align="right">
		2	</td>
			<td>
              <a href="/tr/genel-haberler/4748-Madde-bagimliligi-yuzde-100-tedavi-edilemeyen-bir-beyin-hastaligidir.html">
			“Madde bağımlılığı, yüzde 100 tedavi edilemeyen bir beyin hastalığıdır”</a>
				</td>
					</tr>

protected void onCreate(Bundle savedInstanceState) {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
     StrictMode.setThreadPolicy(policy);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv = (TextView)findViewById(R.id.textView1);

    Document doc;
     try {
          // need url
          doc = Jsoup.connect(url).get();
          Elements links = doc.select("table[class=contentpane]");
          Elements row=links.select("tr:gt(0)");
          Elements tds = row.select("td");
          tv.setText(tds.text());
         } catch (IOException ex) {
               tv.setText("Error");
   }   
}

1 Answer 1

1

Using the code you already have, when you do doc.select(....) it returns a collection of Elements, so you could just do this:

Elements links = doc.select("table[class=contentpane]");
Elements row=links.select("tr:gt(0)");
Elements tds = row.select("td");
tv.setText(tds.get(0).text());

Another way to do this is, only getting the first Element of Elements returned (In one line):

tv.setText(doc.select("table[class=contentpane]").select("tr:gt(0)").select("td").first().text());

Note that I have not tested this code because I don't have JSoup lib right now.

Sign up to request clarification or add additional context in comments.

2 Comments

so how can ı put in listwiew ?
Because you want the ListView to be sorted you should create a class that extends ArrayAdapter<String> an then define the sorting properties that you want. After that add elements to the adapter and then set the adapter on the ListView. There are a lot of tutorials on how to handle/populate list views (for example): vogella.com/tutorials/AndroidListView/article.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.