android中的活动指示器

时间:2012-11-29 06:03:43

标签: android widget activity-indicator

在我的应用程序中,我在galleryview中加载数据。如果用户点击galleryview中的任何项目,我将从adapterclass加载数据。我在此加载和显示数据之间保留了一个活动指示符。但是活动指示器没有消失,它是不断展示。

我的活动代码:

gal.setOnItemClickListener(new OnItemClickListener() 
       {                                                                                                                                                                                                                                                                                                                    
        @Override
        public void onItemClick (AdapterView<?> parent, View v, int position,
                long id) {
            // TODO Auto-generated method stub  

//           mDialog = new ProgressDialog(NewsPaperNov28MainGalleryActivity.this);
//           mDialog.setMessage("Please wait...");
//           mDialog.setCancelable(false);


             mDialog=  ProgressDialog.show(NewsPaperNov28MainGalleryActivity.this,"Working..", "Please Wait", true,false);           
             context.getInstance().setAppVariable("sectionurl", adapter.sectionurl[position]);
             Thread thread = new Thread();
             thread.start();


//          ListView list=(ListView)findViewById(R.id.list);
//          listadapter = new ListViewwithimageAdapter(this);
//          list.setAdapter(listadapter);        

        }
    });


        list.setOnItemClickListener(new OnItemClickListener() 
        {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) 
            {

                // TODO Auto-generated method stub
                context.getInstance().setAppVariable("storyurl", listadapter.url[position]);        
                Intent in = new Intent(getApplicationContext(), NewsDescription.class);
                startActivity(in);

            }
        });
    }
    @Override
    public void run() {
        // TODO Auto-generated method stub
//      context.getInstance().setAppVariable("sectionurl", adapter.sectionurl[position]);
        System.out.println("inside run");
        list=(ListView)findViewById(R.id.list);
        listadapter = new ListViewwithimageAdapter(this);
        System.out.println("B4 handle");
        handler.sendEmptyMessage(0);
    }

     private Handler handler = new Handler() {
         @Override
         public void handleMessage(Message msg) {
             System.out.println("Inside handler");
                 mDialog.dismiss();
                 list.setAdapter(listadapter);

         }
 };

我的ListViewwithimageAdapter类:

public class ListViewwithimageAdapter extends BaseAdapter
{
    private static Context contxt;
    final String URL = "http://xxxxxxx/xml/stories/"+rgd.xml;   
     String[] kickerimage = {};//new String[50];

    ListViewwithimageAdapter(Context conxt)
    {

        this.contxt=conxt;
        getelement();

    }

         public ListViewwithimageAdapter(
            OnItemSelectedListener onItemSelectedListener) 
         {
        // TODO Auto-generated constructor stub
//           this.contxt=conxt;
            getelement();
    }

        public ListViewwithimageAdapter(OnItemClickListener onItemClickListener) {
            // TODO Auto-generated constructor stub
            getelement();
        }


        String[] itemsarray = {};//new String[100];
        String[] url= {};//new String[30];
    public String[] getelement()
    {
//        System.out.println("Insid getelement");
          ArrayList<String> menuItems = new ArrayList<String>();
          TaplistingParser parser = new TaplistingParser();
//        System.out.println("url="+URL);
          String xml= parser.getXmlFromUrl(URL);
          Document doc=parser.getDomElement(xml);        
//        System.out.println("sssss="+doc);
          NodeList nl=doc.getElementsByTagName("article"); 
          kickerimage = new String[nl.getLength()];
          url = new String[nl.getLength()];

//        String headings = null;

          for(int i=0; i < nl.getLength(); i++)
          {

//            System.out.println("i="+i);
            HashMap<String, String> map = new HashMap<String, String>();
            Element e = (Element) nl.item(i);
//          map.put("Title", parser.getValue(e, "title"));
//          map.put("Date", parser.getValue(e, "create_date"));         
            url[i]=parser.getValue(e, "url");
//          System.out.println("b4 kick");
//          System.out.println("value="+parser.getValue(e, "title"));
            kickerimage[i]=parser.getValue(e, "kickerimage");
//          System.out.println("after kick");
//          System.out.println("kick="+kickerimage[i]);
            menuItems.add(parser.getValue(e, "title"));          
          }  
//         System.out.println("b4 items array");
           itemsarray = new String[menuItems.size()];
//         System.out.println("subbu");
           itemsarray=menuItems.toArray(itemsarray);
//         System.out.println("subbu1");
//         System.out.println("in last");
           return itemsarray;
    }
    @Override
    public int getCount() 
    {
        // TODO Auto-generated method stub
        return itemsarray.length;
    }

    @Override
    public Object getItem(int position) 
    {
        // TODO Auto-generated method stub
        return itemsarray[position];
    }
    @Override
    public long getItemId(int position) 
    {
        // TODO Auto-generated method stub
//      System.out.println("pos in id="+position);
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {



        // TODO Auto-generated method stub      
         Bitmap bitmap = DownloadImage(
                 kickerimage[position] );        
//       View listView = convertView;
         if (convertView   == null) 
            {
                //this should only ever run if you do not get a view back            
             LayoutInflater  inflater = (LayoutInflater) contxt
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
             convertView  = inflater.inflate(R.layout.homelistrow, null); 
            }
//       else
//       {
//           holder.removeAllViews();
//       }
//      View listView;
//      if (convertView == null) 
//      {
//          listView = new View(contxt);             
//          LinearLayout holder = (LinearLayout)convertView.findViewById(android.R.id);
////            holder = inflater.inflate(R.layout.homelistrow, null); 
//          System.out.println("pos="+position);
//          System.out.println("item="+getItem(position));
//       else
//       {
            TextView textView = (TextView) convertView
                    .findViewById(R.id.name_label);
            textView.setText(itemsarray[position]);

            ImageView imageView = (ImageView) convertView
                    .findViewById(R.id.icon);
            imageView.setImageBitmap(bitmap);
//      }
//      else        
//      {
//          listView = (View) convertView;
//      } 
//       }

        return convertView ;        
    }   
    private Bitmap DownloadImage(String URL)
    {        
//      System.out.println("image inside="+URL);
        Bitmap bitmap = null;
        InputStream in = null;        
        try {
            in = OpenHttpConnection(URL);
            bitmap = BitmapFactory.decodeStream(in);
            in.close();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
//        System.out.println("image last");
        return bitmap;                
    }
    private InputStream OpenHttpConnection(String urlString)
            throws IOException
            {
                InputStream in = null;
                int response = -1;

                URL url = new URL(urlString);
                URLConnection conn = url.openConnection();

                if (!(conn instanceof HttpURLConnection))                    
                    throw new IOException("Not an HTTP connection");

                try{
                    HttpURLConnection httpConn = (HttpURLConnection) conn;
                    httpConn.setAllowUserInteraction(false);
                    httpConn.setInstanceFollowRedirects(true);
                    httpConn.setRequestMethod("GET");
                    httpConn.connect();

                    response = httpConn.getResponseCode();                
                    if (response == HttpURLConnection.HTTP_OK) 
                    {
                        in = httpConn.getInputStream();                                
                    }                    
                }
                catch (Exception ex)
                {
                    throw new IOException("Error connecting");            
                }
                return in;    
    }
}

我哪里出错了?请提前帮助我...

1 个答案:

答案 0 :(得分:0)

我解决了..问题在于

Thread thread = new Thread();

而不是我做完了:

Thread thread = new Thread(Myactivity.this);

现在加载后指标正在消失。谢谢所有人..