【问题标题】:How i get the image in the same row of selected radio button?我如何在所选单选按钮的同一行中获取图像?
【发布时间】:2014-05-17 11:30:09
【问题描述】:

我有自定义列表,每一行都包含图像、文本视图和单选按钮而不是单选组。 我只选择一个单选按钮。我想获取所选单选按钮同一行中的图像。然后我用这张图片设置背景 请帮帮我。

//rowadapter
public class rowadapter extends ArrayAdapter<String> {

private final Activity context;
int layoutResourceId;    
private final String[] web;
private final Integer[] imageId;
int selectedPosition = -1;
SharedPreferences sharedPref;

public rowadapter(Activity context,String[] web, Integer[] imageId) {
    super(context,R.layout.item_listview, web);
    this.context = context;
    this.web = web;
    this.imageId = imageId;
     sharedPref = context.getSharedPreferences("position",Context.MODE_PRIVATE);

}
public View getView(final int position, View row, ViewGroup parent) 
{
    LayoutInflater inflater = ((Activity)context).getLayoutInflater();
    backgroundholder holder = null;
    View rowView=row;

        rowView= inflater.inflate(R.layout.item_listview, null, true);

        TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
        final ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
        txtTitle.setText(web[position]);
        imageView.setImageResource(imageId[position]);
        holder = new backgroundholder();    
        holder.radiobutton = (RadioButton)rowView.findViewById(R.id.radiobutton);
        holder.radiobutton.setChecked(position == selectedPosition);
        holder.radiobutton.setTag(position);
        int checkedpos=sharedPref.getInt("position",-1);
        if(checkedpos==position)
        {
           holder.radiobutton.setChecked(true);
        }


        holder.radiobutton.setOnClickListener(new View.OnClickListener() {     
        public void onClick(View view)
              {
                  selectedPosition = (Integer)view.getTag();
                   RadioButton radio = (RadioButton)view;
                   if(radio.isChecked())
                   {
                   Editor editor=sharedPref.edit();
                   editor.putInt("position", selectedPosition);
                   editor.commit();
                   }
                   Intent i=new Intent(getContext(), Preferences.class);
                   context.startActivity(i);
                  notifyDataSetInvalidated();

              }

          });        

   return rowView;


}
static class backgroundholder
{

    RadioButton radiobutton;
}

}
//object class
import android.widget.RadioButton;


public class Item {
String txt;
Integer drawable;
RadioButton radiobutton;
boolean checked;
public Item(String txt,Integer drawable,RadioButton radiobutton)
{
this.txt=txt;
this.drawable=drawable;
this.radiobutton=radiobutton;
}

public String gettxt()
{
return txt;

}
public Integer getInteger()
{
return drawable;

 }

public boolean ischecked()
{
return checked;

 }
 public void setchecked(boolean checked)
{
this.checked=checked;
 }
 }

【问题讨论】:

  • 采用线性布局horizontal并在一行中添加所有内容
  • 我不明白答案..
  • 您在 XML 文件中使用哪种布局?在此处发布 xml 文件
  • 没有背景不是 xml 文件...我想先获取图像然后我可以设置背景..现在的问题是从所选单选按钮的同一行获取图像

标签: android radio-button imageview adapter


【解决方案1】:

如果你知道那一行的 textview 值是多少,你可以使用这个方法:

if (listview.getItemAtPosition(position).toString() == "item 1") {
imageview.setImageResource(R.drawable.drawable1)
} else if (listview.getItemAtPosition(position).toString() == "item 2") {
imageview.setImageResource(R.drawable.drawable2)
}

编辑: 现在我再次阅读了您的问题,并发现您想通过单击每个列表视图项目来更改背景。对吗?

因为你根本不需要 getview。

首先你应该实现listview onItemClick,然后根据所选行的位置,改变背景。

类似这样的:

在你的 oncreat 中:

list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                long arg3) {

            if (listview.getItemAtPosition(position).toString() == "item 1") {
              //put your change background code here
             } else if (listview.getItemAtPosition(position).toString() == "item 2") {
              //put your change background code here
}
        }
    });

为了改变背景,你可以使用这个代码:

layout.setBackgroundDrawable(getResources().getDrawable(R.drawable.drawable));

【讨论】:

  • 是的,我可以得到 textview 值.. 但我不知道我在哪里写这部分??
  • @user3631823 在您的 getView() 方法中。在'txtTitle.setText(web[position]);'下
  • 它不起作用...我在其他活动中确定的列表,我让持有者持有列表视图..但它也不起作用。
  • 提示:我在画布上画画
  • @user3631823 这是您更改背景的选择。如果我的回答帮助您找到所选行,请接受。
猜你喜欢
  • 2015-06-26
  • 2021-04-28
  • 1970-01-01
  • 2012-04-16
  • 1970-01-01
  • 2016-06-21
  • 2021-02-03
  • 2011-07-28
相关资源
最近更新 更多