John Ta
John Ta

Reputation: 19

Two adapters in one listview

I apologize in advance for asking a question that is already out there, but I just cannot seem to get it to work (I am also relatively new to Android Studios).

I am creating a homework app that takes user input through a popup and the user's input is stored as an array of strings, which is then converted to an adapter. The adapter is then used to print the values of the array to the user's screen through a list view.

The issue I am having is that I have two columns: one for the class name and one for the class assignment. I am able to use one adapter to display all the values for the class names, but the other column is left with the default hint value. I am trying to display both columns accordingly with each of the class names and their respective assignments.

I know I have to somehow merge the adapters into one, but I do not know how to do it (see Java for Planner). If someone could please help me out, I would appreciate it greatly.

This is the Java for the screen

package com.example.johnta.homeworkappv2;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Arrays;

public class PlannerActivity extends ListActivity {

    public static ArrayList<String> arrayList, arrayList_assignments;

    private static ArrayAdapter<String> adapter, adapter_assignments;
    private static EditText txtInput;
    private static TextView tvMsg;
    private static PopupWindow popUpWindow;
    private static LinearLayout mainLayout;

    private String [] items = {"Physics","Humanities","Math","STEM"};
    private String [] list_of_assignments = {"Notes","Reading","Problems","Engineering"};
    private String [][] twoDimensionalItems = {{"Physics","Notes"}};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        popUpWindow = new PopupWindow(this);
        mainLayout = new LinearLayout(this);

        setContentView(R.layout.activity_planner);

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

        arrayList = new ArrayList<>(Arrays.asList(items));
        arrayList_assignments = new ArrayList<>(Arrays.asList(list_of_assignments));

        adapter = new ArrayAdapter<String>(this,R.layout.editlistitems,R.id.name_of_class,arrayList);
        adapter_assignments = new ArrayAdapter<String>(this,R.layout.editlistitems,R.id.homework_description,arrayList_assignments);

        listView.setAdapter(adapter);


        txtInput = (EditText)findViewById(R.id.name_of_class);
    }

    public void makeEditable(View v) {

    }

    public void onClickEdit (View v) {
        startActivity(new Intent(PlannerActivity.this,popup.class));

    }

    public static void addItemToArray (String itemToAdd) {
        arrayList.add(itemToAdd);
        adapter.notifyDataSetChanged();
    }

}

This is the Java for the popup

package com.example.johnta.homeworkappv2;

import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.EditText;

/**
 * Created by johnta on 4/3/17.
 */

public class popup extends Activity {

    private EditText txtInput;
    private EditText input_assignment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.plannerpopup);

        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);

        int width = dm.widthPixels;
        int height = dm.heightPixels;

        getWindow().setLayout((int)(width*0.8),(int)(height*0.6));

        txtInput = (EditText)findViewById(R.id.name_of_class);
        input_assignment = (EditText)findViewById(R.id.class_assignment);
    }

    public void onClickEditList (View v) {
        String newItem = txtInput.getText().toString();
        PlannerActivity.addItemToArray(newItem);
        super.onBackPressed();
    }
}

**This is the XML for activity_planner **

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context="com.example.johnta.homeworkappv2.settings"
    android:background="#87ceeb">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout_constraintLeft_creator="1"
        tools:layout_constraintTop_creator="1">

        <ImageView
            android:id="@+id/planner_picture"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_marginTop="5dp"
            android:background="@drawable/roundbutton"
            app:srcCompat="@drawable/planner10pad"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="35dp" />

        <Button
            android:id="@+id/edit_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Edit"
            tools:layout_editor_absoluteX="16dp"
            tools:layout_editor_absoluteY="130dp"
            android:layout_marginTop="154dp"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:onClick="onClickEdit"/>

        <Button
            android:id="@+id/copy_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Copy"
            android:layout_alignBaseline="@+id/edit_button"

            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_alignRight="@id/planner_picture"
            />

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Planner"
            android:textColor="#000000"
            android:textSize="32sp"
            android:layout_alignTop="@+id/edit_button"
            android:gravity="center"
            android:layout_alignBottom="@+id/edit_button"
            android:layout_alignLeft="@+id/planner_picture"
            android:layout_alignStart="@+id/planner_picture"
            android:layout_alignRight="@+id/planner_picture"
            android:layout_alignEnd="@+id/planner_picture" />

        <ListView
            android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="#ffffff"
            android:layout_below="@+id/edit_button"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_weight="1"
            android:divider="#000000">
        </ListView>

    </RelativeLayout>

</android.support.constraint.ConstraintLayout>

**This is the XML for the listview in Planner **

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/name_of_class"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:paddingTop="3dp"
        android:paddingBottom="3dp"
        android:paddingLeft="5dp"
        android:paddingRight="3dp"
        android:hint="Class name"
        android:textStyle="bold"
        />

    <TextView
        android:id="@+id/homework_description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="3dp"
        android:paddingBottom="3dp"
        android:paddingLeft="3dp"
        android:hint="Class Assignment"
        android:textSize="20sp"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:inputType="textMultiLine"
        android:layout_alignBaseline="@+id/name_of_class"
        android:layout_alignBottom="@+id/name_of_class"
        android:layout_toRightOf="@+id/name_of_class"
        android:layout_toEndOf="@+id/name_of_class" />
</RelativeLayout>

Upvotes: 1

Views: 2131

Answers (1)

OneCricketeer
OneCricketeer

Reputation: 191743

The issue I am having is that I have two columns: one for the class name and one for the class assignment. I am able to use one adapter to display all the values for the class names, but the other column is left with the default hint value.

Don't store two Arraylists for this.

You can either use a Hashmap and a SimpleAdapter or use the OOP features of Java to create a Custom ArrayAdapter for some Plan class that hold's two strings (note class is a reserved word in Java).

For details https://github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView#using-a-custom-arrayadapter

Might also be worth researching Android Databinding

Upvotes: 1

Related Questions