0

I'm trying to build an Attendance Manager app for students and am using a custom ListView to display subject names and minimum attendance when tapped on.

These are the Issues I'm facing::

1) I want my first list item to be an "Add a subject item" and on tapping on that it goes to AddSubjectActivity and stores everything via SharedPreferences.

2)Once the submit button is pressed in the AddSubjectActivity i want the next list item to say "Tap to add a subject" and so on.

3) Basically ,suppose the ListView has 5 items, tapping on each item takes me to the AddSubject Activity, but information corresponding to the respective list Item being tapped is displayed in it.

This is my Code:

Class CustomAdapter

    public class CustomAdapter extends BaseAdapter{
    String [] result;
    Context context;
    int [] imageId;
    private static LayoutInflater inflater=null;
    public CustomAdapter(SecondActivity mainActivity, String[] prgmNameList, int[] prgmImages) {
        // TODO Auto-generated constructor stub
        result=prgmNameList;
        context=mainActivity;
        imageId=prgmImages;
        inflater = ( LayoutInflater )context.
                    getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return result.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public class Holder
    {
        TextView tv;
        ImageView img;
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        Holder holder=new Holder();
        SecondActivity second=new SecondActivity();
        View rowView;
        rowView = inflater.inflate(R.layout.program_list, null);
        holder.tv=(TextView) rowView.findViewById(R.id.textView);
        holder.img=(ImageView) rowView.findViewById(R.id.statsImageView);
        holder.tv.setText(result[position]);
        holder.img.setImageResource(imageId[position]);
        rowView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                Toast.makeText(context, "You Clicked "+result[position], Toast.LENGTH_LONG).show();
                context.startActivity(new Intent(context, AddSubject.class));
            }
        });





        return rowView;
    }

}

SecondActivity: The Activity that contains the ListView

    @Override
    protected void onCreate(Bundle savedInstanceState)

    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        Bundle bundle=getIntent().getExtras();

        String fullNameFinal = bundle.getString("fullName");
        String courseFinal = bundle.getString("course");
        int avgAttendanceFinal= bundle.getInt("avgAttendance");
        boolean logInStatusFinal = bundle.getBoolean("logInStatus");



        nameTextView=(TextView)findViewById(R.id.nameTextView);
        courseTextView=(TextView)findViewById(R.id.courseTextView);

        nameTextView.setText(fullNameFinal);
        courseTextView.setText(courseFinal);




        overridePendingTransition(R.anim.slide_in, R.anim.slide_out);

        ListView lv;
        Context context;

        ArrayList prgmName;
        int [] prgmImages={R.drawable.listblockgold,R.drawable.listblockindigo,R.drawable.listblocklime,R.drawable.listblockorange,R.drawable.listblockpink,R.drawable.listblockred,R.drawable.listblocksilver,R.drawable.listblocktan,R.drawable.listblockteal};
        String [] prgmNameList={"Maths","P.Comm.","IOT","Operating Systems","Web Programming","DSP","PHP","Jquery","JavaScript"};


        context=this;

        lv=(ListView) findViewById(R.id.testListView);
        lv.setAdapter(new CustomAdapter(this, prgmNameList,prgmImages));





`AddSubject Activity`

    public class AddSubject extends AppCompatActivity {
//Buttons
    Button missedMinus;
    Button missedPlus;
//ButtonsEnd

//TextViews
    TextView safeBunksNumber;

    TextView missedLectures;

    TextView totalLecturesTextView ;

    TextView percentageTextView;


    //TextViewsEnd
EditText missedLecturesTextView;
    ProgressBar progressBar;
    static String s= Integer.toString(10);


    public void missedMinusClick(View view)
    {
        totalLecturesTextView=(TextView)findViewById(R.id.totalLecturesTextView);
        int totalLectures= Integer.parseInt(totalLecturesTextView.getText().toString());


        missedLecturesTextView=(EditText)findViewById(R.id.missedLecturesTextView);
        int missedLectures= Integer.parseInt(missedLecturesTextView.getText().toString());

        missedLecturesTextView.setText(Integer.toString(missedLectures-1));

        safeBunksNumber=(TextView)findViewById(R.id.safeBunksNumber);
        int safeBunksNumber;



    }
    public void missedPlusClick(View view2)
    {

        missedLecturesTextView=(EditText)findViewById(R.id.missedLecturesTextView);
        int missedLectures= Integer.parseInt(missedLecturesTextView.getText().toString());

        missedLecturesTextView.setText(Integer.toString(missedLectures+1));

    }

    public void submitOnClick1(View view3)
    {
        EditText subName = (EditText)findViewById(R.id.subName);
        EditText totalLecturesTextView = (EditText)findViewById(R.id.totalLecturesTextView);
        EditText missedLecturesTextView =(EditText)findViewById(R.id.missedLecturesTextView);
        String subjectName = subName.getText().toString();
        int totalLectures = Integer.parseInt(totalLecturesTextView.getText().toString());
        int missedLectures = Integer.parseInt(missedLecturesTextView.getText().toString());
        SharedPreferences sharedPreferences = this.getSharedPreferences("com.example.yashrandive.attendancemanager", Context.MODE_PRIVATE);
         sharedPreferences.edit().putString("subName",subjectName);
         sharedPreferences.edit().putInt("totalLectures",totalLectures);
         sharedPreferences.edit().putInt("missedLectures",missedLectures);



    }



    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_subject);











    }
}

enter image description here

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • 1
    You've mentioned what you wan, but you didn't mention your issues. What's the issue? A crash? Behaviour different from expected? No action occurs? – Anuraag Baishya Apr 10 '18 at 09:50
  • @AnuraagBaishya See the issue is, i want to know how can i add cuustom list items by tapping on a button. Moreover considering the current list view, when i tap on a list item it takes me to `AddSubject` Activity and after that even if ive stored the contents to be displayed in add subject activity via SharedPreferences, the stored information isnt being displayed. – Yash Randive Apr 10 '18 at 19:06
  • For problem of SharedPreferences, you are using constant 'key' string (e.g. subName), therefore new data replaced old data. You need to have a counter to keep track how many subjects taken by a specific student and for-loop to save individual subject name and attendance with unique key. However you better use database. Take a look my answer here: https://stackoverflow.com/questions/41646094/how-to-add-data-dynamic-on-recyclerview-and-save-the-data/41646555#41646555 – i_A_mok Apr 11 '18 at 02:17
  • For your adapter, I suggest you to change the array into arrayList as you want to add new items (array is fixed in size and cannot add). Take a look of my sample on ArrayAdapter here: http://programandroidlistview.blogspot.com/ Hope those help! – i_A_mok Apr 11 '18 at 02:18

0 Answers0