1

在我的应用程序中,我想对同一帖子类别中的每个帖子进行分组。Listview 不会刷新项目计数。例如,“.Net”类别有 3 个帖子。Listview 有 15 行。12 行似乎是空的。如何刷新列表视图行数?我尝试了一些东西,但我没有。我是否为每个类别创建不同的布局文件?第一张图片显示了我博客中的所有帖子。第二张图片显示仅获取“.Net”类别的帖子。

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();
    }
4

1 回答 1

3

当您获得所有类别的长度时,问题出在您的 XML 解析器上

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;
}

这将为所有内容返回相同的值。您需要测试传入的数据。这将适用于您尝试做的事情

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

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

等等等等。

于 2013-01-19T01:31:55.853 回答