关于 安卓数据库查询问题 怎样将查询到的信息存入数组 再用bundler传递
        灌水  无聊的别来   别搞事情   

        我就想花C找人帮忙解决问题   丢一句话的你就别发了  没有代码  你也别发了  

        多看下我的问题描述   你有好的方法也可以贴代码给我看  

我想说一下我写的东西的功能 就是在一个有很多imagebutton的页面 点击一个imagebtn 通过Switch 选择后 在新的页面输出对应的

图片 和 文字描述 。文字的描述是通过数据库查找出来的!

我现在已经实现了图片传递 ,但是文字查询传递我还没有实现

我的想法是 定义一个方法
public String query(int i){

Cursor cursor = db.rawQuery("select content from car ");
然后定义一个数组 将cursor查询到的内容存入数组
再通过query( int i )给的i找到数组对应的内容
最后return

}

如果是我的想法 有问题 希望大佬能告诉下这个该用什么方法解决 顺便写一下方法代码

            全部代码

            MainActivity.java

            public class MainActivity extends AppCompatActivity {

private ImageButton ib0;
private MyOpenHelper myOpenHelper;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    this.myOpenHelper = new MyOpenHelper(this);
    myOpenHelper.getWritableDatabase();

    ib0 = (ImageButton)findViewById(R.id.ib0);
    ib0.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View view) {
            Intent i0 = new Intent(MainActivity.this,Main2Activity.class);
            startActivity(i0);
        }
    });

}
    }

    Main2Activity.java

    public class Main2Activity extends Activity implements View.OnClickListener {

private ImageButton ib1,ib2,ib3,ib4,ib5,ib6,ib7,ib8,ib9,ib10,ib11,ib12;
private SQLiteDatabase db;
private Cursor cursor;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

    ib1 = findViewById(R.id.ib1);
    ib2 = findViewById(R.id.ib2);
    ib3 = findViewById(R.id.ib3);
    ib4 = findViewById(R.id.ib4);
    ib5 = findViewById(R.id.ib5);
    ib6 = findViewById(R.id.ib6);
    ib7 = findViewById(R.id.ib7);
    ib8 = findViewById(R.id.ib8);
    ib9 = findViewById(R.id.ib9);
    ib10 = findViewById(R.id.ib10);
    ib11 = findViewById(R.id.ib11);
    ib12 = findViewById(R.id.ib12);

    ib1.setOnClickListener(this);
    ib2.setOnClickListener(this);
    ib3.setOnClickListener(this);
    ib4.setOnClickListener(this);
    ib5.setOnClickListener(this);
    ib6.setOnClickListener(this);
    ib7.setOnClickListener(this);
    ib8.setOnClickListener(this);
    ib9.setOnClickListener(this);
    ib10.setOnClickListener(this);
    ib11.setOnClickListener(this);
    ib12.setOnClickListener(this);





}



