0

I tried looking around but cant seem to find an answer that could resolve it. Here is the process of what I'm doing:

1: Users enter data in a alert dialog

2: Data get sent into sql.

3: Adapter runs, takes data and put into a cardview.

4: Cardview is displayed(Stacks according to number of inputs made).

What that is not working to plan, the data that is being outputted overrides all previous inputs,and updates all cardviews with the latest information.

EG: Input 1: Test 1, Input 2: Test 2. Output will be 2 cardviews of Test 2.

Contact Activity

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.sp.R;

public class Contacts extends AppCompatActivity {

    private Button newContact;
    private AlertDialog.Builder builder;
    private AlertDialog dialog;
    private RecyclerView recyclerView;

    private ContactDatabaseHelper helper;
    private ContactAdapter mAdapter;

    private EditText new_contact_name;
    private EditText new_contact_number;
    private EditText new_contact_relation;
    private Button button_create_contact;
    private Button button_cancel_create;

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

        helper = new ContactDatabaseHelper(this);

        recyclerView = findViewById(R.id.contact_recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new ContactAdapter(this,getAllItems());
        recyclerView.setAdapter(mAdapter);

        newContact = findViewById(R.id.contact_button);
        // New Contact Button
        newContact.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                builder = new AlertDialog.Builder(Contacts.this);
                builder.setTitle("Create New Contact");
                builder.setCancelable(false);
                View view = LayoutInflater.from(Contacts.this).inflate(R.layout.new_contact_dialog,null,false);
                CreateDialog(view);
                builder.setView(view);
                dialog = builder.create();
                dialog.show();
            }
        });

    }

    private void CreateDialog(View view) {

        // ContactDialog ID's
        new_contact_name = view.findViewById(R.id.new_contact_name);
        new_contact_number = view.findViewById(R.id.new_contact_number);
        new_contact_relation = view.findViewById(R.id.new_contact_relation);
        button_create_contact = view.findViewById(R.id.button_create_contact);
        button_cancel_create = view.findViewById(R.id.button_cancel_create);

        button_create_contact.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                        // Get String of Name, Number, Relationship
                        String contactNameStr = new_contact_name.getText().toString();
                        String contactNumberStr = new_contact_number.getText().toString();
                        String contactRelationStr = new_contact_relation.getText().toString();

                        // Insert into ContactTable
                        helper.insertContactDatabase(contactNameStr,contactNumberStr,contactRelationStr);
                        mAdapter.swapCursor(getAllItems());

                        // Outputs a Toast
                        Toast.makeText(Contacts.this, "Contact Created", Toast.LENGTH_SHORT).show();
                        dialog.dismiss();
            }
        });

        button_cancel_create.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });

    }

    private Cursor getAllItems(){
        return helper.getContactDatabase();
    }

}

Contact Adapter

import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.sp.R;

