0

I am developing and android app that retrieves news from mysql database and populate them in list view, i managed to retrieve data successfully but am facing one major problem that stops me from proceeding with my project. When i try to populate data in listview the app crashes in displaying the following error in log cat saying

your content must have a ListView whose id attribute is 'android.R.id.list'

I have tried to change my xml listview id and write it in different forms but it still fails and displaying same error. here are my xml codes.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ListView
    android:id="@+id/android.R.id.list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#fff" />

<LinearLayout
    android:id="@+id/read_news"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="2"
    android:baselineAligned="false" />


</LinearLayout>

here are my java codes

package com.jetas.sportsapp;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.ListActivity;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.jetas.sportsapp.utils.JsonUtils;

public class News extends ListActivity {
//   private ProgressDialog pDialog;
private ArrayList<HashMap<String,String>> mCommentList;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.news_single_post);
    StrictMode.enableDefaults();
    ArrayList<HashMap<String,String>> newsList = getData();
    updateList(newsList);
}

public ArrayList<HashMap<String,String>> getData() {
    TextView textview = (TextView) findViewById(R.id.newsTitle);
    try {

//          String s = "";
        String news =     
    JsonUtils.read("http://10.0.2.2/index.php/android/news?offset=0&limit=10");
        Log.d("News data",news);
        JSONArray jArray = new JSONArray(news);

        Log.d("Parsing json", jArray.toString());

        for (int i = 0; i < jArray.length(); i++) {
            JSONObject json = jArray.getJSONObject(i);

            String title = json.getString("title");
            String content = json.getString("content");

            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();

            map.put("title", title);
            map.put("content", content);

            // adding HashList to ArrayList
            mCommentList.add(map);

//              textview.setText(title);
//              s = s + "Title: " + json.getString("title") + "\n"
//                      + "Content: " + json.getString("content") +     
"\n\n";
//              textview.setText(s);

        }
    } catch (Exception e) {

        Log.e("log_tag", "Error Parsing Data" + e.toString());
        textview.setText("Error in Parsing Data !!!!!");
    }
        return mCommentList;
}

/**
     * Inserts the parsed data into the listview.
     */
    private void updateList(ArrayList<HashMap<String,String>> newsLists) {
        // For a ListActivity we need to set the List Adapter, and in order   
                    //  to do
        //that, we need to create a ListAdapter.  This SimpleAdapter,
        //will utilize our updated Hashmapped ArrayList, 
        //use our single_post xml template for each item in our list,
        //and place the appropriate info from the list to the
        //correct GUI id.  Order is important here.
        ListAdapter adapter = new SimpleAdapter(this, newsLists,
                R.layout.news_single_post, new String[] { "title", 
                    "content"}, new int[] { R.id.newsTitle, R.id.content,});

        // I shouldn't have to comment on this one:
        setListAdapter(adapter);

        // Optional: when the user clicks a list item we 
        //could do something.  However, we will choose
        //to do nothing...
        ListView lv = getListView();    
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                // This method is triggered if an item is click 
                                    // within our
                // list. For our example we won't be using this, but
                // it is useful to know in real life applications.

            }
        });

    }   

//      public class LoadComments extends AsyncTask<Void, Void, Boolean> {
//
//          @Override
//          protected void onPreExecute() {
//              super.onPreExecute();
//              pDialog = new ProgressDialog(News.this);
//              pDialog.setMessage("Loading Comments...");
//              pDialog.setIndeterminate(false);
//              pDialog.setCancelable(true);
//              pDialog.show();
//          }
//          @Override
//          protected Boolean doInBackground(Void... arg0) {
//              //we will develop this method in version 2
//              getData();
//              return null;
//
//          }
//
//
//          @Override
//          protected void onPostExecute(Boolean result) {
//              super.onPostExecute(result);
//              pDialog.dismiss();
//            //we will develop this method in version 2
//              updateList();
//          }
    }

I also want to set such that when user clicks on the news the new activity opens with the details of the news.

1
  • Listview id xml should hava ===> android:id="@android:id/list"
    – Srikanth
    Apr 1, 2014 at 11:12

2 Answers 2

2

Try this..

Change this android:id="@+id/android.R.id.list" to android:id="@android:id/list" like below

<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#fff" />
0

I have faced same problem and solved by following

Listview lv;
lv = (listview) Findviewbyid(android.R.id.listviewname);
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.