qq_39216163 2018-06-17 03:33 采纳率: 100%
浏览 905
已采纳

关于 安卓数据库查询问题 怎样将查询到的信息存入数组 再用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条回答 默认 最新

  • dabocaiqq 2018-06-18 02:32
    关注
     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);
    }
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(7条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