2016-10-30 4 views
0

は表示されません。AndroidのArrayListのアダプタは、私がアダプターによって、リストビューで表現するのArrayListを持つListViewの

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のは正しいですが、レイアウトは何も表示されません。

+0

問題がありますか? 'listContacts'は空であるようです。何かを追加する –

+0

'getApplicationContext()'の代わりに 'MainActivity.this'を使用してください –

+0

あなたのアダプタのgetCount()をオーバーライドして値を' Log'し、配列が空でないことを確認してください –

答えて

0

getCount()getItem()をオーバーライドして連絡先を返す方法を上書きします。

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

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

また、Adapterクラスに一つの変数itemsを作成し、adapter constructorに渡されたパラメータに割り当てます。

ArrayList<Contact> items; 

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

あなたの答えをありがとうが、それでも動作しません。 –

+0

@OscarMorenoは 'getCount()'の中にログを記録し、返される値を確認します。 – Sanjeet

+0

は返されます4. –

関連する問題