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> 
  • Or maybe it makes sense to replace ListView with RecyclerView? - Nikolay Romanov
  • @NikolayRomanov, read about RecyclerView, hoping that there is easier ... The same dense forest ... - Pollux
  • Check the correct answer or add your own if none of the current ones helped you - Mihanik71

2 answers 2

If the problem is that instead of one TextView there would be two, then your item.xml should be something like this:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/title_layout_mainToolbar" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" /> </RelativeLayout> 

Plus custom adapter:

 public class BoxAdapter extends BaseAdapter { Context ctx; LayoutInflater lInflater; ArrayList<Product> objects; BoxAdapter(Context context, ArrayList<Product> products) { ctx = context; objects = products; lInflater = (LayoutInflater) ctx .getSystemService(Context.LAYOUT_INFLATER_SERVICE); } // кол-во элементов @Override public int getCount() { return objects.size(); } // элемент по позиции @Override public Object getItem(int position) { return objects.get(position); } // id по позиции @Override public long getItemId(int position) { return position; } // пункт списка @Override public View getView(int position, View convertView, ViewGroup parent) { // используем созданные, но не используемые view View view = convertView; if (view == null) { view = lInflater.inflate(R.layout.item, parent, false); } Product p = getProduct(position); //заполнение TextView данными ((TextView) view.findViewById(R.id.textView_frist)).setText(p.name); ((TextView) view.findViewById(R.id.textView_second)).setText(p.price + ""); return view; } // товар по позиции Product getProduct(int position) { return ((Product) getItem(position)); } } 

Instead of the Product object, you can have your own object that contains the data to fill in the item.

The result is that:

  1. you create a complex (more than one view) list item -> item.xml

  2. Create a custom adapter in which you infiterate items and, for each item, take an object with data from the list and fill it with the view data in the item.

  • Yes, I know) The problem is to write an adapter, which from the ArrayList<String> arr collection of ArrayList<String> arr fill both fields. - Pollux
  • I added the adapter, I did not run the code, but in general this is what you need! - Kirill Stoianov
  • If I understand your task correctly, then I don’t know what could be simpler ... - Kirill Stoianov

For me, as for a beginner, this example turned out to be the most understandable. True, pre-initialized arrays of String [] , I also had a collection of ArrayList<String> , initialized in the process of the application running through Jsoup . Replacing the corresponding types and fields setText() all worked! Thanks for the tips.