Question

In my app, I want to do groups of each posts in same post category. Listview doesn't refresh of items counts. Forexample, ".Net" category has 3 posts. Listview has 15 rows. 12 rows are seem empty. How can I refresh listview rows count ? I tried something but I didn't. Do I create different layout file for every category ? First image shows getting all of posts in my blog. Second image shows getting just posts of ".Net" category.

public class Anasayfa extends FragmentActivity implements ActionBar.TabListener {

    private String[][] liste;
    private int tabdeger = 0; 
    private MyCustomAdapter adapter;

    private static ListView listview;

    /**
     * The serialization (saved instance state) Bundle key representing the
     * current tab position.
     */

    public class arkaPlanIsleri extends  AsyncTask<String[][], String[][], String[][]> {

         private ProgressDialog dialog = new ProgressDialog(Anasayfa.this);
         int kategori;

         public arkaPlanIsleri(int kategori){
             this.kategori = kategori;
         }

         @Override
         protected void onPostExecute(String[][] liste) {
          // TODO Auto-generated method stub
             switch(kategori){
             case 0:

             listview = (ListView)findViewById(R.id.list);
             adapter = new MyCustomAdapter(Anasayfa.this, R.layout.list, liste[0]);
             listview.setAdapter(adapter);


             break;

             case 1:

                 listview = (ListView)findViewById(R.id.list);
                 adapter = new MyCustomAdapter(Anasayfa.this, R.layout.deneme, liste[0]);
                 listview.setAdapter(adapter);

                 break;
             }


          dialog.dismiss();
         }
         @Override
         protected void onPreExecute() {
          // TODO Auto-generated method stub
            dialog.setMessage("Yükleniyor...");

            dialog.show();
         }


        @Override
        protected String[][] doInBackground(String[][]... params) {
            // TODO Auto-generated method stub
            getListFromXml("feed",kategori);
            return liste;
        }
    }

    public class MyCustomAdapter extends ArrayAdapter<String> {

        String[] xmlList;

         public MyCustomAdapter(Context context, int textViewResourceId,
           String[] liste) {
          super(context, textViewResourceId, liste);
          // TODO Auto-generated constructor stub

         xmlList = liste;
         }

         @Override
         public View getView(int position, View convertView, ViewGroup parent) {
          // TODO Auto-generated method stub
          //return super.getView(position, convertView, parent);

          View row=convertView;

          switch(tabdeger){
          case 0:
            if(row==null){
           LayoutInflater inflater=getLayoutInflater();
           row=inflater.inflate(R.layout.list, parent,false);
           } 

            TextView label=(TextView)row.findViewById(R.id.text1);
            label.setText(xmlList[position]);

            ImageView image =(ImageView)row.findViewById(R.id.img);
            image.setImageResource(R.drawable.ic_launcher );
            break;
          case 1:
              if(row==null){
                   LayoutInflater inflater=getLayoutInflater();
                   row=inflater.inflate(R.layout.deneme, parent,false);
                   } 
              TextView label2=(TextView)row.findViewById(R.id.text2);
              label2.setText(xmlList[position]);

              ImageView image2 =(ImageView)row.findViewById(R.id.img2);
              image2.setImageResource(R.drawable.ic_launcher );
              break;
          }

          return row;
         }
        }

