Answer the question
In order to leave comments, you need to log in
How to add a picture to the screen from the class?
I have a SQLite database in which each row of information that has its own _id has an image attached to it. The database currently has a column for the full name of these pictures. There are several such lines. This information and the picture is displayed when you click on one of the ListView elements, the pictures themselves are stored in the drawable folder. In my application, they are all displayed in the same way, so one fragment was created in which we fill in all the data to display them on the user's screen. I figured out the output of the text.
Question: how can I, having the name of the picture (or something else that you tell me, it's not a problem to change) in the database, get this picture on the screen? The output of information depends on which element of the ListView the user clicked on, so .setImageResource(R.drawable./title\) cannot be used here: you need to make sure that the one that matches the _id (aka the element of clicking on the ListView) is shown on the screen ) from the database.
Fragment code:
public class HistoryFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
Bundle bundle = getArguments();
String id = bundle.getString("id"); // получаем id, на который пользователь нажал
DataBaseHelper dataBaseHelper = new DataBaseHelper(getActivity());
HashMap<String,String> information = dataBaseHelper.getDescription(id); // получаем информацию из БД
FrameLayout frameLayout = (FrameLayout)inflater.inflate(R.layout.fragment_historyfragment, container, false);
TextView MainTitleView = frameLayout.findViewById(R.id.maintitle);
MainTitleView.setText(information.get("title")); // Задаём заголовок
TextView DescriptionOneView = frameLayout.findViewById(R.id.descriptionone);
DescriptionOneView.setText(information.get("descriptionOne")); // Задаём первое описание
ImageView pictureOneView = frameLayout.findViewById(R.id.pictureone); // Задаём первую картинку????
TextView DescriptionTwoView = frameLayout.findViewById(R.id.descriptiontwo);
DescriptionTwoView.setText(information.get("descriptionTwo")); // Задаём второе описание
ImageView pictureTwoView = frameLayout.findViewById(R.id.picturetwo); // Задаём вторую картинку?????
return frameLayout;
}
}
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HistoryFragment">
<!-- TODO: Update blank fragment layout -->
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/maintitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:textAlignment="center"
android:textAllCaps="true"
android:textSize="25sp"
android:textStyle="bold" />
<TextView
android:id="@+id/descriptionone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textAlignment="textStart"
android:textSize="15sp" />
<ImageView
android:id="@+id/pictureone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:padding="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
/>
<TextView
android:id="@+id/descriptiontwo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textSize="15sp"/>
<ImageView
android:id="@+id/picturetwo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:padding="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
/>
</LinearLayout>
</ScrollView>
</FrameLayout>
Answer the question
In order to leave comments, you need to log in
Well, assets are used for such purposes.
An assets folder is created in the main application folder (where src, build, config) - pictures are placed there. Their names should be stored in sqlite.
Then
InputStream ims = getAssets().open("my_image.jpg");
Drawable d = Drawable.createFromStream(ims, null);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question