1

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])
                };

Problem in:

     NewsDataAdapter adapter = new NewsDataAdapter(this, 
                        R.layout.news_details, 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;
            }
        }

Problem in:

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

NewsData_data cannot be resolved to a variable

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

As corsair922 mentioned, you'll want to declare your NewsData_data array outside your try catch block, otherwise you won't have access to it from outside the block. Additionally, you want to initialize your array once, and populate the array elements as you go as opposed to reinitializing your array each time:

Edit, I would also recommend you spend some time getting familiar with the Java coding conventions. They will make your code easier to maintain/tidier and will allow other people to better understand your code (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);
}

3 Comments

NewsDataAdapter adapter = new NewsDataAdapter(this, R.layout.news_details, NewsData_data); The constructor NewsFragment.NewsDataAdapter(NewsFragment, int, NewsFragment.NewsData[]) is undefined
Your Fragment class isn't a Context, you'll need to call getActivity() from your fragment to get the Context. So call NewsDataAdapter adapter = new NewsDataAdapter(getActivity(), R.layout.news_details, NewsData_data);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.