即使列表不为空,ActionBarActivity中的ListView中的EmptyView也会出现在页面上

时间:2014-07-19 09:18:55

标签: android listview android-arrayadapter custom-adapter

ListView中的EmptyView出现在页面上,即使列表不为空。 (我没有使用ListActivity)

即使我删除了setEmptyView(...),它也没有变化! 我该怎么办?

以下是我的List Fragment的代码:

public class SearchResultListFragment extends Fragment{
    Pagination pagination;
    boolean loadingMore = false;
    ListView list;
    TextView text1;
    TextView text2;
    TextView text3;
    TextView text4;
    TextView text5;
    Button Btngetdata;
    private static String url = "https://www.....com/api/property/search";
    private static int currentFirstVisibleItem;
    public SearchResultArrayListAdaptor adapter ;
    LinearLayout linlaHeaderProgress;
    JSONArray jsonArray = null;
    JSONParse fetchclass = null;

    public SearchResultListFragment() {

    }
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        linlaHeaderProgress = (LinearLayout) getActivity().findViewById(R.id.linlaHeaderProgress);
        ArrayAdapter<PropertyObject> aa =(ArrayAdapter<PropertyObject>) list.getAdapter();
        if (aa!= null){
            aa.clear();
            aa.notifyDataSetChanged();
        }
        list.setOnItemClickListener((OnItemClickListener) getActivity());

