1

I'm new in Android programming. I created classes of web objects each class has 3 RSS URLs to be parsed. I loop an array of this objects to operate on each one the AsyncTask Executer. Every time I do that different errors appear and the UI freezes. I can't find what's wrong. Then I use custom adapter to display the UI but program don't even reach to that code. Please help me finding what's wrong with my code. I tried everything I had in mind.

I loop it in the main thread like this:

public class MainActivity extends Activity 
{   
ArrayList <WebPage> wpObjects;
ListView lv;


int threadIsFinishedcounter=0;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lv=(ListView)findViewById(R.id.listView1);


    WebPage ynet= new WebPage(R.drawable.ynet,  "http://www.ynet.co.il/Integration/StoryRss2.xml","http://www.ynet.co.il/Integration/StoryRss6.xml","http://www.ynet.co.il/Integration/StoryRss3.xml");
    WebPage walla=new WebPage(R.drawable.walla, "http://rss.walla.co.il/?w=/1/0/12/@rss.e","http://rss.walla.co.il/?w=/2/0/12/@rss.e","http://rss.walla.co.il/?w=/3/0/12/@rss.e");
    WebPage  nrg= new WebPage(R.drawable.nrg,   "http://rss.nrg.co.il/news/","http://rss.nrg.co.il/finance","http://rss.nrg.co.il/sport");

    wpObjects=new ArrayList<WebPage>();

    wpObjects.add(ynet);
    wpObjects.add(walla);
    wpObjects.add(nrg);

    for(WebPage item:wpObjects)
    {

        //new getParseData(wpObjects.indexOf(item)).execute(item.getUrls());
        new getParseData(wpObjects.indexOf(item)).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, item.getUrls());


    }


    if(threadIsFinishedcounter==wpObjects.size())
       {

           MyCostumedAdapter adapter=new MyCostumedAdapter(MainActivity.this, wpObjects); 

           lv.setAdapter(adapter);
           if(threadIsFinishedcounter==wpObjects.size())
       }
}

The AsyncTask class:

 class getParseData extends AsyncTask<String,Void, Collecter>
 {
    int indexofwp;

    public getParseData(int indexofwp) {
        super();
        this.indexofwp = indexofwp;
    }
    protected Collecter doInBackground(String... params) {

        Collecter col = new Collecter() ;

        ArrayList<String> allResult = new ArrayList<String>();
        ArrayList<String> Titles = new ArrayList<String>();

        for (int y = 0; y < params.length; y++) {
            Titles.clear();
            allResult.clear();

                col.yIndex = y;

            String urlString = params[y];
            try {

                URL url = new URL(urlString);
                XmlPullParserFactory factory = XmlPullParserFactory
                        .newInstance();
                XmlPullParser parser = factory.newPullParser();
                InputStream is = url.openStream();
                parser.setInput(is, null);
                boolean inItemTag = false;
                boolean inTitleTag = false;
                String TagName;
                int EventType = parser.getEventType();

                while (EventType != XmlPullParser.END_DOCUMENT) {
                    Log.i("im in while loop of parser", "");
                    if (EventType == XmlPullParser.START_TAG) {
                        TagName = parser.getName();

                        if (inItemTag) {
                            if (TagName.equals("title"))
                                inTitleTag = true;
                        } else// !item
                        {
                            if (TagName.equals("item"))
                                inItemTag = true;
                        }
                    }
                    if (EventType == XmlPullParser.TEXT) {
                        if (inTitleTag) {

                            Titles.add(parser.getText());// AD THE TITLE
                            inTitleTag = false;
                        }

                    }
                    if (EventType == XmlPullParser.END_TAG) {
                        TagName = parser.getName();
                        if (TagName.equals("item"))
                            inItemTag = false;
                    }
                    EventType = parser.next();

                    Log.i("im after parser.next", "");
                }// end while of parsing loop

            } catch (MalformedURLException e) {
                e.printStackTrace();
                Log.i("EXEPTION******************",
                        "MalformedURLException*********");
            } catch (XmlPullParserException e) {
                e.printStackTrace();
                Log.i("EXEPTION******************",
                        "XmlPullParserException*********");
            } catch (IOException s) {
                s.printStackTrace();
                Log.i("IOException***************************", "");

            }

            synchronized (col) {
                if (col.yIndex == 0) {

                    col.news.addAll(Titles);
                    col.rssRsult.add(col.news);
                }

                if (col.yIndex == 1) {

                    col.economy.addAll(Titles);
                    col.economy.size()+"");
                    col.rssRsult.add(col.economy);

                }

                if (col.yIndex == 2) {

                    col.sports.addAll(Titles);
                    col.sports.size()+"");
                    col.rssRsult.add(col.sports);
                }
            }

        }// end of y loop
        return col;
    }


    @Override
    protected void onPreExecute() 
    {
        if(threadIsFinishedcounter==wpObjects.size())
            threadIsFinishedcounter=0;
    }


    @Override
    protected void onPostExecute(Collecter coll) 
    {   
        if(indexofwp<wpObjects.size())
       {

        wpObjects.get(indexofwp).NewsTitles.addAll(coll.rssRsult.get(0));
        wpObjects.get(indexofwp).EconomicsTitles.addAll(coll.rssRsult.get(1))
        wpObjects.get(indexofwp).SportssTitles.addAll(coll.rssRsult.get(2));
        threadIsFinishedcounter++;
        }

I thought maybe my custom adapter is wrong but didn't find anything wrong:

public class MyCostumedAdapter extends BaseAdapter
{
   Context context;
   ArrayList<WebPage> wp;
   public MyCostumedAdapter(Context context , ArrayList<WebPage> wp) 
   {
    super();
    this.context = context;
    this.wp=wp;
   }

@Override
public int getCount() 
{
    return wp.size();
}
@Override
public Object getItem(int position) 
{
    return wp.get(position);
}

@Override
public long getItemId(int position) 
{
    return 0;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) 
{
    View rowView;

    if(convertView==null)
    {
        LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        rowView= inflater.inflate(R.layout.lvlayout, null);
    }
    else
    {
        final WebPage currentwp=wp.get(position);
        rowView=convertView;

        ImageView imag=(ImageView)rowView.findViewById(R.id.imageView1);
        imag.setImageResource(currentwp.getIcon());


        Button newsButton=(Button)rowView.findViewById(R.id.AllnewsButton);
        newsButton.setOnClickListener(new View.OnClickListener() 
        {
            @Override
            public void onClick(View v) 
            {
                Intent intent=new Intent(context,NewHeadlines.class);
                intent.putExtra("newsbutton",currentwp.getNewsTitles());//to pass information to next activity
                context.startActivity(intent);


            }
        });
        Button economicsButton=(Button)rowView.findViewById(R.id.AllEconomicsButton);
        economicsButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) 
            {
                Intent intent=new Intent(context,NewHeadlines.class);
                intent.putExtra("economicbutton",currentwp.getEconomicsTitles());//to pass information to next activity
                context.startActivity(intent);
            }
        });
        Button sportsButton=(Button)rowView.findViewById(R.id.AllSportsButton);
        sportsButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) 
            {
                Intent intent=new Intent(context,NewHeadlines.class);
                intent.putExtra("sportsbutton",currentwp.getSportssTitles());//to pass information to next activity
                context.startActivity(intent);
            }
        });
        TextView firstNewsTitle=(TextView)rowView.findViewById(R.id.firstnewsTitle);
        firstNewsTitle.setText(currentwp.getNewsTitles().get(0));
        TextView firstEconomicsTitle=(TextView)rowView.findViewById(R.id.firsteconomicsTitle);
        firstEconomicsTitle.setText(currentwp.getEconomicsTitles().get(0));
        TextView firstSportsTitle=(TextView)rowView.findViewById(R.id.firstSportsTitle);
        firstSportsTitle.setText(currentwp.getSportssTitles().get(0));      
    }        
    return rowView;
}
}

main layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="326dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:src="@drawable/ic_launcher" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/AllnewsButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/News" />

    <TextView
        android:id="@+id/firstnewsTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView" />
</LinearLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/AllEconomicsButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Economic" />

    <TextView
        android:id="@+id/firsteconomicsTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView" />
</LinearLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/AllSportsButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Sports" />

    <TextView
        android:id="@+id/firstSportsTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView" />
  </LinearLayout  >
 </LinearLayout   > 

each ListView row:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="326dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:src="@drawable/ic_launcher" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/AllnewsButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/News" />
    <TextView
        android:id="@+id/firstnewsTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView" />
</LinearLayout>
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    <Button
        android:id="@+id/AllEconomicsButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Economic" />

    <TextView
        android:id="@+id/firsteconomicsTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView" />
 </LinearLayout>

 <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"  >

    <  Button
        android:id="@+id/AllSportsButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Sports" />
      <  TextView
        android:id="@+id/firstSportsTitle"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:text="TextView" />
  </LinearLayout>   
</LinearLayout>
hata
  • 11,633
  • 6
  • 46
  • 69
rbm
  • 19
  • 2

1 Answers1

1

You should pass your array inyo the constructor and then operate and handle that there. I am sure that you can send your array into your params, but you should make your Asynktask diferent fr string values, as currently you have. So the easy and best way is :

int indexofwp;
List<String> allResult = new ArrayList<String>()
public getParseData(int    indexofwp, List<String> allResult) 
{
    super();
    this.indexofwp = indexofwp;
    This.allResult=allResult;
}

And thats all. Regards.

Max Pinto
  • 1,463
  • 3
  • 16
  • 29
  • Thank you but i need to understand better you mean to pass the arraylist of the wpobjects i created? every object at that list has 3 arraylists to be feeded from the do in background parsing so i thought maybe to pass in the constructor this 3 arraylists – rbm Aug 12 '15 at 11:39
  • Could you explain what is the use of what i pass in the asynctask constructor and between what i pass in the execute params? – rbm Aug 12 '15 at 11:53
  • Sure. If you pass in the Asynctask constructor param, you can leave Asynctask as you have right now, it means: AsyncTask, and you can just pass the array to the constructor. But if you wan to do in the execute params, you have to change your Asynctask definition to this: extends AsyncTask, Void, ArrayList>... and thats a litle complicated, if i were you, i use the constructor option. Regards – Max Pinto Aug 12 '15 at 14:27
  • this answer could explain a litle better and detailed: http://stackoverflow.com/questions/4195609/passing-arguments-to-asynctask-and-returning-results – Max Pinto Aug 12 '15 at 14:29