【问题标题】:How can I pass a ListView contaning imageView and textView to other Activity如何将包含 imageView 和 textView 的 ListView 传递给另一个 Activity
【发布时间】:2017-08-02 20:41:21
【问题描述】:

我有 2 个不同的活动,但它们都有一个 listview。我的第一个活动在数据方面是空的。它只有一个用于传递给我的第二个活动的按钮。

在我的第二个活动中,每一行都包含一个 imageView、textView 和 checkBox。 当我在第二个活动的任何行中激活复选框时,我想将数据发送到我的第一个活动。例如,我单击一个复选框以选择一行,当我返回第一个活动时,我应该看到这一行(带有 ImageView、TextView)。实际上,此外,我想在复选框未激活时删除第一个活动中的数据。但是,我想如果你帮我解决我的第一个问题,我可以找到这个解决方案。

我的第一个活动(我应该在这里看到行)

   ImageView image = (ImageView) findViewById(R.id.image);
    Bundle bundleObject = getIntent().getExtras();
    if (bundleObject != null) {
        String resultText = bundleObject.getString("RESULT_TEXT");
        dbHelper.insertNewTask(resultText);
        byte[] byteArray = bundleObject.getByteArray("picture");
        Bitmap b = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
        image.setImageBitmap(b); //ERROR IS HERE!!!!! IT ISN'T WORKING

我的第二个活动

    byte[] byteArray;
    Bitmap bmp;
    int[] images = {R.drawable.book, R.drawable.watchingtv};
    bmp = BitmapFactory.decodeResource(getResources(), R.drawable.book);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byteArray  = stream.toByteArray();
class MyAdapter extends ArrayAdapter<String> implements View.OnClickListener {
    Context context;
    int[] images;
    String[] mytitles;

    MyAdapter(Context c, String[] titles, int images[]) {
        super(c, R.layout.row_2, R.id.newTaskTitle, titles);
        this.context = c;
        this.images = images;
        this.mytitles = titles;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(R.layout.row_2, parent, false);
        ImageView myImage = (ImageView) row.findViewById(R.id.newImage);

        final TextView myTitle = (TextView) row.findViewById(R.id.newTaskTitle);
        myImage.setImageResource(images[position]);
        myTitle.setText(mytitles[position]);
        checkBox = (CheckBox) row.findViewById(R.id.checkBox);
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Boolean durum = checkBox.isChecked();
                if(durum=true){
                    Intent intent = new Intent (dinlenmeEkrani_newOptions.this, dinlenmeEkrani.class);
                    intent.putExtra("picture", byteArray);
                    intent.putExtra("RESULT_TEXT", myTitle.getText().toString());
                    startActivity(intent);

                }
                else{

                }

            }

        });
        return row;
    }

第二个活动可能有错误。当我试图找到解决方案时,我可能会犯一些错误。

我想说这些:我已经成功通过了textview。因此,当我删除一些与 Image 相关的代码时(尤其是在我的第一个活动中),我看到传递了来自 String.xml 的 textView。而且,我刚刚尝试过 R.Drawable.book,因为如果它有效,我认为我可以添加其他可绘制对象。

(如果你想知道 dbHelper,它就在这里)

public class Database extends SQLiteOpenHelper
public static final String DB_TABLE = "Task";
public static final String DB_COLUMN =  "TaskName";
public void insertNewTask (String task){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(DB_COLUMN,task);
        db.insertWithOnConflict(DB_TABLE,null,values, SQLiteDatabase.CONFLICT_REPLACE);
        db.close();
    }

【问题讨论】:

  • 您是否尝试过将图像作为字符串而不是字节数组发送?

标签: java android listview mobile


【解决方案1】:

不可能在活动之间共享任何视图对象,因为它们与活动链接并且活动是完全独立的。您需要创建一个逻辑来识别和处理仅在活动之间传递小块数据的状态,就像您已经在使用意图一样。

【讨论】:

    猜你喜欢
    • 2016-08-09
    • 2018-11-15
    • 1970-01-01
    • 2012-04-18
    • 1970-01-01
    • 2020-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多