【问题标题】:Android Custom ArrayAdapter does not take an arrayAndroid Custom ArrayAdapter 不带数组
【发布时间】:2014-01-03 01:24:54
【问题描述】:

我正确地填充了一个数组? NewsData_data 无法解析为变量。

    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 无法解析为变量。如何解决此错误?

    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 无法解析为变量

【问题讨论】:

    标签: android listview android-arrayadapter


    【解决方案1】:

    您需要在 try 块之外声明您的数组,以便 ArrayAdapter 构造函数可以看到它。

    【讨论】:

      【解决方案2】:

      正如 corsair922 所提到的,您需要在 try catch 块之外声明您的 NewsData_data 数组,否则您将无法从块外部访问它。此外,您希望初始化一次数组,并在执行过程中填充数组元素,而不是每次都重新初始化数组:

      编辑,我还建议您花一些时间熟悉 Java 编码约定。它们将使您的代码更易于维护/更整洁,并允许其他人更好地理解您的代码 (http://www.oracle.com/technetwork/java/codeconv-138413.html)。

      public void ListDrwaer() {
          String[] header;
          String[] short_text;
          String[] team;
          String[] datatime;
          String[] photo_url;
      
          NewsData NewsData_data[];
      
          try {
              JSONObject jsonResponse = new JSONObject(jsonResult);
              JSONArray jsonMainNode = jsonResponse.optJSONArray("news");
              NewsData_data = new NewsData[jsonMainNode.length()];
              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_data[i] = 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);
      }
      

      【讨论】:

      • NewsDataAdapter 适配器 = new NewsDataAdapter(this, R.layout.news_details, NewsData_data);构造函数 NewsFragment.NewsDataAdapter(NewsFragment, int, NewsFragment.NewsData[]) 未定义
      • 您的 Fragment 类不是 Context,您需要从 Fragment 调用 getActivity() 来获取 Context。所以调用 NewsDataAdapter adapter = new NewsDataAdapter(getActivity(), R.layout.news_details, NewsData_data);
      猜你喜欢
      • 1970-01-01
      • 2012-10-15
      • 2012-07-10
      • 1970-01-01
      • 2012-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多