自定义微调器显示不需要的文本

时间:2014-01-04 10:40:18

标签: android android-spinner

您好我在微调器中为一行创建了一个只有一个ImageView的自定义微调器。     我的问题是它显示了模型类的整个包名。我为微调器实现了一个自定义适配器,并创建了一个模型类,用于存储适配器所需的数据。我在下面的代码中给出了详细信息。正如您可以看到屏幕截图中显示的文本,需要有关如何删除它的建议。谢谢。

以下是我的应用的截图:

Here is the screenshot of my app

显示文字的另一个屏幕截图:

Another screenshot where the text appears

以下是我的代码:

我的微调器CumpanyAdapter.class的自定义适配器

    package axe.madmax.ajaxbrc;

import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;

public class CumpanyAdapter extends ArrayAdapter<CompanyModel> {

    private Activity activity;
    private ArrayList<CompanyModel> data;

    public CumpanyAdapter(Context context, int textViewResourceId, 
            ArrayList<CompanyModel> data) {
        super(context, textViewResourceId, data);
        // TODO Auto-generated constructor stub
        activity = (Activity) context;
        this.data = data;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {   // Ordinary view in Spinner, we use android.R.layout.simple_spinner_item

        return super.getView(position, convertView, parent);   
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent)
    {   // This view starts when we click the spinner.
        View row = convertView;
        LayoutInflater inflater = activity.getLayoutInflater();
            row = inflater.inflate(R.layout.companyrow, parent, false);


     // get each model object and inject into the spinner row
        CompanyModel item = (CompanyModel)data.get(position);



        if(item != null)
        {   // Parse the data from each object and set it.
            ImageView myLogo = (ImageView) row.findViewById(R.id.imageView1);

            if(myLogo != null)
            {
                myLogo.setBackgroundResource(item.getImage());
            }

        }

        return row;
    }

}

CumpanyModel.class

这是保存自定义适配器类数据的数据或模型类。

    package axe.madmax.ajaxbrc;

public class CompanyModel {
public CompanyModel(String name, int image) {

        this.name = name;
        Image = image;
    }
private String name;
/**
 * Modela class for Custom Spinner Class
 */
public int Image;
/**
 * @return the name
 */
public String getName() {
    return name;
}
/**
 * @param name the name to set
 */
public void setName(String name) {
    this.name = name;
}
/**
 * @return the image
 */
public int getImage() {
    return Image;
}
/**
 * @param image the image to set
 */
public void setImage(int image) {
    Image = image;
}

}

我的mainactivity.class

这是主要活动的一部分

    public class MainActivity extends Activity {
Spinner dthSpinner;
// Cusstom Adapter and Model class for the Spinner
      public ArrayList<CompanyModel> CustomCompanyListArr = new ArrayList<CompanyModel>();
      CumpanyAdapter adapterC;

 @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

// Set data in arraylist
    setData();

    //Spinner

    dthSpinner = (Spinner) findViewById(R.id.dthSpinner);
    dthSpinner.setPromptId(R.string.pstr);


   // Create custom adapter
   adapterC = new CumpanyAdapter(activity, android.R.layout.simple_spinner_item, CustomCompanyListArr);
   // set custom adapter to the dthspinner
   dthSpinner.setAdapter(adapterC);
//Dth Spinner setting Appropriate Adapter
    dthSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> dth, View arg1, int position,
                long arg3) {
            // TODO Auto-generated method stub

            CompanyModel c = (CompanyModel)dthSpinner.getSelectedItem();
            dthSpinner.setBackgroundResource(c.getImage());
            if(c.getName().equals("Hathway")){


                dthChnSearch.setAdapter(adapterHW);
            }else if(c.getName().equals("Tatasky")){


                dthChnSearch.setAdapter(adapterTS);
            }else if(c.getName().equals("Sun Dth")){


                dthChnSearch.setAdapter(adapterSun);
            }else if(c.getName().equals("Dish TV")){


                dthChnSearch.setAdapter(adapterDh);
            }else if(c.getName().equals("Videocon DTH")){


                dthChnSearch.setAdapter(adapterVc);
            }else if(c.getName().equals("Big TV")){


                dthChnSearch.setAdapter(adapterBig);
            }else if(c.getName().equals("Airtel DTH")){


                dthChnSearch.setAdapter(adapterAt);
            }else if(c.getName().equals("Digicable")){

                  //  dthChnSearch.setAdapter(adapterDc);
            }else{
                dthSpinner.setBackgroundResource(R.drawable.spinnerdth);
            }

        }

        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }   
    });
}
private void setData(){

    CustomCompanyListArr.add(new CompanyModel("Blank", R.drawable.btn_dropdown_normal));
    CustomCompanyListArr.add(new CompanyModel("Airtel DTH", R.drawable.adth_15050));
    CustomCompanyListArr.add(new CompanyModel("Big TV", R.drawable.bigtv_15050));
    CustomCompanyListArr.add(new CompanyModel("Digicable", R.drawable.digicable_15050));
    CustomCompanyListArr.add(new CompanyModel("Dish TV", R.drawable.dishtv_15050));
    CustomCompanyListArr.add(new CompanyModel("Hathway", R.drawable.hathway_15050));
    CustomCompanyListArr.add(new CompanyModel("Sun Dth", R.drawable.sundth_15050));
    CustomCompanyListArr.add(new CompanyModel("Tatasky", R.drawable.tatasky_15050));
    CustomCompanyListArr.add(new CompanyModel("Videocon DTH", R.drawable.videocondth_15050));   

}
}

我的companyrow.xml文件

这是一行,包含一个出现在微调器下拉菜单中的ImageView。

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

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        tools:ignore="ContentDescription" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

将此添加到您的CompanyModel

@Override public String toString() {
    return getName();
}