2017-03-16 9 views
-1

カスタムリストアダプターの作成方法についてはhttps://github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView#using-a-custom-arrayadapterを使用しています。しかし、エラーがここに表示されます(RegisterAccountクラスでこのコード)Androidリストアダプター非静的メソッドadd(T)は静的コンテキストから参照できません

- 。しかし、私は

私はShop shop = new Shop (uid, name, address,email); listadapter.add(shop); <によってアダプタにデータを追加しようと、「非静的メソッド(T)を追加」の問題を得ました

は、それは私のlistAdapterクラス

public class shopListAdapter extends ArrayAdapter<Shop> { 


public shopListAdapter(Context context, ArrayList<Shop> shops) { 
    super(context, 0, shops); 

} 

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

    Shop shop = getItem(position); 

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

    TextView name = (TextView) convertView.findViewById(R.id.nameTV); 
    TextView address = (TextView) convertView.findViewById(R.id.addressTV); 

    name.setText(shop.getShopName()); 
    address.setText(shop.getAddress()); 

    return convertView; 
} 
} 

モデル

public Shop(String shopID, String name, String address, String email){ 
    this.shopID = shopID; 
    this.name = name; 
    this.address = address; 
    this.email = email; 
} 
public void setShopID(String shopID) { 
    this.shopID = shopID; 
} 

public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

public void setAddress(String address) { 
    this.address = address; 
} 

public String getEmail() { 
    return email; 
} 

public void setEmail(String email) { 
    this.email = email; 
} 
です(断片で)

活動

private void initAdapter(){ 

    ArrayList<Shop> arrayshop = new ArrayList<Shop>(); 

    shopListAdapter adapter = new shopListAdapter(getActivity(), arrayshop); 

    listView.setAdapter(adapter); 

} 

RegisterAccount

mAuth = FirebaseAuth.getInstance(); 
    mProgress = new ProgressDialog(this); 

    businessAccount = FirebaseDatabase.getInstance().getReference().child("businessAcc"); 
protected void startSetupAccount() { 

    final String address = addressTF.getText().toString().trim(); 
    final String name = mNameField.getText().toString().trim(); 
    final String user_id = mAuth.getCurrentUser().getUid(); 
    final String email = mAuth.getCurrentUser().getEmail(); 
    businessAccount.child(user_id).child("uid").setValue(user_id); 
    businessAccount.child(user_id).child("username").setValue(name); 
    businessAccount.child(user_id).child("address").setValue(address); 
    businessAccount.child(user_id).child("email").setValue(email); 
Shop shop = new Shop(uid,name,address,email); 

      //shopListAdapter.add(shop); 


       mProgress.dismiss(); 

      } 
     }); 
    } 
} 
+0

カスタムアダプターはHairShopを受け入れますが、ショップを追加している間は、 –

+0

Register Registerコード... –

+0

編集済み申し訳ありません.... – kings077712

答えて

0

アダプタオブジェクトは、 "アダプタ" およびない "shopListAdapter" ので、代わりの

shopListAdapter.add(shop); 

これを行うです

adapter.add(shop); 

あなたのクラスの最初の文字は常に大文字にする必要があります。 これをお読みくださいhttp://www.oracle.com/technetwork/java/codeconventions-135099.html

+0

これは私が持っているエラーの理由ですか? – kings077712

+0

はい。クラス名でaddを呼び出していますが、アダプタのaddメソッドは静的ではないため、アダプタのオブジェクトで呼び出される必要があります。この場合は「アダプタ」です。 –

+0

thxしかし、答えを編集した後、なぜデータを追加しようとするとこのエラーが出るのですか?(java.lang.NullPointerException:仮想メソッド 'void android.widget.ArrayAdapter.add(java.lang.Object)'を呼び出そうとしています) – kings077712

関連する問題