意图没有传递正确的信息

时间:2014-03-11 20:57:31

标签: android android-intent android-activity android-sqlite

我使用意图将noteId从我的数据库传递到另一个活动,以显示它正在工作的信息,我去了项目的另一部分工作,现在它似乎只传递一个值,从玩我的约发现它似乎是ListView上下一个最高的对象。对不起,如果措辞不好,我不太擅长解释这些事情

ViewNote Class

package com.hardy.passnotes;

import java.util.HashMap;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class ViewNote extends Activity {

    EditText NewNoteTitle;
    EditText NewNoteContent;
    TextView tvNoteId;
    DBTools dbTools = new DBTools(this);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_note);
        NewNoteTitle = (EditText)findViewById(R.id.ETNewNoteTitle);
        NewNoteContent = (EditText)findViewById(R.id.ETNewNoteContent);

        Intent n = getIntent();
        String NoteId = n.getStringExtra("noteId");
        Toast.makeText(getApplicationContext(), NoteId,
               Toast.LENGTH_LONG).show();

        HashMap<String, String> NoteList = dbTools.getNoteInfo(NoteId);


        if(NoteList.size() != 0)
        {
            setTitle("Note: " + NoteList.get("noteTitle"));
            NewNoteTitle.setText(NoteList.get("noteTitle"));
            NewNoteContent.setText(NoteList.get("noteContent"));
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.view_note, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        switch (item.getItemId())
        {
        case R.id.action_Editnote:
            tvNoteId = (TextView)findViewById(R.id.NoteId);
            String NoteValue = tvNoteId.getText().toString();

            Intent intent = new Intent(getApplication(),EditNote.class);
            intent.putExtra("noteId", NoteValue);
            startActivity(intent);

        case R.id.action_DelNote:
            Intent intent1 = getIntent();
            String NoteId = intent1.getStringExtra("noteId");

            dbTools.deleteNote(NoteId);
             Intent i = new Intent(getApplication(),MyNotes.class);
                startActivity(i);
                finish();

        }
        return super.onOptionsItemSelected(item);
    }

}

MyNote类:

package com.hardy.passnotes;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class MyNotes extends ListActivity {

    DBTools dbTools = new DBTools(this);
    TextView tvNoteId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_notes);
        Log.i("Tag", "OnCreate Started As Normal");
        setTitle("My Notes");



        ArrayList <HashMap<String, String>> NoteList = dbTools.getAllNotes();

        if(NoteList.size() != 0)
        {
            ListView listView = getListView();
            listView.setOnItemClickListener(new OnItemClickListener()
            {

                @Override
                public void onItemClick(AdapterView<?> arg0, View view,
                        int position, long id) {
                    tvNoteId = (TextView)findViewById(R.id.NoteId);
                    String NoteValue = tvNoteId.getText().toString();

                    Intent intent = new Intent(MyNotes.this,ViewNote.class);
                    intent.putExtra("noteId", NoteValue);
                    startActivity(intent);

                    Log.i("Tag", "if Started As Normal");
                }

            });



        ListAdapter Adapter = new SimpleAdapter(MyNotes.this,NoteList,R.layout.notes_list, new String[]{"noteId","noteTitle",}, new int[]{R.id.NoteId,R.id.NoteTitle}  ); 
        setListAdapter(Adapter);
        Log.i("Tag", "Arrey Started As Normal");
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.my_notes, menu);
        return true;

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        switch (item.getItemId())
        {
        case R.id.action_Add:
            Intent i = new Intent(getApplicationContext(),CreateNote.class);
            startActivity(i);
            finish();

        }
        return super.onOptionsItemSelected(item);
    }

}

先谢谢你一直在搜索并调试几个小时而没有运气。

3 个答案:

答案 0 :(得分:0)

我认为在从选项菜单中点击编辑时,您的笔记会被删除。 在案例结尾处R.id.action_Editnote没有返回任何内容,也没有调用break,因此也会执行R.id.action_Delnote案例。这是您从数据库中删除注释的地方。

答案 1 :(得分:0)

您好我认为问题是您在findViewById()侦听器中不恰当地使用onItemClick方法。

您应该使用:

tvNoteId = (TextView)view.findViewById(R.id.NoteId); 

请注意,我在findViewById方法的view上调用了onItemClick方法,因为您希望从按下的项目中找到ID为NoteId的视图,不是来自整个活动内容视图..你这样做的方式是在活动的内容视图中搜索第一个与NoteId相等的ID ...

答案 2 :(得分:0)

在你的开关中,休息一下:

    switch (item.getItemId())
            {
            case R.id.action_Editnote:
                tvNoteId = (TextView)findViewById(R.id.NoteId);
                String NoteValue = tvNoteId.getText().toString();

                Intent intent = new Intent(getApplication(),EditNote.class);
                intent.putExtra("noteId", NoteValue);
                startActivity(intent);
break;

            case R.id.action_DelNote:
                Intent intent1 = getIntent();
                String NoteId = intent1.getStringExtra("noteId");

                dbTools.deleteNote(NoteId);
                 Intent i = new Intent(getApplication(),MyNotes.class);
                    startActivity(i);
                    finish();
break;

            }