Expandable Listview not showing

1.5k views Asked by At

I'm just trying to show a Json information in a file in a expandable listview. When I'm running the app nothing it's showed in the ''noticias'' screen. I have the following code :

           public class noticias extends ActionBarActivity {


public ExpandListAdapter ExpAdapter;
public ArrayList<Child> ListChild=null;
public ArrayList<Noticia> ListNoticia=null;
public ExpandableListView ExpandList;
public ProgressDialog dialog;
public ArrayList<ArrayList<Child>> ListChildXXXXXXXXX = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_noticias);

    ListNoticia = new ArrayList<Noticia>();
    ListChild = new ArrayList<Child>();
    ListChildXXXXXXXXX = new ArrayList<ArrayList<Child>>();
    new NoticiasAsyncTask().execute();

}


public class NoticiasAsyncTask extends AsyncTask<Void, Void, String> {

    protected void onPreExecute() {
        super.onPreExecute();
        dialog = ProgressDialog.show(noticias.this, "",
                "melakukan pengambilan data...");
    }

    @Override
    protected String doInBackground(Void... params) {
        String result = "";
        try {
            result = loadJSONFromFile();
        } catch (Exception e) {
            result = "";
            Log.d("test", e.getMessage());
        }
        return result;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        fetchResponse(result.replace("\n", "").trim());
        dialog.dismiss();
    }
}
    private void fetchResponse(String result) {
        if (!result.equals("")) {
            try {

        ArrayList<Noticia> list = new ArrayList<Noticia>();
        ArrayList<Child> ch_list;

        int size = 4;
        int j = 0;

            JSONArray jsonArray = new JSONArray();

                JSONObject jsonObjecto = new JSONObject(loadJSONFromFile());


                Iterator x = jsonObjecto.keys();

                while (x.hasNext()){
                    String key = (String) x.next();
                    jsonArray.put(jsonObjecto.get(key));
                }

            Noticia not = null;
            Child ch = null;

            for (int i = 0; i < jsonArray.length(); i++) {
                ListChild = new ArrayList<Child>();
                JSONObject jsonObject = jsonArray.getJSONObject(i);

                not = new Noticia(jsonObject.optString("titulo"));
                ListNoticia.add(not);

                ch = new Child(jsonObject.optString("mensaje"));
                ListChild.add(ch);


                ListChildXXXXXXXXX.add(ListChild);
                ExpandableListView ExpandList = (ExpandableListView) findViewById(expandableListView);
                ExpAdapter = new ExpandListAdapter(this, ListNoticia,ListChildXXXXXXXXX);
                ExpandList.setAdapter(ExpAdapter);

            }


        } catch (JSONException e) {
            e.printStackTrace();
        }



    }}


    public String loadJSONFromFile() {

        String json = null;
        try {

            FileInputStream is = openFileInput("noticias.json");
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            json = new String(buffer, "UTF-8");

        } catch (IOException ex) {
            ex.printStackTrace();
            return null;
        }
        return json;

    }


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_noticias, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

Here is the code from my ExpandListAdapter:

           public class ExpandListAdapter extends BaseExpandableListAdapter {
public Context context;
public ArrayList<Noticia> noticiasArrayList;
public ArrayList<ArrayList<Child>> ListChild;

public ExpandListAdapter(Context context, ArrayList<Noticia> noticiasArrayList,ArrayList<ArrayList<Child>> ListChild) {
    this.context = context;
    this.noticiasArrayList = noticiasArrayList;
    this.ListChild=ListChild;
}
@Override
public Child getChild(int groupPosition, int childPosition) {
   return ListChild.get(groupPosition).get(childPosition);
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}

@Override
public View getChildView(int groupPosition, int childPosition,
                         boolean isLastChild, View convertView, ViewGroup parent) {

    Child child = getChild(groupPosition, childPosition);
    ViewHolder holder=null;
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) context
                .getSystemService(context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.child_row, null);
        holder=new ViewHolder();
        holder.mensaje=(TextView)convertView.findViewById(R.id.mensaje);
        convertView.setTag(holder);
    }
    else {
        holder=(ViewHolder)convertView.getTag();
    }


    holder.mensaje.setText(child.getMensaje());


    return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
   return ListChild.get(groupPosition).size();
}

@Override
public Noticia getGroup(int groupPosition) {
    return noticiasArrayList.get(groupPosition);
}

@Override
public int getGroupCount() {
    return noticiasArrayList.size();
}

@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
                         View convertView, ViewGroup parent) {
    Noticia not = (Noticia) getGroup(groupPosition);
    ViewHolder holder= null;

    if (convertView == null) {
        LayoutInflater inf = (LayoutInflater) context
                .getSystemService(context.LAYOUT_INFLATER_SERVICE);
        convertView = inf.inflate(R.layout.list_row, null);

        holder=new ViewHolder();
        holder.titulo=(TextView)convertView.findViewById(R.id.titulo);
        holder.fecha=(TextView)convertView.findViewById(R.id.fecha);
        holder.dirigido=(TextView)convertView.findViewById(R.id.dirigido);
        convertView.setTag(holder);

    }
    else{
        holder=(ViewHolder)convertView.getTag();
    }

    holder.titulo.setText(not.getTitulo());
    holder.fecha.setText(not.getFecha());
    holder.dirigido.setText(not.getDirigido());


    return convertView;
}

@Override
public boolean hasStableIds() {
    return true;
}

@Override
public boolean areAllItemsEnabled()
{
    return true;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}

static class ViewHolder{
    TextView titulo, fecha,dirigido, mensaje, moreinfo;
}

}

The diferent XML files are just this : noticias.xml

     <RelativeLayout 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"
tools:context=".noticias"
    >

<ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:divider="@color/list_divider"
    android:dividerHeight="1dp"
     />

</RelativeLayout>

list_row.xml it's the next:

   <?xml version="1.0" encoding="utf-8"?>
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="8dp"
    android:orientation="vertical">

    <TextView
        android:id="@+id/titulo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="@dimen/titulo"
        android:textStyle="bold" />


    <TextView
        android:id="@+id/dirigido"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/titulo"
        android:layout_marginTop="1dip"
        android:textSize="@dimen/dirigido" />


    <TextView
        android:id="@+id/fecha"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/dirigido"
        android:layout_marginTop="5dp"
        android:textSize="@dimen/fecha" />


    <TextView
        android:text="+info"
        android:id="@+id/moreinfo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:textSize="@dimen/moreinfo"
        android:layout_gravity="right" />

 </LinearLayout>

And the code of child_row.xml:

<TextView
            android:id="@+id/mensaje"
            android:text="Hola"
            android:layout_height="wrap_content"
            android:layout_width="335dp"
            android:layout_gravity="left|center_vertical"
            android:textSize="15dip"
            android:layout_marginLeft="30dip"/>

    </RelativeLayout>

The thing is that when i'm running the app it just showing a empty screen. So what I'm doing wrong ?? Must I have to change something ???

Thanks ;)

1

There are 1 answers

5
Mohammad Rahchamani On

your layout should be:

<RelativeLayout 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"
tools:context=".noticias"
    >

<ExpandableListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:divider="@color/list_divider"
    android:dividerHeight="1dp"
     />

</RelativeLayout>

and your activity :

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_noticias);
    ExpandList=(ExpandableListView)findViewById(R.id.list);
    ListNoticia = new ArrayList<Noticia>();
    ListChild = new ArrayList<Child>();
    ListChildXXXXXXXXX = new ArrayList<ArrayList<Child>>();
    new NoticiasAsyncTask().execute();

}