如何解决我的应用程序中的图像适配器错误?

发布于 2024-12-29 09:39:17 字数 5814 浏览 0 评论 0原文

您好,我正在尝试将数组列表值放入我的图像适配器类中,但现在出现错误。我正在尝试解决,但我不知道。

错误行:

Bitmap bitmap = BitmapFactory.decodeFile(path_List.get(position));

错误:Bundle 类型中的方法 get(String) 不适用于参数 (int)

我的代码: parxmlActivity.java 类,

public class ParxmlActivity extends Activity {

    private String array_spinner[];
    private Cursor cursor;
    private int columnIndex;

    ParsedExampleDataSet parsedExampleDataSet= null;

    ArrayList<String> hltag_List=new ArrayList<String>();
    ArrayList<Subchild>sltag_List=new ArrayList<Subchild>();

    ArrayList<String> name_List =new ArrayList<String>();
    ArrayList<String> path_List =new ArrayList<String>();
    ArrayList<String> mmimage_List = new ArrayList<String>();
    ArrayList<String> mname_List = new ArrayList<String>();
    ArrayList<String> mmimage = new ArrayList<String>();
    ArrayList<String> mvideo_List = new ArrayList<String>();
    ArrayList<schild> model_List =new ArrayList<schild>();
    ArrayList<String> mspec_List = new ArrayList<String>();

    Spinner spinner_hltag,spinner_sltag, spinner_path;

    ArrayAdapter<String> adapter_hltag;
    ArrayAdapter<String> adapter_sltag;
    ArrayAdapter<String> adapter_path;

    int hltagPos=0;
    int sltagPos=0;


    Object sdcard;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        GridView sdcardImages = (GridView) findViewById(R.id.sdcard);
        spinner_hltag= (Spinner) findViewById(R.id.spinner1);
        spinner_sltag = (Spinner) findViewById(R.id.spinner2);

        sdcardImages.setOnItemClickListener(new OnItemClickListener() {


            public void onItemClick(AdapterView parent, View v, int position, long id) {
                Intent intent = new Intent(ParxmlActivity.this, tabview.class);
                //intent.putStringArrayListExtra("numbers", mspec_List);
                intent.putExtra("name",model_List.get(position).name);
                //intent.putExtra("name",model_List.get(position).name);


                startActivity(intent);  

            }
        });

        sdcardImages.setAdapter(new ImageAdapter(this));

        String[] proj = {MediaStore.Images.Thumbnails._ID};

        cursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                proj, null, null, null);


        columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);



        try {
            /* Get a SAXParser from the SAXPArserFactory. */
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();

            /* Get the XMLReader of the SAXParser we created. */
            XMLReader xr = sp.getXMLReader();

            /* Create a new ContentHandler and apply it to the XML-Reader */
            ExampleHandler myExampleHandler = new ExampleHandler();
            xr.setContentHandler(myExampleHandler);

            /* Parse the xml-data from our file. */
            xr.parse(new InputSource(getAssets().open("mafile.xml")));
            /* Parsing has finished. */

            parsedExampleDataSet =myExampleHandler. myParsedExampleDataSet;

            for (int i = 0; i < parsedExampleDataSet.gethltag().size(); i++) {
                Log.v("gethltag SIZE ", ""+parsedExampleDataSet.gethltag().size());
                hltag_List.add(parsedExampleDataSet.getHLTag().get(i));

            }

            sltag_List = parsedExampleDataSet.getSLTag(hltag_List.get(hltagPos));

            for(int i = 0; i < sltag_List.size(); i++) {
                name_List.add(sltag_List.get(i).name);
                path_List.add(sltag_List.get(i).path);

            }
            model_List.clear();     
            model_List = sltag_List.get(sltagPos).model;

            for(int j = 0; j<model_List.size(); j++){
                mmimage_List.addAll(model_List.get(j).imageList);
                mname_List.add(model_List.get(j).name);
                mvideo_List.addAll(model_List.get(j).videoList);
                mmimage.add(model_List.get(j).mimage);
                mspec_List.add(model_List.get(j).spec);

            }




            spinner_hltag.setAdapter(new MyAdapter(ParxmlActivity.this, R.layout.row, hltag_List)); 

            spinner_sltag.setAdapter(new MyAdapter1(ParxmlActivity.this, R.layout.row1,name_List)); 


        } catch (Exception e) { 

        }


    }


    }

