2016-07-31 3 views
0

私はArrayListのアンドロイド:コンストラクタクラスContactsAdapterでContactsAdapterが与えられたタイプに適用することはできません

とリストビューを移入しようとしているが、これは私のコードです:

ArrayList<Contact> results = mapper.readValue(response.toString(), 
           new TypeReference<ArrayList<Contact>>() { });//results are coming OK 

         ContactsAdapter adapter = new ContactsAdapter(this, R.layout.list_view_items, results);//error is here 

、これが私のカスタムアダプタクラスです、そのそれぞれのコンストラクタを持つ:

public class ContactsAdapter extends ArrayAdapter<Contact> { 

    ArrayList<Contact> contactList = new ArrayList<>(); 

    public ContactsAdapter(Context context, int textViewResourceId, ArrayList<Contact> objects) { 
     super(context, textViewResourceId, objects); 
     contactList = objects; 
    } 

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

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View v = convertView; 
     LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     v = inflater.inflate(R.layout.list_view_items, null); 
     TextView contactTextView = (TextView) v.findViewById(R.id.contactTextView); 
     TextView phoneTextView = (TextView) v.findViewById(R.id.phoneTextView); 
     ImageView imageView = (ImageView) v.findViewById(R.id.smallImageView); 
     contactTextView.setText(contactList.get(position).getName()); 
     phoneTextView.setText(contactList.get(position).getPhone().toString()); 
     //imageView.setImageResource(animalList.get(position).getAnimalImage()); 
     return v; 
    } 

} 

エラー:

Error:(58, 51) error: constructor ContactsAdapter in class ContactsAdapter cannot be applied to given types; required: Context,int,ArrayList found: >,int,ArrayList reason: actual argument > cannot be converted to Context by method invocation conversion

私のデータはjSONから取得され、ArrayListとして宣言されています。私はコンストラクタを変更するようなことをいくつか試しましたが、うまく動作しないと私は固執します。

ありがとうございます!

+2

あなたの 'Adapter'コンストラクタ呼び出しは明らかに匿名クラスの中にあるので、' this'は最初の引数として必要な 'Context'ではなく、その匿名クラスを参照します。そのコードが 'Activity'にある場合、' Activity'名の前に追加してください。例えば、「MyActivity.this」。 –

+1

おい、あなたは私の一日を作る!あなたのコメントは最高の答えになるはずです! – emboole

答えて

1

Error:(58, 51) error: constructor ContactsAdapter in class ContactsAdapter cannot be applied to given types; required: Context,int,ArrayList<Contact> found: <anonymous Listener<JSONArray>>,int,ArrayList<Contact> reason: actual argument <anonymous Listener<JSONArray>> cannot be converted to Context by method invocation conversion

あなたAdapterコンストラクタ呼び出しがそうthisは、その匿名クラスを指し、匿名クラス内で明らかにされていないContextあなたが最初の引数として必要です。そのコードがActivityの場合はActivityの名前の前にthisを追加してください。例:MyActivity.this

+0

私はあなたの答えなくても2日以上試して失敗することができます。ありがとうございました! – emboole

関連する問題