java - 无法将搜索栏与文本关联起来

标签 java android android-layout android-activity

我正在尝试将我的搜索栏与文本联系起来。在帮助下,我尝试这样做,但我的尝试没有成功。

此外,我还想在下面的代码中包含一个范围搜索栏,但不确定如何解决。任何帮助将不胜感激。

public class ProfileCreation extends Activity {

    private static final int RESULT_LOAD_IMAGE = 1;
    FrameLayout layout;
    Button save;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile_creation);
        save = (Button) findViewById(R.id.button2);
        String picturePath = PreferenceManager.getDefaultSharedPreferences(this).getString("picturePath", "");
        if (!picturePath.equals("")) {
            ImageView imageView = (ImageView) findViewById(R.id.profilePicturePreview);
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
        }


        Button buttonLoadImage = (Button) findViewById(R.id.btnPictureSelect);
        buttonLoadImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent i = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(i, RESULT_LOAD_IMAGE);
            }
        });
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                // Locate the image in res > 
                Bitmap bitmap = BitmapFactory.decodeFile("picturePath");
                // Convert it to byte
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                // Compress image to lower quality scale 1 - 100
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);

                Object image = null;
                try {
                    String path = null;
                    image = readInFile(path);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                // Create the ParseFile
                ParseFile file = new ParseFile("picturePath", (byte[]) image);
                // Upload the image into Parse Cloud
                file.saveInBackground();

                // Create a New Class called "ImageUpload" in Parse
                ParseObject imgupload = new ParseObject("Image");

                // Create a column named "ImageName" and set the string
                imgupload.put("Image", "picturePath");


                // Create a column named "ImageFile" and insert the image
                imgupload.put("ImageFile", file);

                // Create the class and the columns
                imgupload.saveInBackground();

                // Show a simple toast message

                SeekBar seekBar = (SeekBar) findViewById(R.id.seekBarDistance);
                final TextView seekBarValue = (TextView) findViewById(R.id.seekBarDistanceValue);

                seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

                    @Override
                    public void onProgressChanged(SeekBar seekBar, int progress,
                                                  boolean fromUser) {
                        // TODO Auto-generated method stub 
                        seekBarValue.setText(String.valueOf(progress));
                    }

                    @Override
                    public void onStartTrackingTouch(SeekBar seekBar) {
                        // TODO Auto-generated method stub 
                    }

                    @Override
                    public void onStopTrackingTouch(SeekBar seekBar) {
                        // TODO Auto-generated method stub 
                    }
                });
            }
        });
    }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);

            if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
                    && null != data) {
                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]);
                String picturePath = cursor.getString(columnIndex);
                cursor.close();

                ImageView imageView = (ImageView) findViewById(R.id.profilePicturePreview);
                imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

            }

        }

        private byte[] readInFile(String path) throws IOException {
            // TODO Auto-generated method stub
            byte[] data = null;
            File file = new File(path);
            InputStream input_stream = new BufferedInputStream(new FileInputStream(
                    file));
            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
            data = new byte[16384]; // 16K
            int bytes_read;
            while ((bytes_read = input_stream.read(data, 0, data.length)) != -1) {
                buffer.write(data, 0, bytes_read);
            }
            input_stream.close();
            return buffer.toByteArray();

        }
    }

更新了代码

public class ProfileCreation extends Activity {

    private static final int RESULT_LOAD_IMAGE = 1;
    FrameLayout layout;
    Button save;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile_creation);
        save = (Button) findViewById(R.id.button2);
        String picturePath = PreferenceManager.getDefaultSharedPreferences(this).getString("picturePath", "");
        if (!picturePath.equals("")) {
            ImageView imageView = (ImageView) findViewById(R.id.profilePicturePreview);
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
        }

        SeekBar seekBar = (SeekBar) findViewById(R.id.seekBarDistance);
        final TextView seekBarValue = (TextView) findViewById(R.id.seekBarDistanceValue);

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                // TODO Auto-generated method stub 
                seekBarValue.setText(String.valueOf(progress));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub 
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub 
            }

        }


        Button buttonLoadImage = (Button) findViewById(R.id.btnPictureSelect);
        buttonLoadImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent i = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(i, RESULT_LOAD_IMAGE);
            }
        });
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                // Locate the image in res > 
                Bitmap bitmap = BitmapFactory.decodeFile("picturePath");
                // Convert it to byte
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                // Compress image to lower quality scale 1 - 100
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);

                Object image = null;
                try {
                    String path = null;
                    image = readInFile(path);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                // Create the ParseFile
                ParseFile file = new ParseFile("picturePath", (byte[]) image);
                // Upload the image into Parse Cloud
                file.saveInBackground();

                // Create a New Class called "ImageUpload" in Parse
                ParseObject imgupload = new ParseObject("Image");

                // Create a column named "ImageName" and set the string
                imgupload.put("Image", "picturePath");


                // Create a column named "ImageFile" and insert the image
                imgupload.put("ImageFile", file);

                // Create the class and the columns
                imgupload.saveInBackground();

                // Show a simple toast message


            }
        });
    }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);

            if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
                    && null != data) {
                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]);
                String picturePath = cursor.getString(columnIndex);
                cursor.close();

                ImageView imageView = (ImageView) findViewById(R.id.profilePicturePreview);
                imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

            }

        }

        private byte[] readInFile(String path) throws IOException {
            // TODO Auto-generated method stub
            byte[] data = null;
            File file = new File(path);
            InputStream input_stream = new BufferedInputStream(new FileInputStream(
                    file));
            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
            data = new byte[16384]; // 16K
            int bytes_read;
            while ((bytes_read = input_stream.read(data, 0, data.length)) != -1) {
                buffer.write(data, 0, bytes_read);
            }
            input_stream.close();
            return buffer.toByteArray();

        }
    }

最佳答案

以下代码 fragment 应移出onClickListener。写在onCreate

 SeekBar seekBar = (SeekBar) findViewById(R.id.seekBarDistance);
    final TextView seekBarValue = (TextView) findViewById(R.id.seekBarDistanceValue);

    seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromUser) {
            // TODO Auto-generated method stub 
            seekBarValue.setText(String.valueOf(progress));
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub 
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub 
        }
    });

关于java - 无法将搜索栏与文本关联起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25051497/

相关文章:

java - Spring Data MongoDB 日期之间

java - 我需要录制音频来调用 getMaxAmplitude() 吗?

php - 从 Android 应用程序检索数据到 PHP 网站

java - 在布局中正确设置对象

Android:根据所选设备选择不同的布局

java - Hashtable 是否实现了 Map 接口(interface)中的每个方法?

java - infinispan 服务器上的 JDBC_PING 不工作

java - 如何在 SimpleHistogramDataset 中设置 SimpleHistogramBin bin 的高度?

Android - 修改 XML 中的布局值

java - Android应用程序的多语言支持