我有一个名为activity_login_user_entity.xml的自定义布局
<ListView
android:id="@+id/activity_login_user_entity_users"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:clickable="true"
android:divider="#FFECECEC"
android:dividerHeight="1dp"
android:focusable="true" />
和XML适配器:
<ImageButton
android:id="@+id/adapter_user_login"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@null"
android:paddingLeft="15dp"
android:paddingStart="15dp"
android:src="@drawable/ic_login" />
<com.neopixl.pixlui.components.textview.TextView
android:id="@+id/adapter_user_entity"
style="@style/BoldFont"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="start|center"
android:padding="20dp"
android:text="15/05/2015"
android:textColor="@color/colorBaseApp"
android:textSize="14sp" />
我的适配器类:
public class UserAdapter extends BaseAdapter {
private ArrayList<User> dataList;
private final LayoutInflater mInflater;
private final Context mainActivity;
public UserAdapter(Context context, ArrayList<User> results) {
super();
this.dataList = results;
this.mainActivity = context;
this.mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return dataList.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return dataList.get(arg0);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.adapter_user, null);
holder = new ViewHolder();
holder.entity = (TextView) convertView.findViewById(R.id.adapter_user_entity);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.entity.setText(dataList.get(position).getParticipantEntityName());
return convertView;
}
static class ViewHolder {
TextView entity;
}
@Override
public boolean isEnabled(int position) {
return true;
}
}
对话:
final Dialog new_dialog = new Dialog(LoginActivity.this);
new_dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
new_dialog.setContentView(R.layout.activity_login_user_entity);
new_dialog.setCancelable(false);
ListView lv = (ListView) new_dialog.findViewById(R.id.activity_login_user_entity_users);
UserAdapter cdaa = new UserAdapter(getApplicationContext(), users);
lv.setAdapter(cdaa);
new_dialog.show();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(LoginActivity.this, "Item Selected: " + position, Toast.LENGTH_SHORT).show();
}
});
我的问题是,所有内容都显示得很好,项目按照我希望的方式存在,但是无法检测到列表视图的项目被点击。有人可以帮帮我吗?
编辑:
我设法使用以下代码:
final ArrayAdapter<User> adapter = new ArrayAdapter<>(this, android.R.layout.select_dialog_item);
adapter.addAll(users);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
loginSuccess(adapter.getItem(item));
}
});
AlertDialog alert = builder.create();
alert.show();
但是,我无法获得我想要的自定义布局。
答案 0 :(得分:0)
使用and WeekDate not like '2015-12-27'
设置内容
LayoutInflater将布局XML文件实例化为其对应的 查看对象。
LayoutInflater
答案 1 :(得分:0)
您必须使用LayoutInflater来扩展contentView。列表视图应该从contentView引用,而不是从对话框对象引用。
代码将更改为
//Inflate the content view
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View contentView = layoutInflater.inflate(R.layout. activity_login_user_entity, null, false);
//ListView related activities
ListView lv = (ListView) contentView.findViewById(R.id.activity_login_user_entity_users);
UserAdapter cdaa = new UserAdapter(getApplicationContext(), users);
lv.setAdapter(cdaa);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(LoginActivity.this, "Item Selected: " + position, Toast.LENGTH_SHORT).show();
}
});
//Dialog creation
final Dialog new_dialog = new Dialog(LoginActivity.this);
new_dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
new_dialog.setContentView(contentView);
new_dialog.setCancelable(false);
//Dialog display
new_dialog.show();
您需要停用 ImageButton 焦点。将此属性添加到XML布局文件中的 ImageButton 。
android:focusable="false"
android:focusableInTouchMode="false"