I have customized listview with row_layout.xml to be recycled each row. I'm using SQLite database (which gets data from another layout with editText) My database would look something like this:
_________________________________________________________
| date | time | noon | hyd | temp | hr | bp |
|______|________|________|_______|________|______|______|
| 1 | 10 | AM | Y | # | # | # |
| 1 | 5 | PM | N | # | # | # |
| 2 | 11 | AM | Y | # | # | # |
| 2 | 6 | PM | N | # | # | # |
|______|________|________|_______|________|______|______|
I want it to display in this manner:
________________________________
|DATE__________________________|
|AM: |PM: |
|hyd = ______ |hyd = _____ |
|temp = ______ |temp = _____ |
|hr = ______ |hr = _____ |
|bp = ______ |bp = _____ |
|_______________|______________|
Here's my code for row_layout.xml:
<LinearLayout
xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ActivityHistory">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textStyle="bold"
android:padding="15dp"
android:id="@+id/textView1" />
<!-- Monday -->
<TextView
android:id="@+id/date_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="05/11/2015 Monday"
android:textSize="12dp"
/>
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<!-- information: bars -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AM"
android:textSize="10dp"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000"
android:padding="2dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp">
<ImageView
android:layout_width="40dp"
android:layout_height="60dp"
android:id="@+id/StatusBarAM"
android:background="#ffff0000"
/>
</LinearLayout>
<!-- information: stats -->
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="HYD:"
/>
<TextView
android:id="@+id/hyd_textAM"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint=" _____"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEMP:"
/>
<TextView
android:id="@+id/temp_textAM"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint=" _____"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="HR:"
/>
<TextView
android:id="@+id/hr_textAM"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint=" _____"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BP:"
/>
<TextView
android:id="@+id/bp_textAM"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint=" _____"
/>
</LinearLayout>
</LinearLayout>
<!-- information: bars -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:text="PM"
android:textSize="10dp"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000"
android:padding="2dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp">
<ImageView
android:layout_width="40dp"
android:layout_height="60dp"
android:background="#ff00ff00"
android:id="@+id/StatusBarPM"
/>
</LinearLayout>
<!-- information: stats -->
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="HYD:"
/>
<TextView
android:id="@+id/hyd_textPM"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint=" _____"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEMP:"
/>
<TextView
android:id="@+id/temp_textPM"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint=" _____"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="HR:"
/>
<TextView
android:id="@+id/hr_textPM"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint=" _____"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BP:"
/>
<TextView
android:id="@+id/bp_textPM"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint=" _____"
/>
</LinearLayout>
</LinearLayout>
/>
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
But as you can see, AM & PM of each DATE have to display on the same row. Instead, my code will move onto next item for each row. When my code is run, it will display first date with only AM filled, then move onto next row with only PM filled. Is there a way you can either group the same dates, or display 2 rows of database onto same row layout?
And also, please check my code with how I'm using SQLite database. I used an example found online and was wondering if it's a common use, or if there's an easier or more useful way to use SQLite.
Here's my MainActivity.java
package com.example.steven.hsm;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.app.AlertDialog.Builder;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
// Initialize variables
private List<Status> myStats = new ArrayList<Status>();
private String noonResult;
private String hydResult;
// Even = AM & Odd = PM
private int count = 0;
// Initialize database variable
SQLiteDatabase db;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_history);
Button btnupdatestatus = (Button) findViewById(R.id.btnupdatestatus);
btnupdatestatus.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(), UpdateStatus.class));
}
});
// open or create database named HSMdb
// parameters: filename, privately used for this app only, cursor factory object (null)
db=openOrCreateDatabase("HSMdb1", Context.MODE_PRIVATE, null);
// executes SQL command: create table if it doesn't exist
db.execSQL("CREATE TABLE IF NOT EXISTS stats(" +
"hyd VARCHAR,temp VARCHAR,hr VARCHAR,bp VARCHAR);");
// Pull up stats
populateStatList();
// Update History Page
populateListView();
}
private void populateStatList(){
Cursor c=db.rawQuery("SELECT * FROM stats", null);
if(c.getCount()==0)
{
showMessage("Error", "No records found");
return;
}
while(c.moveToNext()){
// Create List of Items
myStats.add(new Status(
c.getString(0), //DATE
c.getString(1), //TIME
c.getString(2), //NOON
c.getString(3), //HYD
c.getString(4), //TEMP
c.getString(5), //HR
c.getString(6))); //BP
}
}
private void populateListView() {
ArrayAdapter<Status> adapter = new MyListAdapter();
ListView list = (ListView) findViewById(R.id.theListView);
list.setAdapter(adapter);
}
private class MyListAdapter extends ArrayAdapter<Status> {
public MyListAdapter() {
super(MainActivity.this, R.layout.row_layout, myStats);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Make sure we have a view to work with (may have been given null)
View itemView = convertView;
if (itemView == null) {
itemView = getLayoutInflater().inflate(R.layout.row_layout, parent, false);
}
// Find the index to work with.
Status index = myStats.get(position);
// bars initialized to white
ImageView statBarAM = (ImageView) itemView.findViewById(R.id.StatusBarAM);
statBarAM.setBackgroundColor(Color.WHITE);
ImageView statBarPM = (ImageView) itemView.findViewById(R.id.StatusBarPM);
statBarPM.setBackgroundColor(Color.WHITE);
// Data into layout
/*************************************************************************************/
// DATE
TextView dateText = (TextView) itemView.findViewById(R.id.date_text);
dateText.setText(" " + index.getDATE());
// get AM/PM in advance for if statement:
noonResult = index.getNOON();
// get HYD in advance for if statement:
hydResult = index.getHYD();
// --------------------AM (left side)--------------------
if(noonResult.equals("AM"))
{
TextView hydText = (TextView) itemView.findViewById(R.id.hyd_textAM);
hydText.setText(" " + hydResult);
// HYD = YES = HYDRATED = GREEN
if(hydResult.equals("YES")){
statBarAM.setBackgroundColor(Color.GREEN);
}
// HYD = NO = DEHYDRATED = RED
else if(hydResult.equals("NO")){
statBarAM.setBackgroundColor(Color.RED);
}
// HYD = ? = BY DEFAULT = GRAY
else{
statBarAM.setBackgroundColor(Color.WHITE);
}
// TEMP:
TextView tempText = (TextView) itemView.findViewById(R.id.temp_textAM);
tempText.setText(" " + index.getTEMP());
// HR:
TextView hrText = (TextView) itemView.findViewById(R.id.hr_textAM);
hrText.setText(" " + index.getHR());
// BP:
TextView bpText = (TextView) itemView.findViewById(R.id.bp_textAM);
bpText.setText(" " + index.getBP());
}
// --------------------PM (right side)--------------------
else if(noonResult.equals("PM")){
TextView hydText = (TextView) itemView.findViewById(R.id.hyd_textPM);
hydText.setText(" " + hydResult);
// HYD = YES = HYDRATED = GREEN
if(hydResult.equals("YES")){
statBarPM.setBackgroundColor(Color.GREEN);
}
// HYD = NO = DEHYDRATED = RED
else if(hydResult.equals("NO")){
statBarPM.setBackgroundColor(Color.RED);
}
// HYD = ? = BY DEFAULT = GRAY
else{
statBarPM.setBackgroundColor(Color.WHITE);
}
// TEMP:
TextView tempText = (TextView) itemView.findViewById(R.id.temp_textPM);
tempText.setText(" " + index.getTEMP());
// HR:
TextView hrText = (TextView) itemView.findViewById(R.id.hr_textPM);
hrText.setText(" " + index.getHR());
// BP:
TextView bpText = (TextView) itemView.findViewById(R.id.bp_textPM);
bpText.setText(" " + index.getBP());
}
/*************************************************************************************/
return itemView;
}
}
public void onClick(View view){
//add button clicks
}
public void showMessage(String title,String message)
{
Builder builder=new Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, 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.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Status.java:
package com.example.steven.hsm;
/**
* Created by Steven on 5/17/2015.
* Store status information
*/
public class Status {
private String date, time, noon, hyd, temp, hr, bp;
public Status(String date, String time, String noon, String hyd, String temp, String hr, String bp) {
super();
this.date = date;
this.time = time;
this.noon = noon;
this.hyd = hyd;
this.temp = temp;
this.hr = hr;
this.bp = bp;
}
public String getDATE() {
return date;
}
public String getTIME() {
return time;
}
public String getNOON() {
return noon;
}
public String getHYD() {
return hyd;
}
public String getTEMP() {
return temp;
}
public String getHR() {
return hr;
}
public String getBP() {
return bp;
}
}
activity_history.xml:
<RelativeLayout
xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".ActivityHistory"
android:id="@+id/history">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Gaia Medical Institute"
android:gravity="center"
android:textColor="#FFFFFF"
android:background="#2513C2"
android:id="@+id/textView2"
android:textSize="22dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:paddingTop="8dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="History"
android:id="@+id/textView3"
android:textSize="30dp"
android:layout_below="@+id/textView2"
android:layout_centerHorizontal="true" />
<LinearLayout
android:id="@+id/maindashboard"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="D"
android:id="@+id/btndashboard"
android:background="#006699"
android:textColor="#FFFFFF" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="U"
android:id="@+id/btnupdatestatus"
android:background="#006699"
android:textColor="#FFFFFF" />
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="H"
android:id="@+id/btnhistory"
android:background="#006699"
android:textColor="#FFFFFF" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="R"
android:id="@+id/btnrecom"
android:background="#006699"
android:textColor="#FFFFFF" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="S"
android:id="@+id/btnsettings"
android:background="#006699"
android:textColor="#FFFFFF" />
</LinearLayout>
<ListView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_below="@id/textView3"
android:layout_above="@id/maindashboard"
android:id="@+id/theListView">
</ListView>
</RelativeLayout>
Aucun commentaire:
Enregistrer un commentaire