0

I have 3 Activities: The Form Activity, DataBase Helper, and the Main Activity. The Main Activity should display list view of the "compNameAdd" that stores in the DataBase. When i am trying to populate the list view through setAdapter

    DataBase dbc = new DataBase(NituachActivity.this);
    dbc.open();

    String[] cs = new String[] { DataBase.RAW_COMPNAMEADD };

    Log.d(TAG, cs.toString());

     blv.setAdapter(new ArrayAdapter<String>(this,
     android.R.layout.simple_list_item_1, cs));

    dbc.close();

I get the name of the column and not the values in the column. what is the problem?

DataBase Helper:

package com.nituach.nituach;

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

public class DataBase {

public static final String RAW_ID = "_id";
public static final String RAW_COMPNAMEADD = "compNameAdd";
public static final String RAW_CASH = "etCash";
public static final String RAW_CUSTOMERS = "castumers";
public static final String RAW_STOCK = "stock";
public static final String RAW_FIXED_ASSETS = "fixedAssets";
public static final String RAW_ACCUMULTED_DEPRECIATION = "accumultedDepreciation";
public static final String RAW_BONDS = "bonds";
public static final String RAW_LOANS = "loans";
public static final String RAW_EQUITY = "equity";

public static final String RAW_COMPNAME_ID = "_id";
public static final String RAW_COMPNAME = "compName";

private static final String DATABASE_NAME = "ProsseDatBase";
static final String DATABASE_TABLE = "data";
static final String DATABASE_TABLE_SETTINGS = "compNameConnector";

private static final int DATABASE_VERSION = 1;

private ProsseDatBase thdb;
private static Context tcontext;
private SQLiteDatabase tdb;

private static class ProsseDatBase extends SQLiteOpenHelper {

    public ProsseDatBase(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String pdb = "CREATE TABLE  " + DATABASE_TABLE + " ( " + RAW_ID
                + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
RAW_COMPNAMEADD
                + " TEXT, " + RAW_CASH + " TEXT, " + RAW_CUSTOMERS
                + " TEXT, " + RAW_STOCK + " TEXT, " + RAW_FIXED_ASSETS
                + " TEXT, " + RAW_ACCUMULTED_DEPRECIATION + " TEXT, "
                + RAW_BONDS + " TEXT, " + RAW_LOANS + " TEXT, " + 
RAW_EQUITY
                + " TEXT);";
        db.execSQL(pdb);

        String cndb = "CREATE TABLE  " + DATABASE_TABLE_SETTINGS + " ( "
                + RAW_COMPNAME_ID + " INTEGER PRIMARY KEY 
AUTOINCREMENT, "
                + RAW_COMPNAME + " TEXT);";
        db.execSQL(cndb);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE_SETTINGS);
        onCreate(db);
    }
}

public DataBase(Context c) {
    tcontext = c;
}

public DataBase open() throws SQLiteException {
    thdb = new ProsseDatBase(tcontext);
    tdb = thdb.getWritableDatabase();
    return this;
}

public SQLiteDatabase getDatabase() {
    return tdb;
}

public void close() {
    tdb.close();
}

public long createEntry(String cm, String ch, String sk, String ot,
        String fs, String an, String bs, String ls, String ey) {
    ContentValues cv = new ContentValues();
    cv.put(RAW_COMPNAMEADD, cm);
    cv.put(RAW_CASH, ch);
    cv.put(RAW_CUSTOMERS, ot);
    cv.put(RAW_STOCK, sk);
    cv.put(RAW_FIXED_ASSETS, fs);
    cv.put(RAW_ACCUMULTED_DEPRECIATION, an);
    cv.put(RAW_BONDS, bs);
    cv.put(RAW_LOANS, ls);
    cv.put(RAW_EQUITY, ey);

    return tdb.insert(DATABASE_TABLE, null, cv);
}

public String getData() {
    String[] columns = new String[] { RAW_COMPNAMEADD };
    Cursor c = tdb.query(DATABASE_TABLE, columns, null, null, null, null,
            null);
    String results = "";

    int iCM = c.getColumnIndex(RAW_COMPNAMEADD);

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
        results = results + c.getString(iCM) + "\n";
    }
    return results;
}
}

The Main Activity:

package com.nituach.nituach;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class NituachActivity extends Activity implements OnClickListener {

Button addNewBuisness;
ListView blv;
TextView tay;
String tyy;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final String TAG = NituachActivity.class.getSimpleName();
    addNewBuisness = (Button) findViewById(R.id.btnAddNewBuisness);
    addNewBuisness.setOnClickListener(NituachActivity.this);
    blv = (ListView) findViewById(R.id.listView1);
    tay = (TextView) findViewById(R.id.TempArray);

    Log.d(TAG, "All Variables was created");
    DataBase dbc = new DataBase(NituachActivity.this);
    dbc.open();

    String[] cs = new String[] { DataBase.RAW_COMPNAMEADD };

    Log.d(TAG, cs.toString());

     blv.setAdapter(new ArrayAdapter<String>(this,
     android.R.layout.simple_list_item_1, cs));

    dbc.close();


}

public void onClick(View v) {
    Intent addnbIntent = new Intent(NituachActivity.this,
            AddNewBuisness.class);
    NituachActivity.this.startActivity(addnbIntent);

}
}

1 Answer 1

3

You did not actually call getData() to get your values from your main activity.

First, change the method signature getData() to return array of string instead:

public List<String> getData() {
    String[] columns = new String[] { RAW_COMPNAMEADD };
    Cursor c = tdb.query(DATABASE_TABLE, columns, null, null, null, null,
        null);
    String results = "";
    List<String> results = new ArrayList<String>();
    int iCM = c.getColumnIndex(RAW_COMPNAMEADD);

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
        results.add(c.getString(iCM);
    }
    return results;
}

Then in your mainactivity, change to this:

DataBase dbc = new DataBase(NituachActivity.this);
dbc.open();

List<String> cs = dbc.getData();
 blv.setAdapter(new ArrayAdapter<String>(this,
 android.R.layout.simple_list_item_1, cs));
Sign up to request clarification or add additional context in comments.

1 Comment

Hi you have two variables called results, one is List<String> and another is String

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.