为什么我的文字不会出现在微调框中的图像视图中?

时间:2017-04-22 10:39:50

标签: java android textview spinner

enter image description here我的文字没有出现在图片视图中我不知道有谁可以纠正我。 下面的代码是主要的java活动代码我试图添加一个文本视图,这是无效的 Mainactivity.java

 package com.example.xainshah.spinner;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;


public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

    String countryNames[]={"pakistan","india","Bangladesh","Sri Lanka"};
    int flags[]={R.drawable.pak,R.drawable.ind,R.drawable.ban,R.drawable.sri};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Spinner spin=(Spinner)findViewById(R.id.spinner);
        spin.setOnItemSelectedListener(this);
        CustomAdapter customAdapter=new CustomAdapter(getApplicationContext(),flags,countryNames);
        spin.setAdapter(customAdapter);

    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
}

客户适配器类代码

CustomAdapter.java

package com.example.xainshah.spinner;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Created by Xain Shah on 4/22/2017.
 */

public class CustomAdapter extends BaseAdapter{
    Context context;
    int flags[];
    String countryNames[];
    LayoutInflater Inflater;
    public CustomAdapter(Context applicationcontext,int flags[],String countryNames[] ){
        this.context=applicationcontext;
        this.flags=flags;
        this.countryNames=countryNames;
        Inflater=(LayoutInflater.from(applicationcontext));
    }

    @Override
    public int getCount() {
        return flags.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView=Inflater.inflate(R.layout.spinner_layout,null);
        ImageView icon=(ImageView)convertView.findViewById(R.id.imageView);
        TextView names=(TextView)convertView.findViewById(R.id.textView2);
        icon.setImageResource(flags[position]);
        names.setText(countryNames[position]);
        return convertView;
    }
}

spinner_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:padding="10dp"
        app:srcCompat="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="42dp"
        android:layout_weight="1"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

</LinearLayout>

1 个答案:

答案 0 :(得分:1)

代码:

  public class CustomAdapter extends ArrayAdapter<String> {

        private final Activity context;
        private final String[] countryNames;
        private final Integer[] imgid;

        public CustomAdapter(Activity context, Integer flags[],String countryNames[]) {
            super(context, R.layout.spinner_layout, countryNames);
            // TODO Auto-generated constructor stub

            this.context=context;
            this.countryNames =countryNames;
            this.imgid=flags;
        }

        public View getView(int position,View view,ViewGroup parent) {
            LayoutInflater inflater=context.getLayoutInflater();
            View rowView=inflater.inflate(R.layout.spinner_layout, null,true);

            TextView txtTitle = (TextView) rowView.findViewById(R.id.textView2);
            ImageView imageView = (ImageView) rowView.findViewById(R.id.imageView);

            txtTitle.setText(countryNames[position]);
            imageView.setImageResource(imgid[position]);
            return rowView;

        }
        @Override
        public View getDropDownView(int position, View convertView,ViewGroup parent) {
            return getView(position, convertView, parent);
        }

        @Override
        public String getItem(int position) {
            return String.valueOf(countryNames.toString());
        }
 }

使用:

来调用它
CustomAdapter customAdapter=new CustomAdapter(MainActivity.this,flags,countryNames);

布局用途:

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:padding="10dp"
        app:srcCompat="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="42dp"
        android:layout_weight="1"
        android:gravity="center|left"
        android:text="FDFDFDFD"
        android:textSize="15sp"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1"
        android:textColor="#000000" />

</LinearLayout>