пишу новостное приложение на android, столкнулся со следующей проблемой. Думаю из видео станет понятно.
Вот, залил на Youtube... https://www.youtube.com/watch?v=7bJJr3skbA4
Вот код самого активити
public class AstActivity extends Activity {
ArrayList<Product> products = new ArrayList<Product>();
ArrayList<Product_place> products2 = new ArrayList<Product_place>();
ArrayList<Item> productsAstNews = new ArrayList<Item>();
Context ctxx;
BoxAdapter boxAdapter;
BoxAdapter2 boxAdapter2;
void fillData3() {
loadXml();
}
@Override
public void onBackPressed() {
finish();
startActivity(new Intent(AstActivity.this, AstActivity.class));
}
String valOfTag(String s, String tag) // выдаёт значение тега (tag) и удаляет его из строки.
{
String TAG_START = "<" + tag + ">";
String TAG_END = "</" + tag + ">";
int l = s.indexOf(TAG_START);
int r = s.indexOf(TAG_END);
if(r == -1 || l == -1)
{
return "not_found";
}
else
{
String valueOfTag = s.substring(l + TAG_START.length() + 1, r );
return valueOfTag;
}
}
String valOfLink(String s)
{
String tag = "enclosure" ;
String TAG_START = "<" + tag;
String TAG_END = "</" + tag + ">";
int l = s.indexOf(TAG_START);
int r = s.indexOf(TAG_END);
if(r == -1 || l == -1)
{
return "not_found";
}
else
{
String valueOfTag = s.substring(l + TAG_START.length() + 1, r );
valueOfTag = valueOfTag.substring(valueOfTag.indexOf("url=\"") + 5, valueOfTag.length() - 3);
//Log.d("borolis", valueOfTag);
return valueOfTag;
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ast);
TabHost tabs = (TabHost) findViewById(R.id.tabhosts);
ctxx = getApplicationContext();
tabs.setup();
TabHost.TabSpec spec = tabs.newTabSpec("tag1");
spec.setContent(R.id.tabs1);
spec.setIndicator("О городе");
tabs.addTab(spec);
spec = tabs.newTabSpec("tag2");
spec.setContent(R.id.tabs2);
spec.setIndicator("Что посетить?");
tabs.addTab(spec);
spec = tabs.newTabSpec("tag3");
spec.setContent(R.id.tabs3);
spec.setIndicator("Новости");
tabs.addTab(spec);
tabs.setCurrentTab(0);
fillData3();
private void loadXml()
{
AsyncTask<String, Element, String> task = new AsyncTask<String, Element, String>() {
@Override
protected void onPreExecute()
{
super.onPreExecute();
}
@Override
protected String doInBackground(String... params)
{
String doc = "";
try
{
doc = new Scanner(new URL("https://vlast.kz/feed/").openStream(), "UTF-8").useDelimiter("\\A").next();
}
catch (IOException e)
{
e.printStackTrace();
}
String itemx = "";
while(itemx != "not_found")
{
itemx = valOfTag(doc, "item");
if(itemx != "not_found")
{
int l = doc.indexOf("<item>");
int r = doc.indexOf("</item>");
doc = doc.substring(0, l) + doc.substring(r + 7, doc.length());
String nameI = valOfTag(itemx, "title"); //разбираем запись на нужные нам данные
String linkI = valOfTag(itemx, "link"); //
String pubDateI = valOfTag(itemx, "pubDate"); //
String descriptionI = valOfTag(itemx, "description");
String imageLinkI = valOfLink(itemx);
descriptionI= descriptionI.replaceAll("(?s)\\<em\\>.*s?\\<\\/em\\>", "");
descriptionI= descriptionI.replaceAll("(?s)\\<i\\>.*s?\\<\\/i\\>", "");
nameI = nameI.replaceAll(""", "");
descriptionI= descriptionI.replaceAll("\\<.*?\\>", "");
descriptionI= descriptionI.substring(9, descriptionI.length()-4);
String pubDate="";
SimpleDateFormat format = new SimpleDateFormat("dd MMMM yyyy");
Date d = null;
try
{
d = org.apache.http.impl.cookie.DateUtils.parseDate(pubDateI);
pubDate = format.format(d);
}
catch (DateParseException e)
{
e.printStackTrace();
}
Item newx = new Item(nameI, imageLinkI, descriptionI, linkI, pubDate);
productsAstNews.add(newx);
}
}
return "allDone";
}
@Override
protected void onPostExecute(String aVoid)
{
AdapterItem2 boxAdapter3 = new AdapterItem2(ctxx, productsAstNews);
ListView lvMain4 = (ListView) findViewById(R.id.vlastView);
lvMain4.setAdapter(boxAdapter3);
}
};
task.execute();
}
}
А вот Adapter
public class AdapterItem2 extends BaseAdapter {
Context ctx;
LayoutInflater lInflater;
ArrayList<Item> objects;
AdapterItem2(Context context, ArrayList<Item> products) {
ctx = context;
objects = products;
lInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
// пункт списка
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null)
{
view = lInflater.inflate(R.layout.city_item, parent, false);
}
Item p = objects.get(position);
((TextView) view.findViewById(R.id.cityTitle)).setText(p.getTitle());
((TextView) view.findViewById(R.id.cityDescription)).setText(p.getDescription());
((TextView) view.findViewById(R.id.cityAdress)).setText(p.getDate());
loadImage(p.getUrlImage(), ((ImageView) view.findViewById(R.id.cityImage)));
return view;
}
private void loadImage(final String urlImage,final ImageView imageView)
{
AsyncTask<String,ImageView,Bitmap> taskx = new AsyncTask<String, ImageView, Bitmap>()
{
@Override
protected Bitmap doInBackground(String... params)
{
Bitmap image = null;
try
{
URL url = new URL(params[0]);
image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
}
catch (Exception ex)
{
ex.printStackTrace();
}
return image;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
imageView.setImageBitmap(bitmap);
}
};
taskx.execute(urlImage);
}
}
getView
. Чем больше вы выкладываете кода, тем меньше человек захочет в нём разбираться.