Я пытаюсь использовать спиннер с адаптером массива, и он будет остановлен безо всяких ошибок. Может ли кто-нибудь сказать мне ошибку в коде, который мне нужен? Появляется изображение с текстом.пожалуйста, я новичок в разработке приложений для Android
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="47dp"
android:spinnerMode="dropdown">
</Spinner>
</RelativeLayout>
spinner_item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/i1"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/image" />
<TextView
android:id="@+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="15dp"
android:text="Orange"
android:textSize="30dp"
android:textStyle="bold" />
</LinearLayout>
Main.java
package com.safaa.user.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.Toast;
public class MainActivity extends AppCompatActivity {
String[] names={"apple","panana","orange"};
int[]images={R.drawable.image,R.drawable.image3,R.drawable.image2
};
CustomAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner=findViewById(R.id.spinner);
adapter=new CustomAdapter(this,names,images);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(),names[position],Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
CustomAdapter
package com.safaa.user.spinner;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomAdapter extends ArrayAdapter<String>{
Context context;
String []names;
int image[];
public CustomAdapter(Context context, String[]names,int[]image) {
super(context, R.layout.spinner_item,names);
this.image=image;
this.names=names;
this.context=context;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row =inflater.inflate(R.layout.spinner_item,null);
TextView t1=row.findViewById(R.id.txt1);
ImageView i1=row.findViewById(R.id.i1);
t1.setText(names[position]);
i1.setImageResource(image[position]);
return row;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row =inflater.inflate(R.layout.spinner_item,null);
TextView t1=row.findViewById(R.id.txt1);
ImageView i1=row.findViewById(R.id.i1);
t1.setText(names[position]);
i1.setImageResource(image[position]);
return row;
}
}