0

I have a problem in adding values to my String[] array from another class. What I wanted to do is to add values to my MainScreenEntered.java class from database through my MyDatabaseAdapter.java class.

PLease help.

THis is my code.

MainScreenentered.java

public class MainScreenEntered extends Activity implements OnItemClickListener{
    @SuppressLint("NewApi")
    ListView lv;
    List<RowItem> rowItems;
    MyDatabaseAdapter mh;
    String username;
    ListView listViewSMS;
    Cursor cursor;
    Context context;

    public static String[] names = new String[] {};
    public static String[] descriptions = new String[] {};
    public static String[] pics = new String[] {};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_screen_entered);
        mh = new MyDatabaseAdapter(this);
        Bundle bundle = this.getIntent().getExtras();
        username = bundle.getString("username");
        Message.message(this, username);

        try
        {
        mh.getDataToDatabase();

        rowItems = new ArrayList<RowItem>();

        for(int i =0; i< names.length; i++)
        {
            RowItem item = new RowItem(pics[i], names[i], descriptions[i]);
            rowItems.add(item);
        }

        lv = (ListView)findViewById(R.id.lvEntries);
        CustomListViewAdapter adapter = new CustomListViewAdapter(this, R.layout.list_layout,rowItems);
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(this);
        }
        catch(Exception e)
        {
            Message.message(this, ""+e);
        }




    }
}

MyDatabaseAdapter.java

public class MyDatabaseAdapter {
    MyHelper helper;

    public MyDatabaseAdapter (Context context)
    {
        helper = new MyHelper(context);

    }


    public void getDataToDatabase()
    {
        MainScreenEntered ms = new MainScreenEntered();
        SQLiteDatabase db = helper.getWritableDatabase();
        String columns[] = {MyHelper.ENTRY_FULLNAME,MyHelper.ENTRY_DESCRIPTION,MyHelper.ENTRY_IMAGE};
        Cursor c=db.query(MyHelper.TABLE_ENTRIES, columns, null, null, null, null, null);

        int i=0;
        while(c.moveToNext())
        {
        String name = c.getString(0);   
        String desc = c.getString(1);   
        String pic = c.getString(2);    

        ms.names[i] = name;
        ms.descriptions[i] = desc;
        ms.pics[i] = pic;

        i++;
        }
        db.close();
    }
}
1
  • 1
    What is the actual "problem"? Are you getting an error? Commented Mar 19, 2014 at 4:24

4 Answers 4

1

you can do like this

in onCreate
mh = new MyDatabaseAdapter(this,this);

now

public class MyDatabaseAdapter {
MyHelper helper;
MainScreenEntered ms;

public MyDatabaseAdapter (Context context, MainScreenEntered ms)
{
    helper = new MyHelper(context);
    this.ms = ms;

}
public void getDataToDatabase()
{
    SQLiteDatabase db = helper.getWritableDatabase();
    String columns[] = {MyHelper.ENTRY_FULLNAME,MyHelper.ENTRY_DESCRIPTION,MyHelper.ENTRY_IMAGE};
    Cursor c=db.query(MyHelper.TABLE_ENTRIES, columns, null, null, null, null, null);

    int i=0;
    while(c.moveToNext())
    {
    String name = c.getString(0);   
    String desc = c.getString(1);   
    String pic = c.getString(2);    

    ms.names[i] = name;
    ms.descriptions[i] = desc;
    ms.pics[i] = pic;

    i++;
    }
    db.close();
 }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try this..

In your MainScreenEntered.java

public static ArrayList<String> names = new ArrayList<String>();
public static ArrayList<String> descriptions = new ArrayList<String>();
public static ArrayList<String> pics = new ArrayList<String>();

and MyDatabaseAdapter

while(c.moveToNext())
{
    String name = c.getString(0);   
    String desc = c.getString(1);   
    String pic = c.getString(2);    

    MainScreenEntered.names.add(name);
    MainScreenEntered.descriptions.add(desc);
    MainScreenEntered.pics.add(pic);
}

then

    rowItems = new ArrayList<RowItem>();

    for(int i =0; i<names.length; i++)
    {
        RowItem item = new RowItem(pics.get(i), names.get(i), descriptions.get(i));
        rowItems.add(item);
    }

Comments

0

use c.movetoFirst() before while loop and for static array u doesn't have to create object of it u can directly call it ClassName.variable_name ... it will be a better if you use arraylist of string rather than string array.

Comments

0

Be careful by doing DB operation on the UI thread you will might have some ANR.

Comments

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.