可展开的Listview未显示

时间:2014-11-16 20:34:43

标签: android android-layout listview expandablelistview expandable

我只想在可扩展列表视图中的文件中显示Json信息。 当我在运行应用程序时,没有任何内容显示在''''''''''''''屏幕。我有以下代码:

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

}

以下是我的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;
}

}

不同的XML文件就是这样的: 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它是下一个:

   <?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>

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>

问题在于,当我运行应用程序时,它只是显示一个空屏幕。那么我做错了什么? 我必须改变一些东西吗???

谢谢;)

1 个答案:

答案 0 :(得分:0)

你的布局应该是:

<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>

和您的活动:

@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();

}