无法在Android中的自定义微调器中设置图片?

时间:2014-05-07 15:07:02

标签: android android-spinner

我只是想问一下,我只是想在一个微调器中从textview获取一个文本,当文本视图设置为A时,我想将它设置为a.png。我一直试图用if语句检查,但看起来代码不起作用,有人可以帮助我吗?这是我的代码,我在下面的代码中给出了评论。

 public class Front_end_akreditasi extends Activity implements OnItemSelectedListener
  {
     //this is database class
     private DBDataSource dataSource;

 // Spinner element
 Spinner spinner;

     private List<String> values;

 JSONArray temp;
 String[] mArray;

     @Override
    public void onCreate(Bundle savedInstanceState)
    {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.front_end_akreditasi);

          dataSource = new DBDataSource(this);

          dataSource.open();

              //getting data from database
          values = dataSource.getnama_Akreditasi();

          temp = new JSONArray(values);
          try 
          {
                    //this is for parsing data from database to an Array
        mArray = temp.join(",").split(",");
      } 
          catch (JSONException e) 
          {
            // TODO Auto-generated catch block
            e.printStackTrace();
       }
          spinner   = (Spinner) findViewById(R.id.spinner_show);


          spinner.setAdapter(new MyAdapter(this, R.layout.custom_spinner, mArray));

    }
       public class MyAdapter extends ArrayAdapter<String> {

        public MyAdapter(Context ctx, int txtViewResourceId, String[] objects) 
        {
            super(ctx, txtViewResourceId, objects);
        }
        @Override
        public View getDropDownView(int position, View cnvtView, ViewGroup prnt) {
            return getCustomView(position, cnvtView, prnt);
        }

        @Override
        public View getView(int pos, View cnvtView, ViewGroup prnt) {
            return getCustomView(pos, cnvtView, prnt);
        }

        public View getCustomView(int position, View convertView, ViewGroup parent) 
        {
            LayoutInflater inflater = getLayoutInflater();
             View mySpinner = inflater.inflate(R.layout.custom_spinner, parent,false);
               TextView main_text = (TextView) mySpinner.findViewById(R.id.text_main_seen);
            main_text.setText(mArray[position]);
               TextView subSpinner = (TextView) mySpinner .findViewById(R.id.sub_text_seen);

        ImageView left_icon = (ImageView) mySpinner.findViewById(R.id.left_pic);
                            //In Here i just want to get a text from main_text, but the
                            //code isn't working
            if(main_text.getText().equals("A"))
            {
                left_icon.setImageResource(R.drawable.home);
            }
            return mySpinner;


        }
  }

任何人都可以帮我解决这个问题吗?需要任何帮助,谢谢你们:D

这里是custom_spinner.xml文件

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="3px" >

<ImageView
    android:id="@+id/left_pic"
    android:layout_width="100px"
    android:layout_height="80px"
    android:background="@drawable/ic_launcher" />

<TextView
    android:id="@+id/text_main_seen"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5px"
    android:layout_marginTop="2px"
    android:layout_toRightOf="@+id/left_pic"
    android:padding="3px"
    android:text="JMD Group"
    android:textColor="#0022ee"
    android:textSize="22px"
    android:textStyle="bold" />

<TextView
    android:id="@+id/sub_text_seen"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/text_main_seen"
    android:layout_marginLeft="5dip"
    android:layout_toRightOf="@+id/left_pic"
    android:padding="2px"
    android:text="beyond the expectations..."
    android:textColor="#777777" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

我认为您的问题是您没有将TextView的值传递给StringgetText会返回CharSequence并且您正在尝试将其与String。尝试将main_text.getText().equals("A"))替换为main_text.getText().toString().equals("A"))

我已尝试过您的代码,main_text.getText().equals("A"))会返回falsemain_text.getText().toString().equals("A"))会返回True。在这里你有概念证明。

Test