mardi 10 février 2015

Android sqlite gridview. App runs, but black screened

I'm trying to make this app working. After fixing few crashes, the app is running, but it's full of black screen and nothing happens. The App is simple. It connects to sqlite and create Goods database with title of good and its price. It insert a test row. Finally it shows all rows in the gridview. This is all functionality by now. Could you please point out for me where to dig or maybe even provide a fix?


activity_main.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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity"
>


<FrameLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1">

<GridView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnCount="4"
android:fastScrollEnabled="true"
android:fastScrollAlwaysVisible="true"
android:id="@+id/gridView1"
android:background="#d3d3d3"></GridView>
</FrameLayout>

<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add to cart"
android:id="@+id/btnAdd"
android:layout_gravity="bottom" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go to cart"
android:id="@+id/btnGoCart"
android:layout_gravity="bottom" />
</LinearLayout>
</LinearLayout>


MainActivity.java:



package com.eshop.app.eshop;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends Activity {

SQLiteDatabase mydb;
GridView data;

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

data=(GridView)findViewById(R.id.gridView1);
List<String> la = new ArrayList<String>();
ArrayAdapter<String> dataAdaptor = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, la);
dataAdaptor.setDropDownViewResource(R.layout.activity_main);

//TextView text = (TextView)findViewById(R.id.textView1);
mydb = openOrCreateDatabase("eshop.db", MODE_PRIVATE, null);
mydb.execSQL("DROP TABLE IF EXISTS Goods;");
mydb.execSQL("CREATE TABLE IF NOT EXISTS Goods (Id INTEGER PRIMARY KEY, Title VARCHAR NOT NULL, Price INTEGER NOT NULL);");
mydb.execSQL("INSERT INTO Goods (Title,Price) VALUES ('Bread', 123);");
try{
Cursor cr = mydb.rawQuery("SELECT * FROM Goods", null);
if(cr!=null){
if(cr.moveToFirst()) {
do {
String id = cr.getString(cr.getColumnIndex("Id"));

String title = cr.getString(cr.getColumnIndex("Title"));
String price = cr.getString(cr.getColumnIndex("Price"));
la.add(id);
la.add(title);
la.add(price);
data.setAdapter(dataAdaptor);
}while(cr.moveToFirst());
}
else{
Toast.makeText(getApplicationContext(), "No data", Toast.LENGTH_LONG).show();
}
}
cr.close();
mydb.close();
}
catch(Exception e){
Toast.makeText(getApplicationContext(), "Error: "+e.getMessage(), Toast.LENGTH_LONG).show();

}
//text.setText(cr.getString(cr.getColumnIndex("Nazwa")));


}


@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);
}
}

Aucun commentaire:

Enregistrer un commentaire