如何“导出保存的数据并将该数据传输到Android中的新活动”

时间:2019-01-31 04:22:54

标签: android sqlite

我将SQLite数据保存在android中;但是,当我调出数据以将其传输到另一个活动时,它仅通过另一个活动传输数据库中的最后数据。我需要在哪里设置编码?谢谢。

这是一个Android Studio 3.0和SQLite。过去,我尝试过 在SQLite数据库中导出保存的数据,并将该数据传输到新活动。我总是只拥有数据库中的最新数据。


我在主要活动中的代码

 private void xuLyXemLuotLike(View view) {
        database = openOrCreateDatabase(DATABASE_NAME, MODE_PRIVATE, null);
        Cursor cursor = database.query("ArirangSongList", null, null, null, null, null, null);
        //dsBaiHatGoc.clear();
        int soLike = 0;
        int soDislike = 0;
        String mabh ="";
        String tenbh = "";
        String casi = "";
        Music music = new Music();
        while (cursor.moveToNext()) {
            mabh = cursor.getString(0);
            tenbh = cursor.getString(1);
            casi = cursor.getString(3);
            soLike = cursor.getInt(6);
            soDislike = cursor.getInt(7);
            music.setMa(mabh);
            music.setTen(tenbh);
            music.setCaSi(casi);
            music.setSoLike(soLike);
            music.setSoDislike(soDislike);
          //  dsBaiHatGoc.add(music);
        }
        cursor.close();
            Intent intent = new Intent(MainActivity.this, XemLuotLikeActivity.class);
            intent.putExtra("mabh",mabh);
            intent.putExtra("tenbh", tenbh);
            intent.putExtra("casi", casi);
            intent.putExtra("soLike", soLike);
            intent.putExtra("soDislike", soDislike);
            startActivity(intent);
        }

我在另一个活动中的代码:

    txtKetQua= (TextView) this.<View>findViewById(R.id.txtKetQua);
    Intent intent =getIntent();

    String mabh =intent.getStringExtra("mabh");

    String tenbh =intent.getStringExtra("tenbh");
    String casi =intent.getStringExtra("casi");

    int soLike =intent.getIntExtra("soLike", 0);
    int soDislike =intent.getIntExtra("soDislike",0);

    txtKetQua.setText(mabh + "   " + tenbh + "   " + casi + "   " + soLike+ "   " + soDislike);

My code in Another Activity

My code in Main Activity

2 个答案:

答案 0 :(得分:0)

每次访问行时,它都使用相同的变量,因此值将被覆盖,因此仅最后一行用于设置通过意图传递的值。

除非您想要许多意图(例如,在键名后附加到位置),否则最好在其他活动中从数据库中检索数据。

您可以使用诸如:-

之类的东西发送许多intentExtras(每行一组)以及行数计数器
private void xuLyXemLuotLike(View view) {
    SQLiteDatabase database = openOrCreateDatabase(DATABASE_NAME, MODE_PRIVATE, null);
    Cursor cursor = database.query("ArirangSongList", null, null, null, null, null, null);

    //dsBaiHatGoc.clear();
    int soLike = 0;
    int soDislike = 0;
    String mabh ="";
    String tenbh = "";
    String casi = "";
    Music music = new Music();
    Intent intent = new Intent(MainActivity.this, XemLuotLikeActivity.class);
    intent.putExtra("rowcounter",cursor.getCount()); //<<<<<<<<<< number of rows
    while (cursor.moveToNext()) {
        intent.putExtra("mabh"+String.valueOf(cursor.getPosition()),cursor.getString(0));
        intent.putExtra("tenbh"+String.valueOf(cursor.getPosition()),cursor.getString(1));
        intent.putExtra("casi"+String.valueOf(cursor.getPosition()),cursor.getString(3));
        intent.putExtra("soLike"+String.valueOf(cursor.getPosition()),cursor.getInt(6));
        intent.putExtra("soDislke"+String.valueOf(cursor.getPosition()),cursor.getInt(7));
    }
    cursor.close();
    startActivity(intent);
}
  • 这将发送键为mabh0 mabh1 mabh2 ........
  • 的intentExtras

然后您可以使用类似以下内容的数据:-

    Intent intent = getIntent();
    int rowcounter = intent.getIntExtra("rowcounter",-1);

    Music[] allthemusicpass = new Music[rowcounter];
    for (int i = 0; i < rowcounter; i++) {
        allthemusicpass[i].setMa(intent.getStringExtra("mabh"+String.valueOf(i)));
        allthemusicpass[i].setTen(intent.getStringExtra("tenbh"+String.valueOf(i)));
        allthemusicpass.setCaSi(intent.getStringExtra("casi"+String.valueOf(i)));
        allthemusicpass.setSoLike(intent.getIntExtra("soLike"+String.valueOf(i),-1));
        allthemusicpass.setSoDislike(intent.getIntExtra("soDislike"+String.valueOf(i),-1));
    }

答案 1 :(得分:0)

尝试

def mirror_tree_iterative(root):
    if root is None:
        return

    q = Queue()
    q.enqueue(root)

    while(not q.is_empty()):
        curr = q.peek()
        q.dequeue()
        curr.left, curr.right = curr.right, curr.left
        if curr.left:
            q.enqueue(curr.left)
        if curr.right:
            q.enqueue(curr.right)

然后在XemLuotLikeActivity活动中oncreate()

    ArrayList<Music> musicList = new ArrayList<Music>();
    while (cursor.moveToNext()) {

       // first add implements Serializable to Music class
       // make constructor with respective parameters
         Music music = new Music(cursor.getString(0), cursor.getString(1), cursor.getString(3), cursor.getString(6), cursor.getString(7));

        musicList.add(music);
    }
    cursor.close();

   Intent intent = new Intent(MainActivity.this, XemLuotLikeActivity.class);
   intent.putExtra("musicList",musicList);            
   startActivity(intent);