0

I am making an app which intakes students details that are entered. The student is able to view their record by retrieving their details from the database. I have coded all of these so far...but now I am stuck at assigning an image to each of those student records as they appear when the student selects to view the records. I have looked online but can't seem to find much information related to the database...

Below is the question from my assignment which I am stuck at:

"Provide an interface and corresponding actions where user can select an image and assign the image to a student. The images can be manually stored within the app itself. The app should be able to display the assigned image when showing the student information."

I have also attached images to make my question clear :)

Student entering details

How the images are meant to appear

//activity_main

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout    
    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=".Viewstudent">

    <android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

     </android.support.design.widget.AppBarLayout>

     <include layout="@layout/screen4" />

    <ListView
    android:id="@+id/listView"
    android:layout_marginTop="60dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    </ListView>

  </android.support.design.widget.CoordinatorLayout>

//customlayoutstudent.xml

     <?xml version="1.0" encoding="utf-8"?>
    <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="match_parent"
    android:orientation="vertical"
   >

<ImageView
    android:id="@+id/imageview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/sno"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="15dp"
    android:text="TextView"
 />

<TextView
    android:id="@+id/sid"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView"
     />

<TextView
    android:id="@+id/fn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView"
     />

<TextView
    android:id="@+id/ln"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView"
     />

<TextView
    android:id="@+id/ge"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView"
     />

<TextView
    android:id="@+id/cs"
    android:layout_width="wrap_content"
    android:layout_height="20dp"
    android:text="TextView"
     />

<TextView
    android:id="@+id/ag"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView"
  />

<TextView
    android:id="@+id/ad"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView"
    android:layout_marginBottom="20dp"
     />

//Main activity

   package com.user.project3;

  import android.app.Activity;
  import android.content.Intent;
  import android.database.Cursor;
  import android.os.Bundle;
  import android.support.v7.app.AppCompatActivity;
  import android.support.v7.widget.Toolbar;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.BaseAdapter;
  import android.widget.ImageView;
  import android.widget.ListView;
  import android.widget.SimpleAdapter;
  import android.widget.TextView;

  import java.util.ArrayList;

  public class Viewstudent extends AppCompatActivity {

 DatabaseManager myDb;

ArrayList<String> sno = new ArrayList<String>();
ArrayList<String> sid = new ArrayList<String>();
ArrayList<String> fn = new ArrayList<String>();
ArrayList<String> ln = new ArrayList<String>();
ArrayList<String> ge = new ArrayList<String>();
ArrayList<String> cs = new ArrayList<String>();
ArrayList<String> ag = new ArrayList<String>();
ArrayList<String> ad = new ArrayList<String>();

Integer[] imgid {R.drawable.ic_launcher_background}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main4);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle(null);

    myDb = new DatabaseManager(this);


    ListView listView  = (ListView)findViewById(R.id.listView);

    CustomAdapter customAdapter = new CustomAdapter();
    listView.setAdapter(customAdapter);


    Cursor res = myDb.getAllDataStudent();
    while(res.moveToNext()){
        sno.add(("Student Number: " + res.getString(0)));
        sid.add(("Student Id: " + res.getString(1)));
        fn.add("First Name: " + res.getString(2));
        ln.add("Last Name: " + res.getString(3));
        ge.add("Gender: " + res.getString(4));
        cs.add("Course Study: " + res.getString(5));
        ag.add("Age: " + res.getString(6));
        ad.add("Address: " + res.getString(7));


      }


   }


class CustomAdapter extends BaseAdapter {

    @Override
    public int getCount() {
        return sno.size();//return count of array
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override

    public View getView(int position, View convertView, ViewGroup                 
    parent)  {

        convertView =  
     getLayoutInflater().inflate(R.layout.customlayoutstudent,null);
        TextView id = (TextView)convertView.findViewById(R.id.sno);
        TextView studentid = 
        (TextView)convertView.findViewById(R.id.sid);
        TextView fname = (TextView)convertView.findViewById(R.id.fn);
        TextView lname = (TextView)convertView.findViewById(R.id.ln);
        TextView gender = (TextView)convertView.findViewById(R.id.ge);
        TextView coursestudy = 
        (TextView)convertView.findViewById(R.id.cs);
        TextView age = (TextView)convertView.findViewById(R.id.ag);
        TextView addr = (TextView)convertView.findViewById(R.id.ad);


        id.setText(sno.get(position));
        studentid.setText(sid.get(position));
        fname.setText(fn.get(position));
        lname.setText(ln.get(position));
        gender.setText(ge.get(position));
        coursestudy.setText(cs.get(position));
        age.setText(ag.get(position));
        addr.setText(ad.get(position));


        return convertView;
    }
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.screen4_menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement

    if (id == R.id.home8) {
        Intent intent = new Intent(Viewstudent.this, Home.class);
        startActivity(intent);
        return true;

    }

    if (id == R.id.prev3) {
        Intent intent = new Intent(Viewstudent.this, Addrecord.class);
        startActivity(intent);
        return true;

    }
    return super.onOptionsItemSelected(item);
  }


   }
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
s98
  • 45
  • 8

2 Answers2

0

If the student Images is saved in device you can open gallery while user enter student data then you save this image as a blob in your database if you are using sqlite .. saving image as Blob will let you retrieve it as byte array and then you can show this images as bitmap to the imageview in the list. in this linke you can find an example of dealing with blobs in android

how to store Image as blob in Sqlite & how to retrieve it?

0

You can do one thing, while registering a student or adding his records to the database, just assign an ImageID to that student's username/name.
This way, each student will have 1 image assigned on database. While fetching information for that particular student, you fetch that image id too, and using switch loop just display the drawable image associated with that imageId.

Abhijit
  • 95
  • 5