P
P
pmozil2017-05-27 18:09:38
Android
pmozil, 2017-05-27 18:09:38

How to parse a site using jsoup to pull out 2 items and build a listView using custom adapter?

The idea is to parse the discount news page of a website. For these purposes, I wrote an adapter that has 2 variables title and price. and so I need that each news in listView had 2 parameters: TITLE and DISCOUNT. I just don't know how to implement it in the doInBackgroung method. Please help me.

public class PromoActivity extends Activity {
public static final String TAG = "myLogs";
public ArrayList<News> newsList = new ArrayList<>();
private ListView lv;
public ProgressDialog pd;

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


}

public class Parse extends AsyncTask<String, Void, ArrayList<News>> {
    protected ArrayList<News> doInBackground(String... Params) {
        Document doc;
        Elements title;
        Elements price;
        try {
            doc = Jsoup.connect("http://www.groupon.pl/oferta/krakow").get();
            Log.d(TAG, "Pobrano stronę");
            price = doc.select(".deal-price");
            title = doc.select(".deal-tile-content");
            String p = String.valueOf(price);
            String t = title.toString();
            newsList.clear();
          // ЧТО ДОЛЖНО БЫТЬ СДЕСЬ?

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

    }

    protected void onPostExecute(ArrayList<News> result) {
        lv = (ListView) findViewById(R.id.lv);
        lv.setAdapter((new MyAdapter(PromoActivity.this,
                R.layout.list_item, result)));

        pd.dismiss();
    }
}

public void ok(View v) {
    pd = ProgressDialog.show(PromoActivity.this, "Pobieranie...", "proszę czekać", true, false);
    Log.d(TAG, "Button clicked");
    sprawdzPolaczenie(v);
}

public void sprawdzPolaczenie(View view) {
    ConnectivityManager connMgr = (ConnectivityManager)
            getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
    if (networkInfo != null && networkInfo.isConnected()) {
        new Parse().execute();
    } else {
        Toast.makeText(PromoActivity.this, "Sprawdź polączenie z internetem", Toast.LENGTH_LONG).show();
        pd.dismiss();
    }
}

This is the adapter:
public class MyAdapter extends BaseAdapter {
Context ctx;
LayoutInflater lInflater;
static List<News> objects;
public Context context;

public MyAdapter(Context context, int item, List<News> newsList) {
    ctx = context;
    objects = newsList;
    lInflater = (LayoutInflater) ctx
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    return 0;
}

@Override
public Object getItem(int position) {
    return objects.get(position);
}

@Override
public long getItemId(int position) {
    return objects.size();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    if (view == null) {
        view = lInflater.inflate(R.layout.list_item, parent, false);
    }
    News n = getNews(position);
    ((TextView) view.findViewById(R.id.tvName)).setText(n.mTitle);
    ((TextView) view.findViewById(R.id.tvPrice)).setText(String.valueOf(n.mPrice));

    return view;

}
News getNews(int position) {
    return ((News) getItem(position));
}

and the News class itself:
public class News {
String mTitle;
String mPrice;


public News(String title,Double price){
    mTitle = title;
    mPrice = price;

}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question