Android自定义Gridview适配器错误排列(如果滚动)的更改

时间:2018-10-03 12:16:08

标签: android android-studio gridview custom-adapter

我有一个具有自定义gridview适配器的应用程序,该适配器可对图像和字符串进行膨胀,使用包含来自ExternalFilesStorage的图像文件路径的字符串数组对图像进行膨胀。

问题是,每次我上下滚动gridview时,内容都会随机重新排列,但是当我单击它时,onclick的文本结果与单击的项目不同,文本结果在正确排列的情况下是相同的。

以下是自定义适配器的代码:

 public class PTSCustomGridView extends BaseAdapter{

    private Context mContext;
    private final String[] pictureNameArray;
    private final String[] picturePathArray;

    public PTSCustomGridView(Context context, String[] pictureNameArray, String[] picturePathArray) {
        mContext = context;
        this.pictureNameArray = pictureNameArray;
        this.picturePathArray = picturePathArray;
    }

    @Override
    public int getCount() {
        return pictureNameArray.length;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup parent) {
        View gridViewAndroid;
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) {

            gridViewAndroid = new View(mContext);
            gridViewAndroid = inflater.inflate(R.layout.custom_gridview, null);
            TextView textViewAndroid = (TextView) gridViewAndroid.findViewById(R.id.android_gridview_text);
            ImageView imageViewAndroid = (ImageView) gridViewAndroid.findViewById(R.id.android_gridview_image);
            textViewAndroid.setText(pictureNameArray[i]);
            //set Image from file path
            File imgFile = new File(picturePathArray[i]);
            if(imgFile.exists()){
                Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
                //ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);
                imageViewAndroid.setImageBitmap(myBitmap);
            }
        } else {
            gridViewAndroid = (View) convertView;
        }

        return gridViewAndroid;
    }
}

以下是onClickListener的代码:

PTSCustomGridView adapterViewAndroid = new PTSCustomGridView(PictureToSpeechActivity.this, pictureNameArray, picturePathArray);
        androidGridView=(GridView)findViewById(R.id.PTSGridView);
        androidGridView.setAdapter(adapterViewAndroid);
        androidGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int i, long id) {
                //Toast.makeText(PictureToSpeechActivity.this, "GridView Item: " + gridViewString[+i], Toast.LENGTH_SHORT).show();
                if(result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED){
                    Toast.makeText(PictureToSpeechActivity.this, "Feature not supported on your device", Toast.LENGTH_LONG).show();
                }
                else{
                    pitch = sharedPref.getFloat("pitch", 1);
                    speed = sharedPref.getFloat("speed", 1);
                    language = sharedPref.getString("language", "UK");
                    text = pictureNameArray[i];
                    ettext.setText(text);
                    TTS.setPitch(pitch);
                    TTS.setSpeechRate(speed);
                    ttsLanguage();
                    TTS.speak(text,TextToSpeech.QUEUE_FLUSH, null,null);
                }
            }
        });

0 个答案:

没有答案