这是我的 ImageAdapter.class 我在下面的代码中遇到错误:

public class ImageAdapter extends BaseAdapter {

    Bundle path_List;
    private Context mContext;

    public ImageAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return path_List.size();
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }

        //Bitmap bitmap = BitmapFactory.decodeFile(path_List);
        Bitmap bitmap = BitmapFactory.decodeFile(path_List.get(position));
        imageView.setImageBitmap(bitmap);
        return imageView;

    }



}

hi I am trying to get my arraylist value into my image adapter class but i got error now. i am trying to solve but i don't know.

error line:

Bitmap bitmap = BitmapFactory.decodeFile(path_List.get(position));

error : The method get(String) in the type Bundle is not applicable for the arguments (int)

my code:
parxmlActivity.java class

public class ParxmlActivity extends Activity {

    private String array_spinner[];
    private Cursor cursor;
    private int columnIndex;

    ParsedExampleDataSet parsedExampleDataSet= null;

    ArrayList<String> hltag_List=new ArrayList<String>();
    ArrayList<Subchild>sltag_List=new ArrayList<Subchild>();

    ArrayList<String> name_List =new ArrayList<String>();
    ArrayList<String> path_List =new ArrayList<String>();
    ArrayList<String> mmimage_List = new ArrayList<String>();
    ArrayList<String> mname_List = new ArrayList<String>();
    ArrayList<String> mmimage = new ArrayList<String>();
    ArrayList<String> mvideo_List = new ArrayList<String>();
    ArrayList<schild> model_List =new ArrayList<schild>();
    ArrayList<String> mspec_List = new ArrayList<String>();

    Spinner spinner_hltag,spinner_sltag, spinner_path;

    ArrayAdapter<String> adapter_hltag;
    ArrayAdapter<String> adapter_sltag;
    ArrayAdapter<String> adapter_path;

    int hltagPos=0;
    int sltagPos=0;


    Object sdcard;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        GridView sdcardImages = (GridView) findViewById(R.id.sdcard);
        spinner_hltag= (Spinner) findViewById(R.id.spinner1);
        spinner_sltag = (Spinner) findViewById(R.id.spinner2);

        sdcardImages.setOnItemClickListener(new OnItemClickListener() {


            public void onItemClick(AdapterView parent, View v, int position, long id) {
                Intent intent = new Intent(ParxmlActivity.this, tabview.class);
                //intent.putStringArrayListExtra("numbers", mspec_List);
                intent.putExtra("name",model_List.get(position).name);
                //intent.putExtra("name",model_List.get(position).name);


                startActivity(intent);  

            }
        });

        sdcardImages.setAdapter(new ImageAdapter(this));

        String[] proj = {MediaStore.Images.Thumbnails._ID};

        cursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                proj, null, null, null);


        columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);



        try {
            /* Get a SAXParser from the SAXPArserFactory. */
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();

            /* Get the XMLReader of the SAXParser we created. */
            XMLReader xr = sp.getXMLReader();

            /* Create a new ContentHandler and apply it to the XML-Reader */
            ExampleHandler myExampleHandler = new ExampleHandler();
            xr.setContentHandler(myExampleHandler);

            /* Parse the xml-data from our file. */
            xr.parse(new InputSource(getAssets().open("mafile.xml")));
            /* Parsing has finished. */

            parsedExampleDataSet =myExampleHandler. myParsedExampleDataSet;

            for (int i = 0; i < parsedExampleDataSet.gethltag().size(); i++) {
                Log.v("gethltag SIZE ", ""+parsedExampleDataSet.gethltag().size());
                hltag_List.add(parsedExampleDataSet.getHLTag().get(i));

            }

            sltag_List = parsedExampleDataSet.getSLTag(hltag_List.get(hltagPos));

            for(int i = 0; i < sltag_List.size(); i++) {
                name_List.add(sltag_List.get(i).name);
                path_List.add(sltag_List.get(i).path);

            }
            model_List.clear();     
            model_List = sltag_List.get(sltagPos).model;

            for(int j = 0; j<model_List.size(); j++){
                mmimage_List.addAll(model_List.get(j).imageList);
                mname_List.add(model_List.get(j).name);
                mvideo_List.addAll(model_List.get(j).videoList);
                mmimage.add(model_List.get(j).mimage);
                mspec_List.add(model_List.get(j).spec);

            }




            spinner_hltag.setAdapter(new MyAdapter(ParxmlActivity.this, R.layout.row, hltag_List)); 

            spinner_sltag.setAdapter(new MyAdapter1(ParxmlActivity.this, R.layout.row1,name_List)); 


        } catch (Exception e) { 

        }


    }


    }

this is my ImageAdapter.class i got error in below code:

public class ImageAdapter extends BaseAdapter {

    Bundle path_List;
    private Context mContext;

    public ImageAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return path_List.size();
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }

        //Bitmap bitmap = BitmapFactory.decodeFile(path_List);
        Bitmap bitmap = BitmapFactory.decodeFile(path_List.get(position));
        imageView.setImageBitmap(bitmap);
        return imageView;

    }



}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜点 2025-01-05 09:39:17

您正在尝试从 Bundle path_List 而不是 ArrayList获取字符串值 path_List 这就是为什么您收到此错误

public class ImageAdapter extends BaseAdapter 中删除此行 Bundle path_List;

并引用ArrayList<字符串> path_List 不要 Bundle path_List

并尝试再次运行..

更新: 另外我假设您的 ImageAdapter 类位于 ParxmlActivity 中..

代码: 另外,如果可能的话,请在您的 ImageAdapter 类中进行更改..类似,

    public class ImageAdapter extends BaseAdapter
{
    ArrayList<String> path_List =new ArrayList<String>();
    Activity activity;

public ImageAdapter(Activity activity,ArrayList<String> path_List)
    {
        this.activity = activity;
        this.path_List = path_List;
    }
 ....

并且在您的活动中这一行应该是,

 sdcardImages.setAdapter(new ImageAdapter(this,path_List));

You are trying to get string values from Bundle path_List instead of ArrayList<String> path_List that's why you got this error..

Remove this line Bundle path_List; from public class ImageAdapter extends BaseAdapter

Make a reference to ArrayList<String> path_List not to Bundle path_List

And try to run again..

UPDATE: Also I am assuming your ImageAdapter class in ParxmlActivity..

Code: Also if possible make changes in your ImageAdapter class.. something like,

    public class ImageAdapter extends BaseAdapter
{
    ArrayList<String> path_List =new ArrayList<String>();
    Activity activity;

public ImageAdapter(Activity activity,ArrayList<String> path_List)
    {
        this.activity = activity;
        this.path_List = path_List;
    }
 ....

And in your activity this line should be,

 sdcardImages.setAdapter(new ImageAdapter(this,path_List));
想你的星星会说话 2025-01-05 09:39:17

public Object get (String key) 不会将 int 作为参数。

因此将 path_List.get(position) 替换为 path_List.get(String.valueOf(position)) 。

我想补充的一件事是bundle在这里为空,所以首先在其中插入路径。
可能是您忘记将其从活动传递到适配器。

更新 :
位图 bitmap = BitmapFactory.decodeFile((String)path_List.getString.valueOf(position));

public Object get (String key) will not take int as argument .

so replace path_List.get(position) by path_List.get(String.valueOf(position)) .

one more thing i want to add is bundle is null here , so first insert path inside it .
might be you forgot to pass it from activity to adapter .

Update :
Bitmap bitmap = BitmapFactory.decodeFile((String)path_List.getString.valueOf(position));

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文