java - 如何在运行时在 Android 中显示图像并将单选按钮作为列表中的单选组

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

//更新代码到此结束

我必须在我的 ListView 中显示 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以及。但是想不出太多可以满足我的要求。

最佳答案

所以。在您的类中创建 RadioGroup RadioGroup radioGroup = new RadioGroup(this); 并在填充内容时简单地向其添加单选按钮 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();
}

关于java - 如何在运行时在 Android 中显示图像并将单选按钮作为列表中的单选组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8223180/

相关文章:

java - 当应用程序关闭或在后台时,Admob 仍在发出请求?

java - 在 Android Studio 中使用线程崩溃 Java apk

java - Android 创建自定义注释

android - 内部的 Horizo​​ntalScrollView 无法单击 ListView 项目

java - Spring MVC : manage Exceptions

java - 如何使用 Spring Integration 从 Java bean 正确创建 RabbitMQ 自定义消息?

android - 使用 Robolectric 测试 PreferenceActivity

android - 使用 ID 删除多行?

android - 在 ArrayAdapter 的 getView() 方法中只下载一次图像

android - 调试 SimpleCursorAdapter