检查ListView Android中的列表项

时间:2016-04-28 03:55:11

标签: android listview android-asynctask baseadapter

我已使用ListView成功将项目放入BaseAdapter,我想问的是......是否可以在ListView中检查onCreate()是否为空,或获取onCreate()中的总列表数?感谢。

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;


public class CustomAdapter extends BaseAdapter {
    Context context;
    ArrayList<Contact> list;
    private static LayoutInflater inflater;

    public CustomAdapter(Context context,ArrayList<Contact> list){
        this.context = context;
        this.list = list;
    }
    @Override
    public int getCount() {
        return list.size();
    }

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(inflater == null)
            inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if(convertView == null)
            convertView = inflater.inflate(R.layout.list_contact,null);
        TextView id = (TextView) convertView.findViewById(R.id.id_keranjang);
        TextView name = (TextView) convertView.findViewById(R.id.name);
        TextView address = (TextView) convertView.findViewById(R.id.contact);

        int contact_id = list.get(position).getId();
        String contact_name = list.get(position).getName();
        String contact_address = list.get(position).getAddress();

        id.setText(String.valueOf(contact_id));
        kode.setText(contact_name);
        judul.setText(contact_address);

        return convertView;
    }
}

MainActivity

public class MainActivity extends AppCompatActivity {
        private static final String URL = "http://domain/contact.php";
        CustomAdapter customAdapter;
        int success = 0;
        JSONParser jsonParser = new JSONParser();
        ArrayList<Contact> listContact = new ArrayList<Contact>();
        JSONObject json=null;
        JSONArray contact=null;ListView li;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            li = (ListView) findViewById(R.id.listView);

            new getContactAsync().execute();

            //int c = li.getAdapter().getCount(); not working

        }
    class getContactAsync extends AsyncTask<String,String,ArrayList<Contact>>{


            @Override
            protected ArrayList doInBackground(String... params) {
                    json = jsonParser.getFromURL(URL);
                try {
                    contact = json.getJSONArray("contact");
                    success = json.getInt(TAG_SUCCESS);
                    if (success == 1) {
                        for (int i = 0; i < contact.length(); i++) {
                            JSONObject c = contact.getJSONObject(i);
                            Contact a = new Contact();
                            a.setId(c.optInt(TAG_ID));
                            a.setName(c.optString(TAG_NAME));
                            a.setAddress(c.optString(TAG_ADDRESS));
                            listContact.add(a);
                        }
                    }
                } catch (JSONException e) {
                    Log.d("ERROR", e.toString());
                }
                return listContact;
            }

            @Override
            protected void onPostExecute(ArrayList<Contact> s) {
                super.onPostExecute(s);
                customAdapter = new CustomAdapter(MainActivity.this,listContact);
                li.setAdapter(customAdapter);
            }
        }

4 个答案:

答案 0 :(得分:0)

在您的活动中,您拥有listContact,请确定此大小,因为这是您传递给适配器的内容

listContact.size ()

答案 1 :(得分:0)

您只需使用自己的BaseAdapter方法getCount()

即可
int number_of_element_in_listview = customAdapter.getCount();

但是你应该在初始化变量customAdapter之后以及在为你定义的AsyncTask getContactAsync中为它分配值之后再这样做。

  

是否可以检查listview在oncreate中是否为空?

如果值number_of_element_in_listview为0,那么listview将没有元素。

答案 2 :(得分:0)

不是在onCreate中检查它,而是在创建适配器对象之前在onPostExecute中检查它。

@Override
protected void onPostExecute(ArrayList<Contact> s) {
    super.onPostExecute(s);
    if(listContact.size()>0){
       customAdapter = new CustomAdapter(MainActivity.this,listContact);
       li.setAdapter(customAdapter);
    }
}

仅当列表中包含项目时,才会创建适配器对象。如果它为空,则不会创建适配器对象。

现在无论何时引用适配器,都应该在以后执行任何操作之前检查它是否为空。

答案 3 :(得分:0)

尝试以下代码: -

public class MainActivity extends AppCompatActivity {
        private static final String URL = "http://domain/contact.php";
        CustomAdapter customAdapter;
        int success = 0;
        JSONParser jsonParser = new JSONParser();
        ArrayList<Contact> listContact = new ArrayList<Contact>();
        JSONObject json=null;
        JSONArray contact=null;ListView li;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            li = (ListView) findViewById(R.id.listView);

            new getContactAsync().execute();

            //int c = li.getAdapter().getCount(); not working

        }
    class getContactAsync extends AsyncTask<String,String,ArrayList<Contact>>{


            @Override
            protected ArrayList doInBackground(String... params) {
                    json = jsonParser.getFromURL(URL);
                try {
                    contact = json.getJSONArray("contact");
                    success = json.getInt(TAG_SUCCESS);
                    if (success == 1) {
                        for (int i = 0; i < contact.length(); i++) {
                            JSONObject c = contact.getJSONObject(i);
                            Contact a = new Contact();
                            a.setId(c.optInt(TAG_ID));
                            a.setName(c.optString(TAG_NAME));
                            a.setAddress(c.optString(TAG_ADDRESS));
                            listContact.add(a);
                        }
                    }
                } catch (JSONException e) {
                    Log.d("ERROR", e.toString());
                }
                return listContact;
            }

            @Override
            protected void onPostExecute(ArrayList<Contact> s) {
            super.onPostExecute(s);

            if(listContact.size()>0)
            {
              customAdapter = new CustomAdapter(MainActivity.this,listContact);
              li.setAdapter(customAdapter);
            }
            else
            {
            //empty
            }
  }

}