【问题标题】:Send string on item click在项目点击时发送字符串
【发布时间】:2015-09-19 19:16:40
【问题描述】:

我想将列表视图中的字符串值发送到第二个活动。但是第二个活动没有得到价值。我应该怎么做才能发送所需的信息?它是获取所需物品 ID 的正确方法吗?请告诉我。 第一个活动代码

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

第二个活动代码

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

【问题讨论】:

  • 我应该添加我的数据库代码吗?

标签: java android database listview


【解决方案1】:

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

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

方法一

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

【讨论】:

  • 不是我真正想要的 + 现在它只是崩溃了。我想要什么 - 如果我在 listView 上单击包含字符串“John”的项目,它应该在第二个活动中显示“John”
  • 使用您的代码 sn-p 编辑了答案。现在,您仍需要在该位置将 DB2.COLUMN_TXT 替换为 actual item(通过查询光标)。
  • 也许我应该编辑我的答案并在其中添加数据库代码?
  • 您可以在我的代码中看到“startActivity(new Intent("com.example.dc2.TEST5"));”。这是我输入字符串和 int 的活动
  • 这就是我在日志中得到的。无法启动活动 ComponentInfo{com.example.dc2/com.example.dc2.TEST6}:java.lang.NullPointerException:尝试调用虚拟方法 'java.lang.String android.os.Bundle.getString(java.lang. String)' 在空对象引用上
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-18
  • 1970-01-01
  • 1970-01-01
  • 2014-09-24
  • 2018-07-25
  • 1970-01-01
相关资源
最近更新 更多