0

私はAndroidの電話の連絡先をすべて表示するアクティビティを開発しました。リストビューを使用して検索した連絡先を表示しています。問題は、リストアイテムをクリックするたびにListViewをスクロールしないうちに、特定の連絡先レコードの詳細が表示されるまでです。しかし、リスト内の項目をスクロールしてクリックすると、アプリケーションがクラッシュしてNullPointerExceptionがスローされます。私が間違っているところや私の結果を達成する方法については、誰でも私を導くことができます。これは重複した質問かもしれませんが、私は成功していないネット全体を検索しようとしました。下記の連絡先を表示する私の活動のコードスニペットです:スクロール後にListViewのアイテムのonItemClickスローNullPointerException

public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.contact_manager); 

    filterText = (EditText) findViewById(R.id.search_box);  
    filterText.addTextChangedListener(filterTextWatcher); 

    mContactList = (ListView) findViewById(R.id.contactList); 
    populateContactList();  

} 

private void populateContactList() 
{ 
    // Another class used to retrieve contacts 
       ContactAPI contactAPI = ContactAPI.getAPI(); 
    ContentResolver cr = getContentResolver(); 
    contactAPI.setCr(cr); 

    ArrayList<Contact> a = new ArrayList<Contact>(); 
    a = contactAPI.newContactList(this).getContacts(); 

    contactsAL = new ArrayList<Contact>(a);  

    adapter = new CustomAdapter(this, R.layout.contact_entry, a);  

    mContactList.setAdapter(adapter); 

    mContactList.setOnItemClickListener(new OnItemClickListener(){ 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
    {    

       ***// The below is the line where I am getting NullPointerException*** 
     LinearLayout l = (LinearLayout) parent.getChildAt(position); 
     TextView tv = (TextView) l.getChildAt(1); 

     Intent myIntent = new Intent(view.getContext(), ContactDetailsActivity.class); 

     Bundle b = new Bundle(); 
     b.putString("ContactID", tv.getText().toString()); 
     myIntent.putExtras(b); 

     startActivityForResult(myIntent, 0); 
    } 
    }); 



private TextWatcher filterTextWatcher = new TextWatcher() 
{  
    public void afterTextChanged(Editable s) 
    {  

    }  
    public void beforeTextChanged(CharSequence s, int start, int count, int after) 
    {  

    }  
    public void onTextChanged(CharSequence s, int start, int before, int count) 
    {   
     adapter.getFilter().filter(s); 
    } 
}; 

protected void onDestroy() 
{  
    super.onDestroy();  
    filterText.removeTextChangedListener(filterTextWatcher); 
} 

そして、以下は私のカスタムアダプタクラスのコードスニペットです:私はどこ

private ArrayList<Contact> original; 
private ArrayList<Contact> fitems; 
private Filter filter; 

private LayoutInflater mInflater; 

public CustomAdapter(Context context, int layoutResourceId, ArrayList<Contact> data) 
{ 
    super(context, layoutResourceId, data); 
    this.layoutResourceId = layoutResourceId; 
    this.context = context; 
    this.original = new ArrayList<Contact>(data);   
    this.fitems = new ArrayList<Contact>(data); 
} 

public long getItemId(int position) { 
    return position; 
} 

public View getView(int position, View convertView, ViewGroup parent) 
{  
    ViewHolder holder; 

    if (convertView == null) 
    { 
     mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);   
     convertView = mInflater.inflate(R.layout.contact_entry, null); 
     holder = new ViewHolder();   
     holder.txtName = (TextView) convertView.findViewById(R.id.contactEntryText); 
     holder.txtID = (TextView) convertView.findViewById(R.id.contactID); 

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

    if(fitems != null) 
    { 
     Contact ct = fitems.get(position); 
     if(ct != null) 
     { 
      holder.txtName.setText((String)ct.getDisplayName()); 
      holder.txtID.setText((String)ct.getId()); 
     } 
    } 

    //this.notifyDataSetChanged(); 

    return convertView; 
} 

@Override 
public Filter getFilter() { 
    // TODO Auto-generated method stub 
    // return super.getFilter(); 

    if (filter == null)   
     filter = new ContactFilter();  

    return filter; 
} 


private class ContactFilter extends Filter 
{ 
    @Override 
    protected FilterResults performFiltering(CharSequence constraint) 
    { 
     FilterResults results = new FilterResults(); 
     String prefix = constraint.toString().toLowerCase(); 

     if (prefix == null || prefix.length() == 0) 
     { 
      ArrayList<Contact> list = new ArrayList<Contact>(original); 
      results.values = list; 
      results.count = list.size(); 
     } 
     else 
     { 
      final ArrayList<Contact> list = new ArrayList<Contact>(original); 
      final ArrayList<Contact> nlist = new ArrayList<Contact>(); 
      int count = list.size(); 

      for (int i=0; i<count; i++) 
      { 
       final Contact cont = list.get(i); 
       final String value = cont.getDisplayName().toLowerCase(); 

       if (value.contains(prefix)) 
       { 
        nlist.add(cont); 
       } 
      } 
      results.values = nlist; 
      results.count = nlist.size(); 
     } 
     return results; 
    } 

    @SuppressWarnings("unchecked") 
    @Override 
    protected void publishResults(CharSequence constraint, FilterResults results) 
    { 
     fitems = (ArrayList<Contact>)results.values; 

     clear(); 
     int count = fitems.size(); 
     for (int i=0; i<count; i++) 
     { 
      Contact cont = (Contact)fitems.get(i); 
      add(cont); 
     } 
    } 
} 

static class ViewHolder { 
    TextView txtName; 
    TextView txtID; 
} 

誰も私を提案してくださいすることができ間違っている?

答えて

0

int onItemClick CustomAdapterを呼び出します。 getItem(int)の位置にあるContactオブジェクトを検索するには、新しいアクティビティを開始するために必要な情報を取得するために連絡先を使用します。

+0

をTextViewにするclicklistenerを追加するには、指導のために、@blackbeltいただきありがとうございます。私はあなたが示唆したようにして、うまくいきます。多くのありがとう –

0

あなたのメインアクティビティにonitemclicklistenerを設定から削除してください。 CustomAdapterで

は、GetViewメソッドの内部で、あなただけの

holder.txtName.setOnclickListener(new OnClickListener() { 

    @Override 
    public void onClick() { 
    Intent myIntent = new Intent(view.getContext(), ContactDetailsActivity.class); 

    Bundle b = new Bundle(); 
    b.putString("ContactID", holder.txtName.getText().toString()); 
    myIntent.putExtras(b); 

    startActivityForResult(myIntent, 0); 

    } 
}) 
+0

あなたのポインタのために@Raghu Nagarajuありがとうございます。あなたの方法も試してみて、あなたを更新します。再度、感謝します.. –

関連する問題