public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.MyHolder>{

    private Context mContext;
    private Cursor c;
    private ContactDatabaseHelper helper;

    public ContactAdapter(Context context,Cursor cursor) {

        mContext = context;
        c = cursor;
        helper = new ContactDatabaseHelper(mContext);
    }

    public static class MyHolder extends RecyclerView.ViewHolder{

        private TextView contact_name;
        private TextView contact_number;
        private TextView contact_relation;

        public MyHolder(View itemView) {
            super(itemView);

            contact_name = itemView.findViewById(R.id.contact_list_name_item);
            contact_number = itemView.findViewById(R.id.contact_list_number_item);
            contact_relation = itemView.findViewById(R.id.contact_list_relation_item);
        }
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(mContext).inflate(R.layout.contact_list,parent,false);

        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(MyHolder holder, final int position) {

        c.moveToFirst();

        holder.contact_name.setText(helper.getContactName(c));
        holder.contact_number.setText(helper.getContactNumber(c));
        holder.contact_relation.setText(helper.getContactRelation(c));

    }

    @Override
    public int getItemCount() {

        return c.getCount();
    }

    public void swapCursor(Cursor newCursor){
        if (c != null){
            c.close();
        }

        c = newCursor;

        if (newCursor != null){
            notifyDataSetChanged();
        }
    }

}

Contact Database

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


public class ContactDatabaseHelper extends SQLiteOpenHelper {

    public static String DATABASE_NAME = "contactlist.db";
    private static final int SCHEMA_VERSION = 1;

    public ContactDatabaseHelper(Context context) { super(context, DATABASE_NAME, null, SCHEMA_VERSION); }

    @Override
    public void onCreate(SQLiteDatabase db) {

        db.execSQL("CREATE TABLE contact_table( name TEXT, number TEXT , relation TEXT);");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }

    // Adds data into Contact database
    public void insertContactDatabase (String contact_name, String contact_number, String contact_relation) {
        ContentValues cv = new ContentValues();

        cv.put("name", contact_name);
        cv.put("number", contact_number);
        cv.put("relation", contact_relation);

        getWritableDatabase().insert("contact_table", "name", cv);
    }

    // Read Contact database
    public Cursor getContactDatabase() {
        return  (getReadableDatabase().rawQuery(
                "SELECT name, number, relation  FROM contact_table ORDER BY  + name", null));
    }

    // Delete Contact database
    public Integer deleteContactDatabase () {
        SQLiteDatabase db = this.getWritableDatabase();
        return db.delete("contact_table",null,null);

    }

    public String getContactName (Cursor c){ return (c.getString(0)); }

    public String getContactNumber (Cursor c){ return (c.getString(1)); }

    public String getContactRelation (Cursor c){ return (c.getString(2)); }
}

contacts

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    android:background="@drawable/contact_background"
    tools:context=".Contact.Contacts">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <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">

                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/contact_recycler_view"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_above="@id/contact_button"
                    android:layout_marginTop="10dp"
                    android:scrollbars="vertical">

                </androidx.recyclerview.widget.RecyclerView>

                <Button
                    android:id="@+id/contact_button"
                    android:layout_width="150dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="10sp"
                    android:layout_marginBottom="10sp"
                    android:background="#43464B"
                    android:text="New Contact"
                    android:textAlignment="center"
                    android:textColor="#FFFFFF"
                    android:textSize="18sp" />
            </LinearLayout>
        </ScrollView>

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

contact_list (Cardview)

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/list_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardBackgroundColor="#FFEAB8"
    app:cardCornerRadius="15dp"
    app:cardElevation="5dp"
    app:cardUseCompatPadding="true">

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardBackgroundColor="#00ACEE"
        app:cardCornerRadius="5dp"
        app:cardElevation="15dp"
        app:cardUseCompatPadding="true">

        <LinearLayout
            android:id="@+id/contact_layout_name"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="10dp"
            android:orientation="horizontal">

            <TextView
                android:layout_width="120dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:text="Name"
                android:textAlignment="center"
                android:textColor="#E6E6E6"
                android:textSize="20sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:text=":"
                android:textSize="20sp" />

            <TextView
                android:id="@+id/contact_list_name_item"
                android:layout_width="190dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:text="Name"
                android:textColor="#FFFF00"
                android:textSize="20sp" />

        </LinearLayout>


        <LinearLayout
            android:id="@+id/contact_layout_number"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="35dp"
            android:orientation="horizontal">

            <TextView
                android:layout_width="120dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="5dp"
                android:text="Number"
                android:textAlignment="center"
                android:textColor="#E6E6E6"
                android:textSize="20sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="5dp"
                android:text=":"
                android:textSize="20sp" />

            <TextView
                android:id="@+id/contact_list_number_item"
                android:layout_width="190dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="5dp"
                android:text="Number"
                android:textColor="#FFFF00"
                android:textSize="20sp" />

        </LinearLayout>


        <LinearLayout
            android:id="@+id/contact_layout_relation"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="60dp"
            android:layout_marginBottom="15dp"
            android:orientation="horizontal">

            <TextView
                android:layout_width="120dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="10dp"
                android:text="Relationship"
                android:textAlignment="center"
                android:textColor="#E6E6E6"
                android:textSize="20sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="10dp"
                android:text=":"
                android:textSize="20sp" />

            <TextView
                android:id="@+id/contact_list_relation_item"
                android:layout_width="190dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="10dp"
                android:text="Relationship"
                android:textColor="#FFFF00"
                android:textSize="20sp" />

        </LinearLayout>

    </androidx.cardview.widget.CardView>

</androidx.cardview.widget.CardView>
DevilArts
  • 3
  • 1

4 Answers4

0

I didn't read through and understand all your code but those lines seems off to me:

holder.contact_name.setText(helper.getContactName(c));
holder.contact_number.setText(helper.getContactNumber(c));
holder.contact_relation.setText(helper.getContactRelation(c));

maybe try using the position

Joe
  • 2,139
  • 1
  • 16
  • 26
0

Are you debug your code? What about method

 public void swapCursor(Cursor newCursor){
        if (c != null){
            c.close();
        }

        c = newCursor;

        if (newCursor != null){
            notifyDataSetChanged();
        }
    }

notifyDataSetChanged() is method which you should call.

0

Try copy all items to arrayList (only for test), then get item by position in onBindViewHolder

I am thinking your problem - c.moveToFirst(); in onBindViewHolder

Arseniy
  • 51
  • 1
  • 4
0

There are some issues in your code. I would start from the Cursor that remains open while the recyclerview is recycling views. That's not good, and moreover, you're doing it in UI Thread, and this will slow down your UI a lot.

I would suggest you read all the data and pass it to your adapter. That's what adapters are for: they are a way to access your data, but they do not have to know anything about how your data is accessed. So the first thing is changing the db helper, adding a method that will loop through all the data in your db and returns a list:

public List<Contact> readAllContacts() {
    List<Contact> contacts = new ArrayList();
    Cursor cursor = this.getContactDatabase();
    if (cursor.moveToFirst()){
       do {
          contacts.add(new Contact(cursor.getString(0), cursor.getString(1), cursor.getString(2)));
       } while(cursor.moveToNext());
    }
    cursor.close();
}

You will also need to create a Contact class, with a public constructor.

Then, I would modify the adapter, to receive a List of Contacts, instead of the cursor:

public ContactAdapter(Context context,List<Contact> contacts) {
    mContext = context;
    this.contacts = contacts;
}

You won't need the db helper in the adapter anymore, since you already have the whole list and the cursor, now, is already closed, improving performance for the whole UI. Also, you'll need a class attribute named contacts, of type List.

Now you can access the value for the single row by position, so in your onBindViewHolder you will have:

public void onBindViewHolder(MyHolder holder, final int position) {
    holder.contact_name.setText(this.contacts.get(position).getName());
    holder.contact_number.setText(this.contacts.get(position).getNumber());
    holder.contact_relation.setText(this.contacts.get(position).getContactRelation());

}

This will already work. On top of all of this, I warmly suggest you to start using Room instead of writing your own db helper.

Gregorio Palamà
  • 883
  • 8
  • 13
  • Does this get saved into SQL thou? Cause initially I tried doing it with ArrayList, but couldn't get it to be stored into a database. – DevilArts Feb 17 '20 at 06:50
  • the question was referred to the list, so I only took a look at the issues related to the list and not to the insert. They are two different operations, so I guess there might be something else in the code you're using to store data. – Gregorio Palamà Feb 17 '20 at 08:11
  • I didn't follow exactly what you did but, creating an array and retrieving information from the array fixed that issue. Gonna look around and open another question if i need for the data saving. – DevilArts Feb 17 '20 at 09:57