0

我是 android 开发的初学者,我正在尝试从不同的活动添加列表项值,我编写了一个代码,我可以从活动内部添加列表项,但不能从活动外部添加

测试数据库活动.java

package com.laith.sql;

import java.util.List;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;

public class TestDatabaseActivity extends ListActivity {
private CommentsDataSource datasource;
public ArrayAdapter<Comment> adapter;
private EditText insert_et;
public Comment comment;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sqlite);

    insert_et =(EditText)findViewById(R.id.editText1);
    datasource = new CommentsDataSource(this);
    datasource.open();

    List<Comment> values = datasource.getAllComments();

    // Use the SimpleCursorAdapter to show the
    // elements in a ListView
    adapter = new ArrayAdapter<Comment>(this,
            android.R.layout.simple_list_item_1, values);
    setListAdapter(adapter);
}

// Will be called via the onClick attribute
// of the buttons in main.xml
public void onClick(View view) {
    ArrayAdapter<Comment> adapter = (ArrayAdapter<Comment>) getListAdapter();
    comment = null;
    switch (view.getId()) {
    case R.id.add:
                    MySingleton mys = MySingleton.getInstance();

        String test = mys.getInstance().getMyStrings();
        addComment(test);
        break;
    case R.id.delete:
        if (getListAdapter().getCount() > 0) {
            comment = (Comment) getListAdapter().getItem(0);
            datasource.deleteComment(comment);
            adapter.remove(comment);
        }
        break;
    }
    adapter.notifyDataSetChanged();
}

@Override
protected void onResume() {
    datasource.open();
    super.onResume();
}

@Override
protected void onPause() {
    datasource.close();
    super.onPause();
}

/// method used to add a comment from a different directory 
public void addComment(String LastComment)
{
    // code here to be called by another activity

                ArrayAdapter<Comment> adapter = (ArrayAdapter<Comment>)getListAdapter();
        comment = datasource.createComment(LastComment);
        adapter.add(comment);
        adapter.notifyDataSetChanged();
           // am getting a null value for comment when I call the function from another activity


}

}

另一个活动中的测试按钮

public Button.OnClickListener Test_button = new Button.OnClickListener() 
 {
     public void onClick(View v)
     {
         String verify_string="test";
         MySingleton mys = MySingleton.getInstance();
         mys.setMyStrings(verify_string);

         TestDatabaseActivity tdba = new TestDatabaseActivity();
         tdba.addComment(mys.getMyStrings());
     }
 };

请帮忙 !:)

谢谢

<---------------------------- 工作解决方案-------------------- -------------------------> 新类创建

  public class ConnectToDB 
{
private CommentsDataSource datasource;

public void addCommentToDB(Context context, String new_comment)
{
    datasource = new CommentsDataSource(context);
    datasource.open();
    datasource.createComment(new_comment);  
}

}

onResume 已编辑

protected void onResume() {
    datasource.open();
    values = datasource.getAllComments();
    adapter = new ArrayAdapter<Comment>(this,
            android.R.layout.simple_list_item_1, values);
    setListAdapter(adapter);
    adapter.notifyDataSetChanged();

    super.onResume();
}
4

2 回答 2

1

应该像您在 onClick 部分中那样工作:

public void addComment(String LastComment)
{
    ArrayAdapter<Comment> adapter = (ArrayAdapter<Comment>) getListAdapter();
    comment = datasource.createComment(LastComment);
    adapter.add(comment);
    adapter.notifyChange();
}
于 2012-04-04T21:28:11.263 回答
0

使用android bundles,将额外的值从你的活动中放到另一个。

或者只创建一个单例并从您的两个活动中使用它。创建一个字段,例如 listViewStrings,将字符串存储在其中,然后从另一个活动中获取它们(通过访问单个(单例)实例)

创建一个新类:

public class MySingleton {
// ----------------------------------------------------------------------
// Properties
// ----------------------------------------------------------------------

private String[] myStrings;

// ----------------------------------------------------------------------
// Public methods
// ----------------------------------------------------------------------

public void setMyStrings(String[] myStrings) {
          this.myStrings = myStrings;
}

public String[] getMyStrings() {
          return myStrings;
}

// ----------------------------------------------------------------------
// Singleton object
// ----------------------------------------------------------------------

private static MySingleton instance = new MySingleton();

/**
 * Close public access to the constructor
 */
private MySingleton() {
}

/**
 * Gets object's singleton instance
 * 
 * @return singleton instance
 */
public static MySingleton getInstance() {
    return instance;
}

}

并在第一个活动中使用:

MySingleton mys = MySingleton.getInstance();
mys.setMyStrings(new String[] {"element1", "element2", "element3"});

在第二个:

MySingleton mys = MySingleton.getInstance();
String[] mySavedStrings = ys.getMyStrings();
于 2012-04-04T21:27:49.757 回答