        adapter = new SearchResultArrayListAdaptor(getActivity(), R.layout.list_view_items, new ArrayList<PropertyObject>());
        list.setAdapter(adapter);
        pagination = new Pagination(0,15);
        fetchclass = new JSONParse(getActivity());
        fetchclass.execute(url);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_search_result_list, container, false);
        list=(ListView)rootView.findViewById(R.id.listViewSearchResult);
        list.setOnScrollListener(
            new OnScrollListener(){
                private int currentVisibleItemCount;
                private int currentTotalItemCount;
                private int currentScrollState;
                @Override
                public void onScrollStateChanged(AbsListView view, int scrollState) {
                    this.currentScrollState = scrollState;
                    this.isScrollCompleted();
                }
                private void isScrollCompleted() {
                    if (currentFirstVisibleItem + currentVisibleItemCount >= currentTotalItemCount) {
                        if (this.currentVisibleItemCount > 0 && this.currentScrollState == SCROLL_STATE_IDLE) {
                            if(fetchclass!=null) {
                                pagination = new Pagination(this.currentTotalItemCount,15);
                                if(!(fetchclass.getStatus()== AsyncTask.Status.RUNNING)) {
                                    fetchclass= new JSONParse(getActivity());
                                    fetchclass.execute(url);
                                }
                            }
                            else {
                                fetchclass = new JSONParse(getActivity());
                                fetchclass.execute(url);
                            }
                        }
                    }
                }
                @Override
                public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                    currentFirstVisibleItem = firstVisibleItem;
                    this.currentVisibleItemCount = visibleItemCount;
                    this.currentTotalItemCount = totalItemCount;
                }
        });
        return rootView;
    }
    //*********************************** inner class
    public class JSONParse extends AsyncTask<String, String, JSONObject> {
        Context mContext;
        int checkBoxRooms;
        public JSONParse(Context context){
            mContext = context;
        }
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            linlaHeaderProgress.setVisibility(View.VISIBLE);
        }
        @Override
        protected JSONObject doInBackground(String... args) {
            JSONObject json = null;
            PropertyFilter searchFilter = SearchFilterManager.initializePropertyFilter(new PropertyFilter(), getArguments());
            getActivity().setProgressBarIndeterminateVisibility(true);
            JSONParserForSearch jParser = new JSONParserForSearch();
            json = jParser.getJSONFromUrl(url, searchFilter, pagination);
            return json;
        }
        @Override
        protected void onPostExecute(JSONObject json) {
            // SHOW THE SPINNER WHILE LOADING FEEDS
            getActivity().setProgressBarIndeterminateVisibility(false);
            PropertyObject propertyObject;
            try {
                jsonArray = json.getJSONArray("PropertyListings");
                if (jsonArray == null || jsonArray.length()<1){

//                  list.setEmptyView(getActivity().findViewById(R.id.txtNoResult));
                }
                else {
                    for(int i = 0; i < jsonArray.length(); i++){
                        JSONObject c = jsonArray.getJSONObject(i);
                        propertyObject = new Gson().fromJson(c.toString(), new PropertyObject().getClass());
                        adapter.add(propertyObject);
                        adapter.notifyDataSetChanged();
                    }

                }
                // CHANGE THE LOADINGMORE STATUS TO PERMIT FETCHING MORE DATA
                loadingMore = false;

                // HIDE THE SPINNER AFTER LOADING FEEDS
                linlaHeaderProgress.setVisibility(View.GONE);
            } 
            catch (JSONException e) {
              e.printStackTrace();
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

我的布局XML:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" 
android:orientation="vertical"
tools:context="com.khoonat.khoonat2.SearchResultListActivity$SearchResultListFragment">

    <ListView
    android:id="@+id/listViewSearchResult"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_weight="2"
    >
    </ListView>

    <LinearLayout
    android:id="@+id/linlaHeaderProgress"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical"
    android:visibility="gone" 
    >

我的适配器是ArrayAdapter。 我也实现了isEmpty和getCount。

这是我的适配器:

public class SearchResultArrayListAdaptor extends ArrayAdapter<PropertyObject>{
Context context; 
    int layoutResourceId; // This is the layout you created for the list items   
    ArrayList<PropertyObject> items;
    public SearchResultArrayListAdaptor(Context context, int layoutResourceId, ArrayList<PropertyObject> items) {
        super(context, layoutResourceId, items);
        this.items = items;
        this.layoutResourceId = layoutResourceId;
        this.context = context;
    }
public View getView(int position, View convertView, ViewGroup parent) {  

View row = convertView;
    LayoutInflater inflater = ((Activity) getContext()).getLayoutInflater();    
            row = inflater.inflate(layoutResourceId, parent, false);    
 TextView txtLarge1 = 

(TextView)row.findViewById(R.id.listViewSearchResultTxtLarge1);
            TextView txtLarge2 = 

    (TextView)row.findViewById(R.id.listViewSearchResultTxtLarge2);

         @Override
    public int getCount() { 
        if (items == null)
            return 0;
        else 
            return items.size(); 
        }
    @Override
    public boolean isEmpty(){
        if (items == null || items.size()==0)
            return true;
        return false;
    }

我的列表活动:

public class SearchResultListActivity extends ActionBarActivity implements OnItemClickListener{
static PropertyObject selectedPropertyObject;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent intent = getIntent();
    Bundle bundle=new Bundle();
    bundle.putInt("intentionOfOwner", intent.getIntExtra("intentionOfOwner",0));

    SearchResultListFragment fragobj=new SearchResultListFragment();
    fragobj.setArguments(bundle);
    ///back button

    setContentView(R.layout.activity_search_result_list);
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction().add(R.id.container, fragobj).commit();
    }

}

@Override
public void onItemClick(AdapterView<?> arg0, View textView, int rowNumber, long arg3) {
    Intent intent = new Intent(this, PropertyDetailActivity.class);
    PropertyObject po = (PropertyObject) arg0.getAdapter().getItem((int)arg3);
    intent.putExtra("listingID",po.getID());
    startActivity(intent);
}

任何猜测? 非常感谢。

1 个答案:

答案 0 :(得分:0)

我认为问题出在这里,如果您正在进行惰性列表加载

,则不适用
for(int i = 0; i < jsonArray.length(); i++){
                    JSONObject c = jsonArray.getJSONObject(i);
                    propertyObject = new Gson().fromJson(c.toString(), new PropertyObject().getClass());
                    adapter.add(propertyObject);
                    adapter.notifyDataSetChanged();
                }

尝试

ArrayList<PropertyObject> items = new ArrayList<PropertyObject>();
for (int i = 0; i < jsonArray.length(); i++) {
    JSONObject c = jsonArray.getJSONObject(i);
    propertyObject = new Gson().fromJson(c.toString(), new PropertyObject().getClass());
    items.add(propertyObject);
}
adapter.items = items;
adapter.notifyDataSetChanged();