@Override
public void onClick(View view) {
    switch (view.getId()){
        case R.id.ib1:
            Intent i1 = new Intent(Main2Activity.this,Main3Activity.class);


            Bundle bundle1 = new Bundle();
            bundle1.putInt("imagebtn",R.mipmap.a);
           // bundle1.putString("text",query(0));
            i1.putExtras(bundle1);
            startActivity(i1);
            break;
        case R.id.ib2:
            Intent i2 = new Intent(Main2Activity.this,Main3Activity.class);
            Bundle bundle2 = new Bundle();
            bundle2.putInt("imagebtn",R.mipmap.b);
            i2.putExtras(bundle2);
            startActivity(i2);
            break;
        case R.id.ib3:
            Intent i3 = new Intent(Main2Activity.this,Main3Activity.class);
            Bundle bundle3 = new Bundle();
            bundle3.putInt("imagebtn",R.mipmap.c);
            i3.putExtras(bundle3);
            startActivity(i3);
            break;
        case R.id.ib4:
            Intent i4 = new Intent(Main2Activity.this,Main3Activity.class);
            Bundle bundle4 = new Bundle();
            bundle4.putInt("imagebtn",R.mipmap.d);
            i4.putExtras(bundle4);
            startActivity(i4);
            break;
        case R.id.ib5:
            Intent i5 = new Intent(Main2Activity.this,Main3Activity.class);
            Bundle bundle5 = new Bundle();
            bundle5.putInt("imagebtn",R.mipmap.e);
            i5.putExtras(bundle5);
            startActivity(i5);
            break;
        case R.id.ib6:
            Intent i6 = new Intent(Main2Activity.this,Main3Activity.class);
            Bundle bundle6 = new Bundle();
            bundle6.putInt("imagebtn",R.mipmap.f);
            i6.putExtras(bundle6);
            startActivity(i6);
            break;
        case R.id.ib7:
            Intent i7 = new Intent(Main2Activity.this,Main3Activity.class);
            Bundle bundle7 = new Bundle();
            bundle7.putInt("imagebtn",R.mipmap.g);
            i7.putExtras(bundle7);
            startActivity(i7);
            break;
        case R.id.ib8:
            Intent i8 = new Intent(Main2Activity.this,Main3Activity.class);
            Bundle bundle8 = new Bundle();
            bundle8.putInt("imagebtn",R.mipmap.h);
            i8.putExtras(bundle8);
            startActivity(i8);
            break;
        case R.id.ib9:
            Intent i9 = new Intent(Main2Activity.this,Main3Activity.class);
            Bundle bundle9 = new Bundle();
            bundle9.putInt("imagebtn",R.mipmap.i);
            i9.putExtras(bundle9);
            startActivity(i9);
            break;
        case R.id.ib10:
            Intent i10 = new Intent(Main2Activity.this,Main3Activity.class);
            Bundle bundle10 = new Bundle();
            bundle10.putInt("imagebtn",R.mipmap.j);
            i10.putExtras(bundle10);
            startActivity(i10);
            break;
        case R.id.ib11:
            Intent i11 = new Intent(Main2Activity.this,Main3Activity.class);
            Bundle bundle11 = new Bundle();
            bundle11.putInt("imagebtn",R.mipmap.k);
            i11.putExtras(bundle11);
            startActivity(i11);
            break;
        case R.id.ib12:
            Intent i12 = new Intent(Main2Activity.this,Main3Activity.class);
            Bundle bundle12 = new Bundle();
            bundle12.putInt("imagebtn",R.mipmap.l);
            i12.putExtras(bundle12);
            startActivity(i12);
            break;
    }

}

    Main3Activity.java

    public class Main3Activity extends AppCompatActivity {

private ImageView imageView;
private TextView textView;
private int i1;
private String i2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main3);

    imageView = (ImageView) findViewById(R.id.iv);
    textView = (TextView)findViewById(R.id.textv) ;
    Intent intent = getIntent();
    if (intent != null) {
        Bundle bundle = intent.getExtras();
        i1 = bundle.getInt("imagebtn");
        i2 = bundle.getString("text");
    }
    imageView.setImageResource(i1);
    textView.setText(i2);
}

    }

    public class MyOpenHelper extends SQLiteOpenHelper {

public MyOpenHelper(Context context){
    super(context,"car.db",null,1);
}


@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table [user] ([id] integer not null primary key autoincrement,[name] varchar(255) not null,[content] varchar(255) not null)");
}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

}
}

8个回答

 MainActivity.java

            public class MainActivity extends AppCompatActivity {

private ImageButton ib0;
private MyOpenHelper myOpenHelper;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    this.myOpenHelper = new MyOpenHelper(this);
    myOpenHelper.getWritableDatabase();

    ib0 = (ImageButton)findViewById(R.id.ib0);
    ib0.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View view) {
            Intent i0 = new Intent(MainActivity.this,Main2Activity.class);
            startActivity(i0);
        }
    });

}
    }

    Main2Activity.java

    public class Main2Activity extends Activity implements View.OnClickListener {

private ImageButton ib1,ib2,ib3,ib4,ib5,ib6,ib7,ib8,ib9,ib10,ib11,ib12;
private SQLiteDatabase db;
private Cursor cursor;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

    ib1 = findViewById(R.id.ib1);
    ib2 = findViewById(R.id.ib2);
    ib3 = findViewById(R.id.ib3);
    ib4 = findViewById(R.id.ib4);
    ib5 = findViewById(R.id.ib5);
    ib6 = findViewById(R.id.ib6);
    ib7 = findViewById(R.id.ib7);
    ib8 = findViewById(R.id.ib8);
    ib9 = findViewById(R.id.ib9);
    ib10 = findViewById(R.id.ib10);
    ib11 = findViewById(R.id.ib11);
    ib12 = findViewById(R.id.ib12);

    ib1.setOnClickListener(this);
    ib2.setOnClickListener(this);
    ib3.setOnClickListener(this);
    ib4.setOnClickListener(this);
    ib5.setOnClickListener(this);
    ib6.setOnClickListener(this);
    ib7.setOnClickListener(this);
    ib8.setOnClickListener(this);
    ib9.setOnClickListener(this);
    ib10.setOnClickListener(this);
    ib11.setOnClickListener(this);
    ib12.setOnClickListener(this);





}



