我是一名新开发人员,我尝试在我的项目上使用适配器,Android应用程序意外停止,未显示任何错误。 尝试使用网格视图,图像视图和微调器,但不幸的是,在所有情况下,我的应用程序都停止运行而未显示任何错误 在这里,我尝试了使用自定义适配器的微调器 在一个类中尝试过阵列适配器,它可以运行,但是我需要一个单独的适配器类,并由另一个类调用它
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>
答案 0 :(得分:0)
您需要更新这两种方法:
@Override
public String getItem(int position) {
return fruit[position];
}
@Override
public long getItemId(int position) {
return position;
}
还需要将ItemSelectedListener
与Spinner
一起使用,而不要ItemClickListener
更改为以下代码:
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) {
}
});