1

我想做一个活动,通过按下按钮将一个项目添加到另一个活动的图像列表中。我正在使用一个基本适配器来制作包含图像和文本的列表。图像是从 sd 卡中选择的,文本是在编辑文本中输入的。

这是将从中添加新项目的活动:

public class Add extends Menu_Activity implements OnClickListener {

    ImageView img;
    Bitmap bmp;
    private static int REQ_IMAGE = 1;
    String title = null;
    String picturePath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add);
        ss = new Start_Screen();

        Button btnBrowse = (Button) findViewById(R.id.btnBrowse);
        Button btnSave = (Button) findViewById(R.id.btnSave);
        img = (ImageView) findViewById(R.id.imgSong);
        EditText etTitle = (EditText) findViewById(R.id.etTitle);
        title = etTitle.getText().toString();

        if (title != null) {
            btnSave.setVisibility(View.VISIBLE);
        }

        btnBrowse.setOnClickListener(this);
        btnSave.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.btnBrowse:
            Intent i = new Intent(
                    Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(i, REQ_IMAGE);
            break;
        case R.id.btnSave:

            break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQ_IMAGE && resultCode == RESULT_OK) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };

            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            picturePath = cursor.getString(columnIndex);
            cursor.close();
            bmp = BitmapFactory.decodeFile(picturePath);
            img.setImageBitmap(bmp);
        }
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        finish();
    }

单击 btnSave 时,我希望在此活动中出现一个新项目:

public class Start_Screen extends Menu_Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
setContentView(R.layout.start_screen);
ListView list = (ListView)findViewById(R.id.soundslist);

List<Bitmap> lbmp = new ArrayList<Bitmap>();
List<String> ltitles=new ArrayList<String>();

lbmp.add(BitmapFactory.decodeResource(getResources(), R.drawable.cobblestone));
lbmp.add(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));

ltitles.add("I'm Blue");
ltitles.add( "Eminem-Lose Yourself");


Bitmap[] bmp = lbmp.toArray(new Bitmap[lbmp.size()]);
String[] titles=ltitles.toArray(new String[ltitles.size()]);
Custom_Adapter adapter = new Custom_Adapter(this, bmp, titles);
list.setAdapter(adapter);
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    finish();
}
4

1 回答 1

0

您需要将图像路径作为额外的意图传递给您的 Start_Screen 并在那里以与在之前的活动中解码文件相同的方式对其进行解码。

于 2013-09-16T11:39:33.260 回答