java - 在项目点击时发送字符串

标签 java android database listview

我想将 ListView 项中的字符串值发送到第二个 Activity 。但是第二个 Activity 没有得到值(value)。我应该怎么做才能发送所需的信息?这是获取所需元素 ID 的正确方法吗?请告诉我。 第一个 Activity 代码

private static final int CM_DELETE_ID = 1;
ListView lvData;
DB2 db;
SimpleCursorAdapter scAdapter;
Cursor cursor;
Button bt;
SharedPreferences bh;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test4);
    bt = (Button)findViewById(R.id.button);
    bh = getSharedPreferences("O", Context.MODE_PRIVATE);
    db = new DB2(this);
    db.open();



    cursor = db.getAllData();
    startManagingCursor(cursor);

    String[] from = new String[] { DB2.COLUMN_IMG, DB2.COLUMN_TXT, DB2.COLUMN_NMB };
    int[] to = new int[] { R.id.ivImg, R.id.tvText, R.id.tvNmb };

    scAdapter = new SimpleCursorAdapter(this, R.layout.item, cursor, from, to);
    lvData = (ListView) findViewById(R.id.lvData);
    lvData.setAdapter(scAdapter);

    registerForContextMenu(lvData);
    lvData.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                                long id) {
            //THIS IS A PART WHERE I NEED HELP!
            final SharedPreferences.Editor editor = bh.edit();
            editor.putString("name", DB2.COLUMN_TXT);
            editor.putString("number",DB2.COLUMN_NMB);
            Intent intent = new Intent(TEST4.this,TEST6.class);
            startActivity(intent);
        }
    });

    lvData.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {



        public void onItemSelected(AdapterView parentView, View childView,
                                   int position, long id) {
            setContentView(R.layout.item);
        }

        public void onNothingSelected(AdapterView parentView) {

        }
    });


}

private void buttonClick()
{
    startActivity(new Intent("com.example.dc2.TEST5"));
}


public void onButtonClick(View view) {
    switch(view.getId())
    {
        case R.id.button: {
            buttonClick();
            break;
        }
    }
}

public void onCreateContextMenu(ContextMenu menu, View v,
                                ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.add(0, CM_DELETE_ID, 0, R.string.delete_record);
}


public boolean onContextItemSelected(MenuItem item) {
    if (item.getItemId() == CM_DELETE_ID) {
        AdapterView.AdapterContextMenuInfo acmi = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        db.delRec(acmi.id);
        cursor.requery();
        return true;
    }
    return super.onContextItemSelected(item);
}

protected void onDestroy() {
    super.onDestroy();
    db.close();
}

第二个 Activity 代码

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test6);
    SharedPreferences bh;
    bh = getSharedPreferences("O", Context.MODE_PRIVATE);
    TextView tv = (TextView)findViewById(R.id.textView2);
    String name = bh.getString("name","Hello");
    String number = bh.getString("number","HELLO");
    tv.setText(name + " " + number);
}

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

最佳答案

使用 Intent 而不是 SharedPreferences 来 bundle 您的 string 并将其传递给 Activity2

要么创建一个 Bundle 对象并将您的字符串放入 Bundle 对象中,要么直接将您的字符串放入 Intent(这将隐式创建一 bundle 给你)

方法一

Activity1中,

    public void onItemClick(AdapterView<?> parent, View view, int position,
                            long id) {
        //THIS IS A PART WHERE I NEED HELP!
        Bundle bundle = new Bundle();
        bundle.putString("name", cursor.getString(cursor.getColumnIndex(DB2.COLUMN_TXT));
        bundle.putString("number", cursor.getString(cursor.getColumnIndex(DB2.COLUMN_NMB));
        Intent intent = new Intent(TEST4.this,TEST6.class);
        intent.putExtras(bundle);
        startActivity(intent);
    }
});

Activity2中,

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test6);
    Bundle bundle = getIntent().getExtras();
    String name = bundle.getString("name");
    String number = bundle.getString("number");
    TextView tv = (TextView)findViewById(R.id.textView2);
    tv.setText(name + " " + number);
}

关于java - 在项目点击时发送字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32672246/

相关文章:

java - 缺少本地 jar 的传递性 Maven 依赖

java - 在 HashMap 中搜索给定键的值

android - flutter android - 无法列出 com.google.android.gms :play-services-location 的版本

android - 从设备 : EOF 获取屏幕截图时出现意外错误

mysql - 次要关系的唯一性约束

java - 在消息的对象字段中发送消息

java - 用于 WebSphere 和 Maven 的 deployment.xml

android - 如何使用 Android adb logcat 按标签名称排除某些消息?

node.js - 在不知道索引的情况下使用 NodeJS 更新 MongoDB 中数组中的单个元素

c# - EF 使用新引用更新现有实体并获取违反唯一键约束