Парсинг сайтов урок 4

MainActivity.java

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;

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;
import java.util.List;


public class MainActivity extends AppCompatActivity {
private Document doc;
private Thread secThread;
private Runnable runnable;
private ListView listView;
private CustomArrayAdapter adapter;
private List<ListItemClass> arrayList;

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





}
private void init()
{
listView = findViewById(R.id.listView);
arrayList = new ArrayList<>();
adapter = new CustomArrayAdapter(this,R.layout.list_item_1,arrayList,getLayoutInflater());
listView.setAdapter(adapter);
runnable = new Runnable() {
@Override
public void run() {
getWeb();

}
};
secThread = new Thread(runnable);
secThread.start();
/* ListItemClass items = new ListItemClass();
items.setData_1("Dollar");
items.setData_2("213");
items.setData_3("657");
items.setData_4("78");
arrayList.add(items);
items = new ListItemClass();
items.setData_1("Dollar");
items.setData_2("98");
items.setData_3("6");
items.setData_4("456");
arrayList.add(items);
adapter.notifyDataSetChanged();*/






}
private void getWeb()
{
try {
doc = Jsoup.connect("https://minfin.com.ua/currency/").get();
Elements tables = doc.getElementsByTag("tbody");
Element our_table = tables.get(0);
Elements elements_from_table = our_table.children();
Element dollar = elements_from_table.get(0);
Elements dollar_elements = dollar.children();
Log.d("MyLog","Tbody size : " + our_table.children().get(0).text());
for(int i = 0;i < our_table.childrenSize();i++ )
{
ListItemClass items = new ListItemClass();
items.setData_1(our_table.children().get(i).child(0).text());
items.setData_2(our_table.children().get(i).child(1).text());
items.setData_3(our_table.children().get(i).child(2).text().substring(0,7));
items.setData_4(our_table.children().get(i).child(3).text());
arrayList.add(items);
}
runOnUiThread(new Runnable() {
@Override
public void run() {
adapter.notifyDataSetChanged();
}
});






} catch (IOException e) {
e.printStackTrace();
}
}
}

ListItemClass.java

public class ListItemClass {
String data_1;
String data_2;
String data_3;
String data_4;

public String getData_1() {
return data_1;
}

public void setData_1(String data_1) {
this.data_1 = data_1;
}

public String getData_2() {
return data_2;
}

public void setData_2(String data_2) {
this.data_2 = data_2;
}

public String getData_3() {
return data_3;
}

public void setData_3(String data_3) {
this.data_3 = data_3;
}

public String getData_4() {
return data_4;
}

public void setData_4(String data_4) {
this.data_4 = data_4;
}
}

CustomArrayAdapter.java

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

public class CustomArrayAdapter extends ArrayAdapter<ListItemClass> {
private LayoutInflater inflater;
private List<ListItemClass> listItem = new ArrayList<>();



public CustomArrayAdapter(@NonNull Context context, int resource, List<ListItemClass> listItem, LayoutInflater inflater) {
super(context, resource, listItem);
this.inflater = inflater;
this.listItem = listItem;


}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
ViewHolder viewHolder;
ListItemClass listItemMain = listItem.get(position);
if(convertView == null){
convertView = inflater.inflate(R.layout.list_item_1, null, false);
viewHolder = new ViewHolder();
viewHolder.data1 = convertView.findViewById(R.id.tvData1);
viewHolder.data2 = convertView.findViewById(R.id.tvData2);
viewHolder.data3 = convertView.findViewById(R.id.tvData3);
viewHolder.data4 = convertView.findViewById(R.id.tvData4);
convertView.setTag(viewHolder);



}
else
{
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.data1.setText(listItemMain.getData_1());
viewHolder.data2.setText(listItemMain.getData_2());
viewHolder.data3.setText(listItemMain.getData_3());
viewHolder.data4.setText(listItemMain.getData_4());








return convertView;
}
private class ViewHolder{
TextView data1;
TextView data2;
TextView data3;
TextView data4;


}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Курс валют в Украине"
android:textColor="@android:color/background_dark"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.161"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.054" />

<ListView
android:id="@+id/listView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="32dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />

</androidx.constraintlayout.widget.ConstraintLayout>

list_item_1.xml

<?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="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:paddingTop="10dp"
android:paddingBottom="10dp">

<TextView
android:id="@+id/tvData1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="TextView"
android:textColor="@android:color/background_dark"
android:textSize="10sp"
android:textStyle="bold" />

<TextView
android:id="@+id/tvData2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="TextView"
android:textColor="@color/colorPrimary"
android:textSize="12sp" />

<TextView
android:id="@+id/tvData3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="TextView"
android:textColor="@color/colorAccent"
android:textSize="12sp" />

<TextView
android:id="@+id/tvData4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="TextView"
android:textColor="@android:color/holo_orange_dark"
android:textSize="12sp" />
</LinearLayout>

1 комментарий для “Парсинг сайтов урок 4”

  1. Здраствуй. Видает такую ошибку , подскажи как решить. Спасибо. Твои уроки супер

    Process: com.example.exchangereal, PID: 9348
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.exchangereal/com.example.exchangereal.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method ‘android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()’ on a null object reference

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *