iT邦幫忙

0

Spinner Toast問題

  • 分享至 

  • xImage
import androidx.appcompat.app.AppCompatActivity;

        import android.content.DialogInterface;
        import android.os.Bundle;
        import android.view.View;
        import android.view.ViewGroup;
        import android.widget.AdapterView;
        import android.widget.BaseAdapter;
        import android.widget.GridView;
        import android.widget.ImageView;
        import android.widget.ListView;
        import android.widget.Spinner;
        import android.widget.TextView;
        import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    class Data{
        int photo;
        String name;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Data[] ad=new Data[4];
        for(int i=0;i<ad.length;i++){
            ad[i]=new Data();
        }
        ad[0].name="腳踏車";
        ad[1].name="機車";
        ad[2].name="汽車";
        ad[3].name="公車";
        ad[0].photo=R.drawable.trans1;
        ad[1].photo=R.drawable.trans2;
        ad[2].photo=R.drawable.trans3;
        ad[3].photo=R.drawable.trans4;

        myAdapter ad1=new myAdapter(ad,R.layout.test1);
        Spinner sp=(Spinner)findViewById(R.id.spinner);
        sp.setAdapter(ad1);
        sp.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this,ad[position],Toast.LENGTH_SHORT).show();
            }
        });
public class myAdapter extends BaseAdapter{
        public    Data[] data;
        public   int view;
        public myAdapter(Data[] data,int view){
            this.data=data;
            this.view=view;
        }

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

        @Override
        public Data getItem(int position) {
            return data[position];
        }

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView=getLayoutInflater().inflate(view,parent,false);
            TextView txt1=(TextView)convertView.findViewById(R.id.text1);
            ImageView image1=(ImageView)convertView.findViewById(R.id.image1);
            txt1.setText(data[position].name);

            image1.setImageResource(data[position].photo);
            return convertView;
        }
    }
}

請問在

sp.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this,ad[position],Toast.LENGTH_SHORT).show();
            }
        });

會有錯要怎麼改,我想要選到其中一個時能顯示選到哪個.第一次發問,謝謝大家

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答