0

我想使用具有使用ArrayAdapter添加的自定義視圖的GridView設置Activity,但在模擬器上,gridview不顯示任何內容,這裏是我的代碼使用ArrayAdapter設置具有自定義視圖的GridView

用的GridView

的活動
public class ContactActivity extends AppCompatActivity{ 
@Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.contact_activity); 
    GridView gv = (GridView) findViewById(R.id.gv); 
    Contact c1 = new Contact(1,"a","ab","01","[email protected]"); 
    Contact c2 = new Contact(2,"b","cd","02","[email protected]"); 
    Contact c3 = new Contact(3,"c","ef","03","[email protected]"); 
    Contact c4 = new Contact(4,"d","gh","04","[email protected]"); 
    Contact c5 = new Contact(5,"e","ij","05","[email protected]"); 

    List<Contact> listc = new ArrayList<>(); 
    listc.add(c1); 
    listc.add(c2); 
    listc.add(c3); 
    listc.add(c4); 
    listc.add(c5); 
    ContactAdapter ca = new ContactAdapter(this, R.layout.item, listc); 
    gv.setAdapter(ca); 
    Intent intent = getIntent(); 
    final Intent i = new Intent(this, AjouterContactActivity.class); 
    FloatingActionButton a = (FloatingActionButton) findViewById(R.id.fab); 
    a.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      startActivity(i); 
     } 
    }); 
}} 

延伸ArrayAdapter

public class ContactAdapter extends ArrayAdapter { 
List<Contact> items; 
int viewResourecId; 

public ContactAdapter(Context context, int viewResourecId, List<Contact> items) { 
    super(context,viewResourecId); 
    this.viewResourecId=viewResourecId; 
    this.items=items; 
} 

@Override 
public int getCount() { 
    return 0; 
} 



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

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    View itemView= convertView; 
    if(convertView==null){ 
     LayoutInflater inflater1 = (LayoutInflater) ((ContactActivity)getContext()).getLayoutInflater(); 
     itemView = inflater1.inflate(viewResourecId,parent,false); 
    } 
    TextView tv1 = (TextView)itemView.findViewById(R.id.name); 
    TextView tv2 = (TextView)itemView.findViewById(R.id.cap); 
    tv2.setText(items.get(position).getNom().charAt(0)); 
    tv1.setText(items.get(position).getNom()+"   "+items.get(position).getPrenom()); 
    return itemView; 
    }} 

自定義項目佈局

0類
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"> 
<TextView 
    android:layout_width="192dp" 
    android:layout_height="50dp" 
    android:id="@+id/cap" 
    android:gravity="center|top" 
    /> 
<TextView 
    android:layout_width="192dp" 
    android:layout_height="10dp" 
    android:id="@+id/name" 
    android:layout_below="@+id/cap" 
    android:gravity="center|bottom" 
    /> 
</RelativeLayout> 

Here's an image of the acticivity

回答

0

你需要給一些代碼來getCount將和getItemId方法。例如:

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



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

現在它崩潰,而不是隻顯示任何東西 –

+0

你的崩潰是什麼?我認爲這是'items'上的NullPointerException。你需要初始化它:'List items = new ArrayList <>();'。你可能需要調用notifyDataSetChanged();在構造函數的最後,如果你沒有顯示任何東西(但我不確定)。 –

相關問題