I am a new Developer, I tried using adapter on my project,android app stopped unfortulately without showing any error. Tried grid view, image view and spinner, but in all cases my app stopped unfortunately without showing any error Here I tried spinner with custom adapter Tried array adapter in one class, it ran but I need a Separate class for adapter and call it by another class
MainActivity
package com.safaa.user.last_app;
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 [] fruit={
"apple","panana","orange"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner=findViewById(R.id.spinner);
CustomSpinnerAdapter spinnerAdapter=new CustomSpinnerAdapter(this,fruit);
spinner.setAdapter(spinnerAdapter);
spinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int i, long id) {
Toast.makeText(MainActivity.this,fruit[i],Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView<?>adapterView){
}
});
}
}
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">
<android.support.v7.widget.AppCompatSpinner
android:layout_width="200dp"
android:layout_height="50dp"
android:id="@+id/spinner"
android:layout_centerHorizontal="true">
</android.support.v7.widget.AppCompatSpinner>
</RelativeLayout>
CustomSpinnerAdapter
package com.safaa.user.last_app;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.zip.Inflater;
import static com.safaa.user.last_app.R.layout.spinner_row;
public class CustomSpinnerAdapter extends BaseAdapter {
Activity activity;
String []fruit;
LayoutInflater inflater;
@SuppressLint("ServiceCast")
public CustomSpinnerAdapter (Activity activity, String [] fruit){
this.activity=activity;
this.fruit=fruit;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return fruit.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View row = inflater.inflate(R.layout.spinner_row,null);
TextView textView=(TextView)row.findViewById(R.id.txt1);
textView.setText(fruit[i]);
return row;
}
}
spinner_row.xml
<?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="40dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/txt1"
android:layout_marginLeft="10dp"
/>
</LinearLayout>
You need to update those two method :
@Override
public String getItem(int position) {
return fruit[position];
}
@Override
public long getItemId(int position) {
return position;
}
also you need to use ItemSelectedListener
with Spinner
not ItemClickListener
change to the code below :
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(MainActivity.this,fruit[i],Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});