package ru.sezex.miped.adapter;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import ru.sezex.miped.R;
public class NewsAdapter extends BaseAdapter {
ActionBarActivity context;
public ArrayList<String> image;
public ArrayList<String> title;
public NewsAdapter(ActionBarActivity context,
ArrayList<String> image,
ArrayList<String> title) {
super();
this.context = context;
this.image = image;
this.title = title;
}
public int getCount() {
return title.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
private class ViewHolder {
ImageView imgViewPreview;
TextView txtViewTitle;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.main_item, parent, false);
holder = new ViewHolder();
holder.imgViewPreview = (ImageView) convertView.findViewById(R.id.preview);
holder.txtViewTitle = (TextView) convertView.findViewById(R.id.title);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Picasso.with(context).load(image.get(position)).into(holder.imgViewPreview);
holder.txtViewTitle.setText(title.get(position));
return convertView;
}
}
public class NewThread extends AsyncTask<String, Void, ArrayList<String>> {
@Override
protected ArrayList<String> doInBackground(String... arg) {
Document doc;
urlIncomplete = "http://miped.ru/page/";
url = urlIncomplete + pageNum;
try {
doc = Jsoup.connect(url).get();
imageList.clear();
titleList.clear();
linkList.clear();
image = doc.select("div.story div.story_c img");
title = doc.select("div.story div.story_title a");
link = doc.select("div.story div.story_title a");
for (Element images : image) {
imageList.add(images.attr("src"));
}
for (Element titles : title) {
titleList.add(titles.text());
}
for (Element links : link) {
linkList.add(links.attr("href"));
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(ArrayList<String> result) {
if (pageNum != 1) {
prevPage.setVisibility(View.VISIBLE);
} else {
prevPage.setVisibility(View.GONE);
}
news.setAdapter(adapter);
news.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View itemClicked, int position,
long id) {
int idClicked;
idClicked = position;
Intent browseIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(linkList.get(idClicked)));
browseIntent.putExtra("title", titleList.get(idClicked));
startActivity(browseIntent);
}
});
news.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View itemClicked, int position,
long id) {
int idClicked;
idClicked = position;
@SuppressWarnings("deprecation")
ClipboardManager copylink = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
copylink.setText(titleList.get(idClicked) + ": " + linkList.get(idClicked));
copy = Toast.makeText(getApplication(), R.string.copy,
Toast.LENGTH_SHORT);
copy.show();
return true;
}
});
}
}