Android - 通过另一个类向String []添加值

时间:2014-03-19 04:19:59

标签: java android arrays database string

我在从另一个类向我的String []数组添加值时遇到问题。我想要做的是通过MyDatabaseAdapter.java类从数据库向我的MainScreenEntered.java类添加值。

提供帮助。

这是我的代码。

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

4 个答案:

答案 0 :(得分:1)

你可以这样做

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

现在

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

答案 1 :(得分:0)

试试这个..

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>();

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

然后

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

答案 2 :(得分:0)

在while循环之前使用c.movetoFirst()而对于静态数组你不必创建它的对象你可以直接调用它ClassName.variable_name ...如果你使用字符串的arraylist它会更好而不是字符串数组。

答案 3 :(得分:0)

在UI线程上进行数据库操作时要小心,你可能会有一些ANR。