我正在为ListView
使用自定义适配器,每次都在ListView
设置行。
在行中我希望有两个可点击的视图,我希望两者都有点击事件。
将OnItemClickListener
设置为ListView
,但不能正常工作,
我希望功能与callLog列表视图完全相同(默认在手机中)。
请解决我的问题,如果你想我也可以在这里粘贴我的代码。
package com.example.testsmspopup;
公共类CallLogActivity扩展了Activity {
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;
}
}
}
}
答案 0 :(得分:2)
用于在onItemClick()方法中显示Toast写入
Toast.makeText(getApplicationContext(), "onItemClick is calling", Toast.LENGTH_SHORT).show();
而不是
Toast.makeText(getApplicationContext(), "onItemClick is calling", Toast.LENGTH_SHORT);
答案 1 :(得分:1)
您的列表项中有ImageButton
。它是focusable
,它可以防止点击事件中的整个项目。为了获得列表项的点击事件,您必须将ImageButton
设置为不可聚焦。请注意,您必须在代码中执行此操作,但不能在xml
中执行。
只需在CallDetailHolder
构造函数中添加此代码,它就可以正常运行:
ImageButton callBackButton = (ImageButton) row.findViewById(R.id.callBackBtn);
callBackButton.setFocusable(false);