Skip to content

Commit

Permalink
Use CursorAdapter
Browse files Browse the repository at this point in the history
  • Loading branch information
ujjwalmaity committed Jan 28, 2020
1 parent 6e9dae1 commit 10993b1
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".ShowActivity"></activity>
<activity
android:name=".MainActivity"
android:screenOrientation="portrait">
Expand All @@ -21,7 +22,6 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity android:name=".AttendanceActivity" />

<provider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public void onClick(View v) {
findViewById(R.id.read).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/*
// StudentDbHelper studentDbHelper = new StudentDbHelper(getApplicationContext());
// SQLiteDatabase db = studentDbHelper.getReadableDatabase();
Expand Down Expand Up @@ -123,6 +124,9 @@ public void onClick(View v) {
showMessage("Data", buffer.toString());
cursor.close();
*/
Intent intent = new Intent(MainActivity.this, ShowActivity.class);
startActivity(intent);
}
});

Expand Down
33 changes: 33 additions & 0 deletions app/src/main/java/dev/ujjwal/sqlitedatastorage/ShowActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package dev.ujjwal.sqlitedatastorage;

import androidx.appcompat.app.AppCompatActivity;

import android.database.Cursor;
import android.os.Bundle;
import android.widget.ListView;

import dev.ujjwal.sqlitedatastorage.data.StudentContract.StudentEntry;

public class ShowActivity extends AppCompatActivity {

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

String[] projection = {StudentEntry._ID,
StudentEntry.COLUMN_QR_CODE_ID,
StudentEntry.COLUMN_NAME,
StudentEntry.COLUMN_BATCH};
Cursor cursor = getContentResolver().query(
StudentEntry.CONTENT_URI,
projection,
null,
null,
null);

ListView listView = findViewById(R.id.activity_show_listView);
StudentCursorAdapter studentCursorAdapter = new StudentCursorAdapter(this, cursor);
listView.setAdapter(studentCursorAdapter);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package dev.ujjwal.sqlitedatastorage;

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

import dev.ujjwal.sqlitedatastorage.data.StudentContract.StudentEntry;

public class StudentCursorAdapter extends CursorAdapter {

public StudentCursorAdapter(Context context, Cursor c) {
super(context, c, 0);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.item_show, parent, false);
}


@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView nameTextView = view.findViewById(R.id.name_item_show);
TextView summaryTextView = view.findViewById(R.id.batch_item_show);

// Find the columns of pet attributes that we're interested in
int nameColumnIndex = cursor.getColumnIndex(StudentEntry.COLUMN_NAME);
int breedColumnIndex = cursor.getColumnIndex(StudentEntry.COLUMN_BATCH);

// Read the pet attributes from the Cursor for the current pet
String petName = cursor.getString(nameColumnIndex);
String petBreed = cursor.getString(breedColumnIndex);

// Update the TextViews with the attributes for the current pet
nameTextView.setText(petName);
summaryTextView.setText(petBreed);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public AttendanceAdapter(Context context, Integer[] id, String[] name, Integer[]
@Override
public AttendanceHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.attendance_layout, parent, false);
View view = layoutInflater.inflate(R.layout.item_attendance, parent, false);
return (new AttendanceHolder(view));
}

Expand Down
12 changes: 12 additions & 0 deletions app/src/main/res/layout/activity_show.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".ShowActivity">

<ListView
android:id="@+id/activity_show_listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
File renamed without changes.
23 changes: 23 additions & 0 deletions app/src/main/res/layout/item_show.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp">

<TextView
android:id="@+id/name_item_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#2B3D4D" />

<TextView
android:id="@+id/batch_item_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif"
android:textAppearance="?android:textAppearanceSmall"
android:textColor="#AEB6BD" />
</LinearLayout>

0 comments on commit 10993b1

Please sign in to comment.