@Override
public void onClick(View view) {
    switch (view.getId()){
        case R.id.ib1:
            Intent i1 = new Intent(Main2Activity.this,Main3Activity.class);


            Bundle bundle1 = new Bundle();
            bundle1.putInt("text",query(你的id)); //不知道你的按钮都是做什么的,这里改你需要修改的,或者两个都传,保留imagebtn
            i1.putExtras(bundle1);
            startActivity(i1);
            break;
        case R.id.ib2:
            Intent i2 = new Intent(Main2Activity.this,Main3Activity.class);
            Bundle bundle2 = new Bundle();
            bundle1.putInt("text",query(你的id));
            i2.putExtras(bundle2);
            startActivity(i2);
            break;
        case R.id.ib3:
            Intent i3 = new Intent(Main2Activity.this,Main3Activity.class);
            Bundle bundle3 = new Bundle();
            bundle1.putInt("text",query(你的id));
            i3.putExtras(bundle3);
            startActivity(i3);
            break;
        case R.id.ib4:
            Intent i4 = new Intent(Main2Activity.this,Main3Activity.class);
            Bundle bundle4 = new Bundle();
            bundle1.putInt("text",query(你的id));
            i4.putExtras(bundle4);
            startActivity(i4);
            break;
        case R.id.ib5:
            Intent i5 = new Intent(Main2Activity.this,Main3Activity.class);
            Bundle bundle5 = new Bundle();
            bundle1.putInt("text",query(你的id));
            i5.putExtras(bundle5);
            startActivity(i5);
            break;
        case R.id.ib6:
            Intent i6 = new Intent(Main2Activity.this,Main3Activity.class);
            Bundle bundle6 = new Bundle();
            bundle1.putInt("text",query(你的id));
            i6.putExtras(bundle6);
            startActivity(i6);
            break;
        case R.id.ib7:
            Intent i7 = new Intent(Main2Activity.this,Main3Activity.class);
            Bundle bundle7 = new Bundle();
            bundle7.putInt("text",R.mipmap.g);
            i7.putExtras(bundle7);
            startActivity(i7);
            break;
        case R.id.ib8:
            Intent i8 = new Intent(Main2Activity.this,Main3Activity.class);
            Bundle bundle8 = new Bundle();
            bundle1.putInt("text",query(你的id));
            i8.putExtras(bundle8);
            startActivity(i8);
            break;
        case R.id.ib9:
            Intent i9 = new Intent(Main2Activity.this,Main3Activity.class);
            Bundle bundle9 = new Bundle();
            bundle9.putInt("imagebtn",R.mipmap.i);
            i9.putExtras(bundle9);
            startActivity(i9);
            break;
        case R.id.ib10:
            Intent i10 = new Intent(Main2Activity.this,Main3Activity.class);
            Bundle bundle10 = new Bundle();
            bundle10.putInt("imagebtn",R.mipmap.j);
            i10.putExtras(bundle10);
            startActivity(i10);
            break;
        case R.id.ib11:
            Intent i11 = new Intent(Main2Activity.this,Main3Activity.class);
            Bundle bundle11 = new Bundle();
            bundle11.putInt("imagebtn",R.mipmap.k);
            i11.putExtras(bundle11);
            startActivity(i11);
            break;
        case R.id.ib12:
            Intent i12 = new Intent(Main2Activity.this,Main3Activity.class);
            Bundle bundle12 = new Bundle();
            bundle12.putInt("imagebtn",R.mipmap.l);
            i12.putExtras(bundle12);
            startActivity(i12);
            break;
    }
public ArrayList query(int i){
ArrayList al = new ArrayList();
String m = "";
Cursor cursor = db.rawQuery("select content from car where id=?",new String[]{s});
cursor.moveToNext()){  
al.add(cursor.getString(0));
}
return al;
}

    Main3Activity.java

    public class Main3Activity extends AppCompatActivity {

private ImageView imageView;
private TextView textView;
private int i1;
private String i2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main3);

    imageView = (ImageView) findViewById(R.id.iv);
    textView = (TextView)findViewById(R.id.textv) ;
    Intent intent = getIntent();
    if (intent != null) {
        Bundle bundle = intent.getExtras();
        i1 = bundle.getInt("imagebtn");
        i2 = bundle.getString("text");
    }
    imageView.setImageResource(i1);
    textView.setText(i2);
}
}


