如何在运行时和Android中显示Android中的图像将单选按钮设为列表中的无线电组

时间:2011-11-22 07:06:16

标签: java android listview

根据要求修改了更新的代码:

int MovieNum=children.size();  
                    String[] MovieName=new String[MovieNum]; 
                    String[] MovieCover=new String[MovieNum];                                       

                    RadioGroup rg = new RadioGroup (this); 
                    rg = (RadioGroup) findViewById(R.id.radioGroup1);
                    RadioButton rb[]= new RadioButton[children.size()];

                    //LinearLayout layout = (LinearLayout) findViewById(R.id.layout);                
                    //LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
                    //layout.addView(rg, p);

                    for (int i = 0; i < children.size(); i++) 
                    {   
                        Element movieAtt = (Element)doc.getRootElement().getChild("movies").getChildren("movie").get(i);                                            
                        MovieName[i]=movieAtt.getAttributeValue( "Title" );
                        MovieCover[i]=movieAtt.getAttributeValue( "cover" );
                        ShowTime[i]=movieAtt.getAttributeValue( "showtime" );


                TweetText+=" I will see "+movieAtt.getAttributeValue("Title");
            System.out.println(TweetText);
                        //ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.rowlayout, R.id.label, MovieName);
                        //setListAdapter(adapter);
                        //String item = (String) getListAdapter().getItem(position);

                        rb[i] = new RadioButton(this);
                        rb[i].setText(movieAtt.getAttributeValue("Title"));
                        rg.addView(rb[i]);  

                        //Calling this func to get Images
                        //LoadImageFromWebOperations(movieAtt.getAttributeValue( "cover" ));

                    }                     
                }               
        } 
        catch (MalformedURLException e1) 
        {
            e1.printStackTrace();
            System.out.println("ErrorThrowedIs: "+e1);
            Toast.makeText(getBaseContext(), e1.toString(),5);
        } 

    }

    public static Drawable LoadImageFromWebOperations(String url) 
    {
        try 
        {
            InputStream is = (InputStream) new URL(url).getContent();
            Drawable d = Drawable.createFromStream(is, "src name");
            return d;
        } 
        catch (Exception e) 
        {
            return null;
        }
    }

//更新的代码在此处结束

我必须在列表视图中显示3件事。一个是对应于每一行的单选按钮。我不能使用Radio组,因为我不知道动态生成的列表长度。所以我不能把它设置为Radio组。 我想知道我怎么能这样做。

我正在做的是,我想在同一个列表中为从运行时获得的路径创建的每一行获取一个图像。

所以在我的节目中,我会得到这些值:

for (int i = 0; i < mvLen; i++) 
                    {   
                        Element movieAtt = (Element)doc.getRootElement().getChild("movies").getChildren("movie").get(i);                                            
                        MovieName[i]=movieAtt.getAttributeValue( "Title" );
                        MovieCover[i]=movieAtt.getAttributeValue( "cover" );
                        ShowTime[i]=movieAtt.getAttributeValue( "showtime" );

现在显然这是一个电影的字符串数组

MovieName中的名称和该影片封面的URL字符串数组。我希望在电影名称前面显示所有这些图像。我的列表适配器和相应的XML文件如下所示。任何人都可以建议相同的更改/提示。我也一直在阅读http://www.vogella.de/articles/AndroidListView/article.html。但无法满足我的要求。

2 个答案:

答案 0 :(得分:1)

因此。在您的班级RadioGroup radioGroup = new RadioGroup(this);中创建RadioGroup,只需在填充内容radioGroup.addView(radioButton);时添加单选按钮

要点:

RadioGroup radioGroup = new RadioGroup(this);
//Cycle begin
      RadioButton rButton = (RadioButton)findViewById(R.id.my_radiobutton);
      // OR
      RadioButton radioButton = new RadioButton(this);
      radioButton .setText(R.string.radio_group_snack);
      radioButton .setId(R.id.snack);
      radioGroup.addView(rButton);
      radioGroup.addView(radioButton);
//Cycle end

自定义适配器和getview方法的示例(它是100%工作代码):

    public class MyCustomAdapter extends ArrayAdapter<String> {

    public MyCustomAdapter(Context context, int textViewResourceId,
            String[] objects) {
        super(context, textViewResourceId, objects);

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        String[] users = getUsers(); //it returns list of users in String array
        LayoutInflater inflater=getLayoutInflater();
        View row=inflater.inflate(R.layout.main, parent, false);
        TextView label=(TextView)row.findViewById(R.id.label); //TextView decalred in my layout xml file
        label.setText(users[position]);
        ImageView icon=(ImageView)row.findViewById(R.id.icon); //ImageView decalred in my layout xml file
        byte[] bb = getAvatar(users[position]); //some method where I get image for user. It not interesting for us so I cut it from here...
        if(bb != null && bb.length != 0){
            icon.setImageBitmap(BitmapFactory.decodeByteArray(bb, 0, bb.length)); //SET IMAGE FOR ROW
            icon.setVisibility(View.VISIBLE); //SET VISIBILITY OF IMAGE
        }
        //RIGHT HERE YOU CAN MANIPULATE WITH YOUR RADIOBUTTONS. FOR EXAMPLE:
        RadioButton rButton = (RadioButton)findViewById(R.id.my_radio_button);
        radioGroup.addView(rButton); //WHERE radioGroup IS RADIOGROUP YOU INITIALIZED BEFORE;
        return row;

    }

    }


}

这是我的onCreate方法:

    @Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    db.open();
    String[] users = db.getUsersList();
    ListView listView = getListView();
    this.setListAdapter(new MyCustomAdapter(this, R.layout.main, users));
    db.close();
}

答案 1 :(得分:0)

对于列表中的每一行,获取radioButton并将其添加到RadioGroup。 您可能需要预先创建所有radioButton并将它们存储在列表中。根据位置将它们放在ContentView中