    public String[][] getListFromXml(String strng,int kategori)  {

        try {

            URL url=new URL(strng);
            DocumentBuilderFactory dFactory=DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder=dFactory.newDocumentBuilder();

            Document document=dBuilder.parse(new InputSource(url.openStream()));
            document.getDocumentElement().normalize();

            NodeList nodeListCountry=document.getElementsByTagName("item");
            int sayi = nodeListCountry.getLength();
            liste[1] = new String[sayi]; //category
                int genelcount=0;
                int netcount=0;
                int androidcount=0;
                int ilgicount=0;
                int windowscount=0;

            for (int i = 0; i < nodeListCountry.getLength(); i++) {
                Node node=nodeListCountry.item(i);
                Element elementMain=(Element) node;

                NodeList nodeListText=elementMain.getElementsByTagName("title");
                Element elementText=(Element) nodeListText.item(0);

                NodeList kategoriler = elementMain.getElementsByTagName("category");
                Element kategoriText = (Element) kategoriler.item(0);
                liste[1][i] = kategoriText.getChildNodes().item(0).getNodeValue();
                    switch(kategori){
                    case 0:
                        break;
                    case 1:
                        netcount++;
                        break;
                    case 2:
                        androidcount++;
                        break;
                    case 3:
                        genelcount++;
                        break;
                    case 4:
                        ilgicount++;
                        break;
                    case 5:
                        windowscount++;
                        break;
                    }
                }

            switch(kategori){
                case 0:
                    liste[0] = new String[sayi]; //title
                    break;
                case 1:
                    liste[0] = new String[netcount]; //title
                    break;
                case 2:
                    liste[0] = new String[androidcount]; //title
                    break;
                case 3:
                    liste[0] = new String[genelcount]; //title
                    break;
                case 4:
                    liste[0] = new String[ilgicount]; //title
                    break;
                case 5:
                    liste[0] = new String[windowscount]; //title
                    break;
                }
            int flag=0;

            for (int i = 0; i < nodeListCountry.getLength(); i++) {
                Node node=nodeListCountry.item(i);
                Element elementMain=(Element) node;

                NodeList nodeListText=elementMain.getElementsByTagName("title");
                Element elementText=(Element) nodeListText.item(0);

                switch(kategori){
                case 0:
                    liste[0][i]=elementText.getChildNodes().item(0).getNodeValue();
                    break;
                case 1:
                    if(liste[1][i].equals(".Net")){
                        liste[0][flag]=elementText.getChildNodes().item(0).getNodeValue();
                        flag++;
                    }
                    break;
                case 2:
                    if(liste[1][i].equals("Android")){
                        liste[0][flag]=elementText.getChildNodes().item(0).getNodeValue();
                        flag++;
                    }
                    break;
                case 3:
                    if(liste[1][i].equals("Genel")){
                        liste[0][flag]=elementText.getChildNodes().item(0).getNodeValue();
                        flag++;
                    }
                    break;
                case 4:
                    if(liste[1][i].equals("İlgi Çekici")){
                        liste[0][flag]=elementText.getChildNodes().item(0).getNodeValue();
                        flag++;
                    }
                    break;
                case 5:
                    if(liste[1][i].equals("Windows Phone")){
                        liste[0][flag]=elementText.getChildNodes().item(0).getNodeValue();
                        flag++;
                    }
                    break;
                }
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        }catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


        return liste;
    }



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

            liste = new String[2][];

    //       new arkaPlanIsleri(tabdeger).execute();

             listview = (ListView)findViewById(R.id.list);

            // Set up the action bar to show tabs.
            final ActionBar actionBar = getActionBar();
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

            // For each of the sections in the app, add a tab to the action bar.

            actionBar.addTab(actionBar.newTab().setText("HEPSİ")
                    .setTabListener(this));
            actionBar.addTab(actionBar.newTab().setText(".Net")
                    .setTabListener(this));
            actionBar.addTab(actionBar.newTab().setText("ANDROID")
                    .setTabListener(this));
            actionBar.addTab(actionBar.newTab().setText("GENEL")
                    .setTabListener(this));
            actionBar.addTab(actionBar.newTab().setText("İLGİ ÇEKİCİ")
                    .setTabListener(this));
            actionBar.addTab(actionBar.newTab().setText("WINDOWS PHONE")
                    .setTabListener(this));
        }


@Override
    public void onTabSelected(ActionBar.Tab tab,
            FragmentTransaction fragmentTransaction) {
        // When the given tab is selected, show the tab contents in the
        // container view.
        Fragment fragment = new DummySectionFragment();
        Bundle args = new Bundle();
        args.putInt(DummySectionFragment.ARG_SECTION_NUMBER,
                tab.getPosition() + 1);
        fragment.setArguments(args);
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.container, fragment).commit();

        tabdeger = tab.getPosition();
        liste = new String[2][];
//      xmlList = new String[sayi];
        listview.setAdapter(null);
        new arkaPlanIsleri(tabdeger).execute();
    }
Was it helpful?

Solution

The Problem is with your XML parser when you get the length of all the Catagories

liste[1][i] = ((Node) kategoriText).getChildNodes().item(0).getNodeValue();

switch(kategori){
      case 0:
         break;
      case 1:
         netcount++;
         break;
      case 2:
         androidcount++;
         break;
      case 3:
         genelcount++;
         break;
      case 4:
         ilgicount++;
         break;
      case 5:
         windowscount++;
         break;
}

this will return the same value for everything. you need to test the data coming in. this would work for waht you are trying to do

liste[1][i] = ((Node) kategoriText).getChildNodes().item(0).getNodeValue();

if (liste[1][i].contains(".Net")){
    netcount++;
} else if (liste[1][i].contains("Android")){
    androidcount++;
} .........

ect ect.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top