dabocaiqq
请你们大家多多姿瓷我菠菜菌 看下面
一年多之前 回复
dabocaiqq
请你们大家多多姿瓷我菠菜菌 回复qq_39216163: 嗯,我修改了下
一年多之前 回复
qq_39216163
qq_39216163 你的 query()方法是ArrayList型的 怎样在下面用的是 putint 这跟昨天贴的方法一样都没改啊 我上面的putint 是传递的图片 我需要在下面在传递查询的内容是文字
一年多之前 回复

将你在数据库查询到的数据通过startActivity()函数传到新的activity不就行了 简单

qq_39216163
qq_39216163 有具体代码吗
一年多之前 回复

和你传输图片的方式一样,在 bundle1.putInt("imagebtn",R.mipmap.a) 后面添加 bundle1.putString("string",describe);在接收的Activity里处理方式也是一样的。

qq_39216163
qq_39216163 describe 这个方法怎么写
一年多之前 回复

你没有必要再这个Activity查询传过去,一般处理只传一个id之类的值过去,然后到另一个activity查询后进行加载,数据库如果查询的是文字描述
就传递一个String就行了,这里你说的方式使用 putStringArray()和getStringArrayExtra()也能实现。如果要数据库查询的代码,需要你多贴点代码才能知道逻辑

qq_39216163
qq_39216163 代码全贴了 你看看 我是在这个activity2中对按钮进行选着后 将图片和查询到的结果 一起传递到activity3里面进行输出
一年多之前 回复
 private ImageButton ib1,ib2;

            protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

    ib1 = findViewById(R.id.ib1);
    ib2 = findViewById(R.id.ib2);
            }

            @Override
public void onClick(View view) {
    switch (view.getId()){
        case R.id.ib1:
            Intent i1 = new Intent(Main2Activity.this,Main3Activity.class);
            Bundle bundle1 = new Bundle();
            bundle1.putInt("imagebtn",query(你的id));
            i1.putExtras(bundle1);
            startActivity(i1);
            break;
        case R.id.ib2:
            Intent i2 = new Intent(Main2Activity.this,Main3Activity.class);
            Bundle bundle2 = new Bundle();
            bundle2.putInt("imagebtn",query(你的id));
            i2.putExtras(bundle2);
            startActivity(i2);
            break;
                            }

然后query
public ArrayList query(int i){
ArrayList al = new ArrayList();
String m = "";
Cursor cursor = db.rawQuery("select content from car where id=?",new String[]{s});
cursor.moveToNext()){  
al.add(cursor.getString(0));
}
return al;

}
qq_39216163
qq_39216163 我把所有java代码都贴上来了 你看看吧
一年多之前 回复
qq_39216163
qq_39216163 回复qq_39216163: public ArrayList query(int i){ String s = String.valueOf(i); ArrayList al = new ArrayList(); String m = ""; Cursor cursor = db.rawQuery("select content from car where id=?",new String[]{s}); while(cursor.moveToNext()) { al.add(cursor.getString(0)); } return al; } 这样写后不报错了 但程序还是运行不了
一年多之前 回复
qq_39216163
qq_39216163 你这个光把查询的数据传过去 我原先的图片呢?
一年多之前 回复
 另外一个页面用
Bundle bundle = getIntent().getExtras(); 


ParcelableData parcelableData = bundle.getParcelable("imagebtn");  


ArrayList list = bundle.getObject("imagebtn"); 
for(int i = 0;i < list.size(); i ++){
            System.out.println(list.get(i));
        }

public String query(int i){
String m = "";
Cursor cursor = db.rawQuery("select content from car where id=?",new String[]{s});
cursor.moveToNext()){

m = m + cursor.getString(0) + " ";
}
return al;
}

        Intent i1 = new Intent(Main2Activity.this,Main3Activity.class);
        Bundle bundle1 = new Bundle();
        bundle1.putInt("imagebtn",R.mipmap.a);
        bundle1.putString("text",query(查询的id));
        i1.putExtras(bundle1);
        startActivity(i1);


                    public String query(int i){
              String m = "";
              Cursor cursor = db.rawQuery("select content from car where id=?",new String[]{s});
              cursor.moveToNext()
              return cursor.getString(0);
         }
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!