Custom ListView not showing any items

613 Views Asked by At

I am trying to display a custom listview but nothing appears. My activity:

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;

import com.example.elnoorgeh.ServerAPI;

import android.app.Fragment;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class BlogFragment extends Fragment {
    JSONArray jArray;
    TextView title;
    RelativeLayout layout;
    int previousID = 0;
    int currentID = 0;
    ArrayList<String> titles;
    ArrayList<String> contents;
    ListView list;

    public BlogFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_blog, container,
                false);
        new AsyncFetch().execute();
        return rootView;
    }

    private class AsyncFetch extends AsyncTask<Object, Object, Object> {

        @Override
        protected Object doInBackground(Object... params) {
            // TODO Auto-generated method stub
            jArray = new JSONArray();
            jArray = ServerAPI.getData();
            titles = new ArrayList<String>();
            contents = new ArrayList<String>();
            for (int i = 0; i < jArray.length(); i++) {
                String blogTitle = null;
                String content = null;
                try {
                    blogTitle = jArray.getJSONObject(i).getString("title");
                    content = jArray.getJSONObject(i).getString("content");
                    titles.add(blogTitle);
                    contents.add(content);

                } catch (JSONException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

                System.out.println(blogTitle);
                System.out.println(content);
            }
            // display(titles, contents);
            return null;
        }

        protected void onPostExecute(Object result) {

            layout = (RelativeLayout) getView().findViewById(R.id.blogPage);
            layout.removeAllViews();

            CustomList adapter = new CustomList(getActivity(), titles);
            list = new ListView(getActivity());
            System.out.println("list done");
            list.setAdapter(adapter);
            list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    Toast.makeText(getActivity(),
                            "You Clicked at " + titles.get(position),
                            Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

    public void display(ArrayList<String> t, ArrayList<String> c) {

    }
}

Custom ListView class:

import java.util.ArrayList;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomList extends ArrayAdapter<String> {
    private final Activity context;
    private final ArrayList<String> web;
//  private final Integer[] imageId;

    public CustomList(Activity context, ArrayList<String>web) {
        super(context, R.layout.fragment_listview);
        this.context = context;
        this.web = web;
//      this.imageId = imageId;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView = inflater.inflate(R.layout.fragment_listview, null, true);
        TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
        txtTitle.setText((CharSequence) web.get(position));
//      imageView.setImageResource(imageId[position]);
        return rowView;
    }
}

fragment_blog:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/blogPage"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >


    <ListView
       android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>

    <TextView
        android:id="@+id/txtLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:maxLines="20"
        android:singleLine="false"
        android:textSize="16dp" />


</RelativeLayout>

fragment_listview:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TableRow>

        <ImageView
            android:id="@+id/img"
            android:layout_width="50dp"
            android:layout_height="50dp" />

        <TextView
            android:id="@+id/txt"
            android:layout_width="wrap_content"
            android:layout_height="50dp" />
    </TableRow>

</TableLayout>

I can't find any errors or notice something irregular, so why is that happening?

1

There are 1 best solutions below

4
On

you should extend BaseAdapter and implement abstract methods

@Override
public int getCount() {
    return web.size();
}

@Override
public Object getItem(int position) {
    return web.get(position);
}

@Override
public long getItemId(int position) {
    return 0;
}

also you might change

txtTitle.setText((CharSequence) web.get(position));

to

txtTitle.setText((CharSequence) getItem(position));

now your adapter don't know size of web array

edit:

you can get one inflater in constructor and keep in class, no need to getting inflater each time (little bit better for perfomance)

  LayoutInflater inflater = context.getLayoutInflater();

edit 2:

localhost put proper comment - you are removing all Views from RelativeLayout, also ListView, and creating new ListView without adding to Relative. keeping reference will not auto-add View

protected void onPostExecute(Object result) {

        layout = (RelativeLayout) getView().findViewById(R.id.blogPage);
        layout.removeView(getView().findViewById(R.id.txtLabel);
        //assuming you need to remove only txtLabel

        CustomList adapter = new CustomList(getActivity(), titles);
        list.setAdapter(adapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                Toast.makeText(getActivity(),
                        "You Clicked at " + titles.get(position),
                        Toast.LENGTH_SHORT).show();
            }
        });
    }