Can';在Android的自定义微调器中设置图片?
我只是想问一下,我只是尝试在微调器中从textview获取文本,当文本视图设置为a时,我想将其设置为a.png。我一直在尝试检查if语句,但代码似乎不起作用,有人能帮我吗?这是我的代码,我在下面的代码中给出了注释Can';在Android的自定义微调器中设置图片?,android,android-spinner,Android,Android Spinner,我只是想问一下,我只是尝试在微调器中从textview获取文本,当文本视图设置为a时,我想将其设置为a.png。我一直在尝试检查if语句,但代码似乎不起作用,有人能帮我吗?这是我的代码,我在下面的代码中给出了注释 public class Front_end_akreditasi extends Activity implements OnItemSelectedListener { //this is database class private DBDataSour
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;
}
}
public类前端\u end\u akreditasi扩展活动实现OnItemSelectedListener
{
//这是数据库类
私有数据库数据源;
//旋转元件
纺纱机;
私有列表值;
JSONArray温度;
字符串[]mArray;
@凌驾
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(右布局、前端和akreditasi);
dataSource=新的DBDataSource(此);
dataSource.open();
//从数据库获取数据
values=dataSource.getnama_Akreditasi();
temp=新JSONArray(值);
尝试
{
//这用于将数据从数据库解析到数组
mArray=临时连接(“,”)。拆分(“,”);
}
捕获(JSONException e)
{
//TODO自动生成的捕捉块
e、 printStackTrace();
}
微调器=(微调器)findViewById(R.id.spinner\u show);
setAdapter(新的MyAdapter(this,R.layout.custom_spinner,mArray));
}
公共类MyAdapter扩展了ArrayAdapter{
公共MyAdapter(上下文ctx、int-txtViewResourceId、字符串[]对象)
{
超级(ctx、txtViewResourceId、对象);
}
@凌驾
公共视图getDropDownView(内部位置、视图cnvtView、视图组prnt){
返回getCustomView(位置、cnvtView、prnt);
}
@凌驾
公共视图getView(内部位置、视图cnvtView、视图组prnt){
返回getCustomView(pos、cnvtView、prnt);
}
公共视图getCustomView(int位置、视图转换视图、视图组父视图)
{
LayoutInflater充气机=getLayoutInflater();
查看mySpinner=充气器。充气(R.layout.custom_微调器,父项,false);
TextView main\u text=(TextView)mySpinner.findViewById(R.id.text\u main\u seed);
main_text.setText(mArray[position]);
TextView subscreen=(TextView)mySpinner.findViewById(R.id.sub_text_seen);
ImageView left_icon=(ImageView)mySpinner.findViewById(R.id.left_pic);
//在这里,我只想从主文本中获取一个文本,但是
//代码不起作用
if(main_text.getText().equals(“A”))
{
左图标setImageResource(R.drawable.home);
}
返回mySpinner;
}
}
有人能帮我解决这个问题吗?需要任何帮助吗,谢谢你们
这里是定制的_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>
我认为您的问题在于您没有将
TextView
的值传递给String
,getText
返回一个CharSequence
,您试图将其与String
进行比较。尝试用main\u text.getText().equals(“a”)替换main\u text.getText().toString().equals(“a”)))
我试过你的代码,用main\u text.getText().equals(“A”)
返回false
,但用main\u text.getText().toString().equals(“A”)
返回True
。这是概念证明
你能把R.layout.custom\u微调器的XML放在你的问题中吗?@davidchristopherrynolds:我已经放了?你能帮我吗?你试过main\u text.getText().toString.equals(“A”)了吗
?@algui91:它仍然不起作用,我已经试过了too@ndr_sd然后,在您的文本视图中
没有A
,检查您是否有隐藏字符,或者小写的A。如果
,程序停止?或者干脆不输入?