D
Here I bring you a code made 100% by me, I will explain to you every peace as you believe it, originally believes it to use it on a custom listView, but it also serves in your case.I didn't really understand your code written in kotlin.activity_main.xml<android.support.constraint.ConstraintLayout 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"
android:padding="20dp"
tools:context=".MainActivity">
<Spinner
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main_activity_list_view" />
<!-- <ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main_activity_list_view"/>
-->
</android.support.constraint.ConstraintLayout>
MainActivity.java public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Spinner list = findViewById(R.id.main_activity_list_view);
final String[] headers = {
"tips/importacia/header.json",
"tips/porque/header.json",
"tips/quehacer/header.json",
"tips/tresr/header.json",
"tips/ahorra/header.json",
"tips/hacerhuertos/header.json",
"tips/haceresquejes/header.json",
};
final OptionAdapter adapter = new OptionAdapter(getApplicationContext(), headers);
list.setAdapter(adapter);
/*list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
final String path = adapter.getPaths().get(headers[i]);
final Intent intent = new Intent(MainActivity.this, WebActivity.class);
intent.putExtra("path", path);
startActivity(intent);
}
});*/
}
}
Here you have to see that we get the Spinner for your ID, then in my case I create an array that contains the internal route of the files .json which contains the information(file of the image, title and description), which would be the information we will occupy to create the customized view.Don't look at the commented part, that was for the original listView, with Spinner will make you mistake.OptionAdapter.javapublic class OptionAdapter extends BaseAdapter {
private final String[] headers;
private final HashMap<String, String> paths;
private final Context context;
private final LayoutInflater inflter;
public OptionAdapter(Context context, String[] headers) {
this.headers = headers;
this.paths = new HashMap<>(headers.length);
inflter = (LayoutInflater.from(context));
this.context = context;
}
public HashMap<String, String> getPaths() {
return paths;
}
@Override
public int getCount() {
return headers.length;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@SuppressLint({"ViewHolder", "InflateParams"})
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
if (view != null) return view;
view = inflter.inflate(R.layout.option, null);
final ImageView icon = view.findViewById(R.id.option_icon);
final TextView title = view.findViewById(R.id.option_title);
final TextView preview = view.findViewById(R.id.option_preview);
try {
String auxPath;
final Gson gson = new Gson();
final AssetManager assets = context.getAssets();
final Header instance = gson.fromJson(new InputStreamReader(assets.open(headers[i]), StandardCharsets.UTF_8), Header.class);
title.setText(instance.title);
preview.setText(instance.preview);
final File parents = new File(headers[i]).getParentFile();
auxPath = new File(parents, instance.path).toString();
paths.put(headers[i], auxPath);
auxPath = new File(parents, instance.icon).toString();
icon.setImageDrawable(Drawable.createFromStream(assets.open(auxPath), null));
} catch (Throwable ignored) {
}
return view;
}
public static class Header {
String icon, title, preview, path;
}
}
First of all, the builder, we keep everything in internal variables and we get the LayoutInflater with the context.Then as we can see, the most important method we have is getView(...)What exactly is this method?Well, my friend that method is that of all magic, I'll return a "view" view that's basically where we want what we want comes in, it's the point because that's the view that looks at the spinner, each of the spinner options will be obtained from this method.Now is where to play in LayoutInflater. .
will allow us to create a view from a file .xml already existing. .
Then we'll get your internal views and assign the data. .
also comes into play the other data that will help us get those data.In my case I decided to do it that way to pass the file route .json and read them, you can do something easier and it is to pass an array of a custom class that content the title, id of the image, and the description. .But something's missing, I'll leave the file here. .xml to inflate the view. .
Option.xml<LinearLayout
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="wrap_content"
android:orientation="horizontal"
android:background="@color/option_background"
>
<android.support.v7.widget.AppCompatImageView
android:padding="5dp"
android:id="@+id/option_icon"
android:layout_width="200dp"
android:layout_height="100dp"
android:layout_weight="1"
app:srcCompat="@drawable/error" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:textAlignment="center"
android:textStyle="bold"
android:id="@+id/option_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/error" />
<TextView
android:id="@+id/option_preview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/error" />
</LinearLayout>
</LinearLayout>
this too, but to be clear, the files .xml Must be in. res/layoutEditI don't understand much about your code, I just program in java, up it's like I would use java, but I don't see you having a file. .xml to inflate, I don't know what to do with you. Layout InflaterMaybe that's your problem.