Android的定制ArrayAdapter不采取数组 [英] Android Custom ArrayAdapter does not take an array

查看:159
本文介绍了Android的定制ArrayAdapter不采取数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正确填写一个数组? NewsData_data不能被解析为一个变量。

I correctly fill an array? NewsData_data cannot be resolved to a variable.

    NewsData NewsData_data[] = new NewsData[]
                {
                    new NewsData(header[i], short_text[i], team[i], datatime[i], photo_url[i])
                };

问题的:

     NewsDataAdapter adapter = new NewsDataAdapter(this, 
                        R.layout.news_details, NewsData_data);

NewsData_data不能被解析为一个变量。如何解决这个问题?

NewsData_data cannot be resolved to a variable. How to fix this error?

    public void ListDrwaer() { 
            String[] header;
            String[] short_text;
            String[] team;
            String[] datatime;
            String[] photo_url;
      try {
       JSONObject jsonResponse = new JSONObject(jsonResult);
       JSONArray jsonMainNode = jsonResponse.optJSONArray("news");
       for (int i = 0; i < jsonMainNode.length(); i++) {
        JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
        header[i] = jsonChildNode.optString("header");
        short_text[i] = jsonChildNode.optString("short_text");
        team[i] = jsonChildNode.optString("team");
        datatime[i] = jsonChildNode.optString("datatime");
        photo_url[i] = jsonChildNode.optString("photo_url");    

        NewsData NewsData_data[] = new NewsData[]
                {
                    new NewsData(header[i], short_text[i], team[i], datatime[i], photo_url[i])
                };      
       }
      } catch (JSONException e) {
       Toast.makeText(getActivity(), "Error" + e.toString(),
         Toast.LENGTH_SHORT).show();
      }

NewsDataAdapter adapter = new NewsDataAdapter(this, 
                        R.layout.news_details, NewsData_data);
      View header1 = getActivity().getLayoutInflater().inflate(R.layout.news_details, null);
                listView.addHeaderView(header1);                    
                listView.setAdapter(adapter);
     }

     public class NewsData {
            public String header;
            public String short_text;
            public String team;
            public String datatime;
            public String photo_url;
            public NewsData(){
                super();
            }

            public NewsData(String header,
                        String short_text,
                        String team,
                        String datatime,
                        String photo_url) {
                super();
                this.header = header;
                this.short_text = short_text;
                this.team = team;
                this.datatime = datatime;
                this.photo_url = photo_url;
            }
        }

     public class NewsDataAdapter extends ArrayAdapter<NewsData>{

            Context context; 
            int layoutResourceId;    
            NewsData data[] = null;

            public NewsDataAdapter(Context context, int layoutResourceId, NewsData[] data) {
                super(context, layoutResourceId, data);
                this.layoutResourceId = layoutResourceId;
                this.context = context;
                this.data = data;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View row = convertView;
                NewsDataHolder holder = null;

                if(row == null)
                {
                    LayoutInflater inflater = ((Activity)context).getLayoutInflater();
                    row = inflater.inflate(layoutResourceId, parent, false);

                    holder = new NewsDataHolder();
                    holder.img_news = (ImageView)row.findViewById(R.id.img_news);
                    holder.header = (TextView)row.findViewById(R.id.header);
                    holder.short_text = (TextView)row.findViewById(R.id.short_text);
                    holder.team = (TextView)row.findViewById(R.id.team);
                    holder.datatime = (TextView)row.findViewById(R.id.datatime);

                    row.setTag(holder);
                }
                else
                {
                    holder = (NewsDataHolder)row.getTag();
                }       
                NewsData NewsData = data[position];                 
                Picasso.with(context).load(NewsData.photo_url).into(holder.img_news);
                holder.header.setText(NewsData.header);
                holder.short_text.setText(NewsData.short_text);
                holder.team.setText(NewsData.team);
                holder.datatime.setText(NewsData.datatime);                 
                return row;
            }

             class NewsDataHolder
            {
                ImageView img_news;
                TextView header;
                TextView short_text;
                TextView team;
                TextView datatime;
            }
        }

问题的:

     NewsDataAdapter adapter = new NewsDataAdapter(this, 
                        R.layout.news_details, NewsData_data);

NewsData_data不能被解析为一个变量

NewsData_data cannot be resolved to a variable

推荐答案

您需要声明try块以外的阵列,因此它是对 ArrayAdapter 构造可见

You need to declare your array outside of the try block so that it is visible to the ArrayAdapter constructor.

这篇关于Android的定制ArrayAdapter不采取数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