Android ArrayList适配器不显示ListView

奥斯卡·莫雷诺(Oscar Moreno)

我有一个ArrayList由适配器表示在ListView中:

private ArrayList <Contact> listContacts = new ArrayList <Contact> ();

这是(简单的)Contact类:

public class Contact {
    String pic;
    String name;
    String surname1;
    String surname2;
    String phonenumber;
}

这是适配器类:

public class ContactsAdapter extends ArrayAdapter<Contact> {

    private static class ViewHolder {
        ImageView pic;
        TextView name;
        TextView surname1;
        TextView surname2;
        TextView phonenumber;
    }

    Context context;

    public ContactsAdapter(Context context, int textViewResourceId, ArrayList<Contact> items) {
        super(context, textViewResourceId, items);
        this.context = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;

        if (convertView == null) {
            convertView = LayoutInflater.from(this.getContext())
                    .inflate(R.layout.layout_contact, parent, false);

            holder = new ViewHolder();
            holder.pic= (ImageView) convertView.findViewById(R.id.pic);
            holder.name= (TextView) convertView.findViewById(name);
            holder.surname1= (TextView) convertView.findViewById(R.id.surname1);
            holder.surname2= (TextView) convertView.findViewById(R.id.surname2);
            holder.phonenumber= (TextView) convertView.findViewById(R.id.phonenumber);

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

        Contact item = getItem(position);

        if (item!= null) {
            int idImage = context.getResources().getIdentifier(item.pic, "drawable", context.getPackageName());
            holder.pic.setImageResource(idImage);

            holder.name.setText(item.name);
            holder.surname1.setText(item.surname1);
            holder.surname2.setText(item.surname2);
            holder.phonenumber.setText(item.phonenumber);
        }

        return convertView;
    }
}

MainActivity是这样的:

public class MainActivity extends ListActivity {

    private ArrayList <Contact> listContacts = new ArrayList <Contact> ();
    ContactsAdapter contactsAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        boolean result = loadListFromFiles();
        if (result) {
            loadContacts();
        }
    }

    private void loadContacts() {

        contactsAdapter = new ContactsAdapter(getApplicationContext(), R.layout.activity_main, listContacts);

        setListAdapter(contactsAdapter);
    }

loadListFromFiles代码:

private boolean loadListFiles(){

        boolean result = false;
        Context context = getApplicationContext();
        File path = context.getFilesDir();

        File[] files = path.listFiles();
        Log.d("Files", "Size: "+ files.length);

        for (int i = 0; i < files.length; i++)
        {
            Log.d("Files", "FileName:" + files[i].getName());
            result = loadFile(files[i].getName());
        }
        return resultado;
    }

    private boolean loadFile(String fileName) {

        String[] arrayContact = new String[4];

        try
        {
            BufferedReader fin =
                    new BufferedReader(
                            new InputStreamReader(
                                    openFileInput(fileName)));

            int i = 0;

            String line= "";
            while ((line= fin.readLine()) != null) {
                arrayContact[i] = linea;
                i++;
            }

            fin.close();

            Contact contact = new Contact();

            contact.pic = arrayContact[3];
            contact.name= arrayContact[0];
            contact.surname1 = arrayContact[1];
            contact.surname2 = arrayContact[2];
            contact.phonenumber= arrayContact[3];

            listContacts.add(contact);

            return true;

        }
        catch (Exception ex)
        {
            Log.e("Files", "Error to read file by memory");
            return false;
        }

    }

ArrayList是正确的,但布局未显示任何内容。

Sanjeet

覆盖getCount()getItem()返回计数和联系方式的方法。

@Override
public int getCount() {
    if(items == null)
        return 0;
    return items.size();
}

@Override
public Contact getItem(int i) {
    return items.get(i);
}

另外,itemsAdapter类中创建一个变量并将其分配给传递给的参数adapter constructor

ArrayList<Contact> items;

public ContactsAdapter(Context context, int textViewResourceId, ArrayList<Contact> items) {
    super(context, textViewResourceId, items);
    this.context = context;
    this.items = items;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

android recyclerview适配器进度条不显示

Android-在Listview中显示来自适配器的数据

从适配器android返回更改的arrayList

Android ListView适配器未过滤

Android ListView适配器...不重绘

自定义Android适配器从不显示数据

具有两个ArrayList的Android ListView适配器

如何使用适配器在Android ListView中显示Firestore集合

带有自定义适配器的 Android ListView 仅显示最后一项

Android Studio ListView将不会显示适配器中的项目

Android中的ListView和适配器,是否属于适配器设计模式?

如何从Android Kotlin中的RecyclerView适配器获取arraylist

从Android中的ListView适配器获取TextView

Android ListView适配器异步任务图像加载

适配器的类getView()函数不叫为Android的ListView

Android自定义ListView /适配器

在Android中刷新ListView适配器的正确方法

Android ListView onItemclick以及自定义适配器

Android ListView适配器崩溃问题/重复项

Android:在ListView问题之外更改了适配器

AlertDialog中的Android ListView-设置适配器

Android ListView中适配器和模板视图的组合

Android:从viewPager中的Fragment调用ListView的适配器的getview()

我的Android Listview适配器中的IndexOutOfBoundsException

自定义ListView适配器Android

Android Xamarin-具有ObservableCollection的ListView适配器

用于循环适配器更新的 Android Studio Listview

Android ListView onClickListener自定义适配器

如何在其适配器中刷新android listview