2013-01-09 61 views
0

我正在使用自定義適配器爲ListView,每次在ListView設置行。 在行我想要兩個視圖可點擊,我想從兩個點擊事件。 設置OnItemClickListenerListView,但不工作,
我希望功能與callLog列表視圖(默認在手機中)完全相同。 請解決我的問題,如果你想我也可以在這裏粘貼我的代碼。onItemClickListener無法正常工作,我沒有收到事件當我點擊

package com.example.testsmspopup; 

公共類CallLogActivity延伸活動{

private ArrayList<CallDetail> callDetailArrayList; 
private ListView callDetailListView = null; 
private CallDetailListAdapter callDetailListAdapter; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    callDetailListView = (ListView) findViewById(R.id.contactListView); 
    callDetailListView.setOnItemClickListener(new OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 
      Log.i("MyTag","calling ..........................."); 
      Toast.makeText(getApplicationContext(), "onItemClick is calling", Toast.LENGTH_SHORT); 
     } 
    }); 
} 

@Override 
protected void onResume() { 
    // TODO Auto-generated method stub 
    super.onResume(); 
    callDetailArrayList = DB.getInstance(this).getAllCallDetails(); 
    callDetailListAdapter = new CallDetailListAdapter(); 
    callDetailListView.setAdapter(callDetailListAdapter); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.call_detail_activity, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    super.onOptionsItemSelected(item); 
    if (item.getItemId() == R.id.menu_clear_all) { 
     DB.getInstance(this).deleteAllCallDetails(); 
    } 
    return true; 
} 

private class CallDetailListAdapter extends ArrayAdapter<CallDetail> { 
    public CallDetailListAdapter() { 
     super(CallLogActivity.this, R.layout.call_log_row, callDetailArrayList); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View row = convertView; 
     CallDetailHolder holder = null; 
     if (row == null) { 
      LayoutInflater inflater = getLayoutInflater(); 
      row = inflater.inflate(R.layout.call_log_row, parent, false); 
      holder = new CallDetailHolder(row); 
      row.setTag(holder); 
     } else { 
      holder = (CallDetailHolder) row.getTag(); 
     } 
     holder.populateFrom(callDetailArrayList.get(position)); 
     return (row); 
    } 
} 

private class CallDetailHolder { 
    private TextView nameTextView = null; 
    private TextView numTextView = null; 
    private TextView timeTextView = null; 
    private ImageView typeImageView = null; 

    public CallDetailHolder(View row) { 
     nameTextView = (TextView) row.findViewById(R.id.callDetailName); 
     numTextView = (TextView) row.findViewById(R.id.callDetailNumber); 
     timeTextView = (TextView) row.findViewById(R.id.callDetailTime); 
     typeImageView = (ImageView) row.findViewById(R.id.callDetailType); 

// row.findViewById(R.id.callDetail).setOnClickListener(CallLogActivity.this); // row.findViewById(R.id.callBackBtn).setOnClickListener(CallLogActivity.this); }

public void populateFrom(CallDetail detail) { 
     String fName = DB.getInstance(CallLogActivity.this).getContact(detail.getCallNumber()).getFirstName(); 
     String lName = DB.getInstance(CallLogActivity.this).getContact(detail.getCallNumber()).getLastName(); 
     nameTextView.setText(fName + " " + lName); 
     numTextView.setText(detail.getCallNumber()); 
     timeTextView.setText(AppUtility.parseCallTime(detail.getCallTime())); 
     switch (detail.getCallType()) { 
     case AppConstants.INCOMING_CALL: 
      typeImageView.setImageResource(android.R.drawable.sym_call_incoming); 
      break; 
     case AppConstants.OUTGOING_CALL: 
      typeImageView.setImageResource(android.R.drawable.sym_call_outgoing); 
      break; 
     case AppConstants.MISS_CALL: 
      typeImageView.setImageResource(android.R.drawable.sym_call_missed); 
      break; 
     default: 
      break; 
     } 
    } 
} 

}

+2

是盡請粘貼代碼。 –

+0

@AndroSelva:請檢查它,我貼了代碼 – umesh

+0

告訴我們call_log_row.xml –

回答

2

對於顯示吐司內onItemClick()方法寫

Toast.makeText(getApplicationContext(), "onItemClick is calling", Toast.LENGTH_SHORT).show(); 

代替

Toast.makeText(getApplicationContext(), "onItemClick is calling", Toast.LENGTH_SHORT); 
1

你在你的列表項有一個ImageButton。它是focusable,它可以防止點擊事件發生。爲了獲得列表項的單擊事件,您必須將ImageButton設置爲不可聚焦。請注意,您必須在代碼中執行此操作,但不能在xml中執行。

就在您的CallDetailHolder構造函數中添加以下代碼,它應該很好地工作:

ImageButton callBackButton = (ImageButton) row.findViewById(R.id.callBackBtn); 
callBackButton.setFocusable(false); 
+0

我在CallDetailHolder(查看行)中添加了這一行{row35} – umesh

+0

是的,我犯了一個小錯誤。 'ImageButton'具有'R.id.callBackBtn' id。它現在有效嗎? –

+0

現在onItemClickListener調用,但是當我點擊按鈕,它不會調用 – umesh

相關問題