0

嗨,我对这个 android 很陌生..实际上我正在尝试在屏幕上显示 1 个标题和描述。我已经使用 Galleryview 完成了。我不知道这是否是正确的方法。

我的实际想法是..在galleryview中,如果我滑动屏幕下一个(标题和描述应该出现)但我不能这样做..所以我试图保留下一个和上一个选项来执行此操作..但这也是我不能做..我知道我做错了。请给我一些好办法。

我的活动代码:

public class NewsDescription extends Activity
{
//   private String url = "http://m.indiatoday.in/xml/stories/"+(String)context.getInstance().getAppVariable("storyurl");
     public void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.newsdesgallery);            
 Gallery gl=(Gallery)findViewById(R.id.GalleryNewsDesc);
            NewsDescriptionAdapter adapter =new NewsDescriptionAdapter(this);
            gl.setAdapter(adapter);   
}
}

我的适配器类:

public class NewsDescriptionAdapter extends  BaseAdapter
{
    private static Context contxt;


    String[] body= {};//new String[30];
    String[] heading= {};//new String[30];
    NewsDescriptionAdapter(Context conxt)
    {
//      System.out.println("inside cons");
        this.contxt=conxt;
        getelement();
    }

    public void getelement()
    {
//      System.out.println("Inside getElement");
        String[] url=context.getInstance().getselectedUrl();
//      System.out.println("After url");
//      System.out.println("count="+(String)context.getInstance().getAppVariable("count"));
        int count = Integer.parseInt((String)context.getInstance().getAppVariable("count"));
//      System.out.println("count="+count);
//      System.out.println("after count="+url[count]);
        String URL = "http://xxxx.in/xml/stories/"+url[count];  
//      System.out.println("url="+URL);
         TaplistingParser parser = new TaplistingParser();
//          try {
//              url=URLEncoder.encode(url, "UTF-8");
//          } catch (UnsupportedEncodingException e1) {
//              // TODO Auto-generated catch block
//              e1.printStackTrace();
//          }
            URL=URL.replace(" ","");
//          System.out.println("url="+url);
            String xml= parser.getXmlFromUrl(URL);
            Document doc=parser.getDomElement(xml);                 
            NodeList n1 = doc.getElementsByTagName("item");
            body = new String[n1.getLength()];
            heading = new String[n1.getLength()];
            for( int i = 0 ; i < n1.getLength(); i++ )
            {
//              HashMap<String, String> map = new HashMap<String, String>();
                Element e = (Element) n1.item(i);
                body[i]=parser.getValue(e, "body");
                heading[i]=parser.getValue(e, "headline");
//              map.put("Body", parser.getValue(e,"body"));             
//              menuItems.add(map);
            }
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return body.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return body[position];
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        // TODO Auto-generated method stub
//      System.out.println("body="+body[position]);
         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.newsdescriptionrow, null); 
            }        
         TextView next =(TextView)convertView.findViewById(R.id.nextnews);
//       final Gallery gal=(Gallery)convertView.findViewById(R.id.GalleryNewsDesc);

         next.setOnClickListener(new View.OnClickListener() 
         {          
            @Override
            public void onClick(View arg0) 
            {
                // TODO Auto-generated method stub
                System.out.println("Inside next");
                int count = Integer.parseInt((String)context.getInstance().getAppVariable("count"));
                count++;
                context.getInstance().setAppVariable("count", Integer.toString(count));     


            }
        });     

         TextView textViewhead = (TextView) convertView
                    .findViewById(R.id.name_DescHeading);
         textViewhead.setText(heading[position]);

             TextView textView = (TextView) convertView
                    .findViewById(R.id.name_Desclabel);
            textView.setText(body[position]);
        return convertView;
    }
}
4

1 回答 1

0

您不应再使用图库小部件,因为它在较新版本的 android 中已被弃用。相反,您可以使用来自 android 支持 jar 的 ViewPager。

你可以在这里找到一个例子: https ://github.com/nostra13/Android-Universal-Image-Loader

在上面的代码中寻找 ImagePagerActivity。您可以创建一个带有 imageview 和 textview 的自定义单元格进行描述(修改 item_pager_image xml)。让我知道事情的后续。

于 2012-12-07T06:34:40.550 回答