Good day. There is a code (reduced to the considered components). The code goes to parsing articles from the site, then each article is placed in the arr
array and loaded in onPostExecute
in the listview lv
: lv.setAdapter(adapter);
. There are no problems, everything is fine. I want to add the time of adding the article (also sparsen respectively) and display in each list the listview
on the right below in small print. It turns out you already need a custom listview
. This is where the problems begin. Due to my lack of experience, I myself will not write a new adapter, so I considered examples. I tried to customize this example . Everything seems to be nothing, but only there it is loaded from ready-made arrays, I tried to install my own - it swears that the types are supposedly not appropriate (probably because I have an ordinary array, but the ArrayList<String>
collection). Maybe someone knows, please tell textview
how in this example in one listview
element to display two textview
from the given arr
array at once. In this example, I have not yet implemented the article time parsing, let there be two identical textview
(with the text of the article), I’m going to go further under myself. Thanks in advance who will help.
MainActivity.java
import android.content.Intent; import android.os.AsyncTask; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; import android.widget.TextView; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import java.io.IOException; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { Parser par; ListView lv; ArrayList<String> arr = new ArrayList<>(); ArrayAdapter<String> adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv = (ListView)findViewById(R.id.listView); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); par = new Parser(); par.execute(); adapter = new ArrayAdapter<String>(this, R.layout.list_item, arr); } class Parser extends AsyncTask<Void, Void, Void> { protected void onPreExecute() { super.onPreExecute(); } @Override protected Void doInBackground(Void ... params) { Document doc = null; try { doc = Jsoup.connect("http://www.site.ru").get(); } catch (IOException e) { e.printStackTrace(); } if (doc != null) { Elements text2 = doc.select("div.entry-content"); for (Element temp : text2) { arr.add(temp.text().replaceAll("—", "\n —")); } } return null; } @Override protected void onPostExecute(Void result) { super.onPostExecute(result); lv.setAdapter(adapter); } } }
list_item.xml
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" android:textColor="@color/colorText" android:textSize="20sp" android:divider="@color/colorDivider" android:typeface="serif" > </TextView>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <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" tools:context=".MainActivity" > <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" android:src="@drawable/logo" android:title="@string/app_name"> <ImageView android:id="@+id/logo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/logo" android:layout_gravity="left"/> </android.support.v7.widget.Toolbar> <ListView android:layout_width="wrap_content" android:layout_height="match_parent" android:id="@+id/listView" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_above="@+id/button_next" android:choiceMode="none" android:scrollingCache="true" android:dividerHeight="1dp" android:divider="@color/colorDivider" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:stackFromBottom="false" android:smoothScrollbar="false" android:layout_below="@+id/toolbar" android:padding="16dp" /> </RelativeLayout>