我有一个自定义列表视图,每行包含两个按钮,我正在努力的是这些按钮的监听器。我的Listview包含在AlertDialog中,这是我的代码
@Override
public void displayUnders(List<UndersLM> ulm) {
AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
ArrayList<UndersLM> undersreturn = new ArrayList<UndersLM>();
final ListView L = new ListView(ctx);
final UndersLM y = new UndersLM();
for (UndersLM aulm : ulm) {
final UndersLM s = new UndersLM();
s.set_id(aulm.get_id());
s.set_cartonid(aulm.get_cartonid());
s.set_sku(aulm.get_sku());
s.set_sentqty(aulm.get_sentqty());
s.set_scannedqty(aulm.get_scannedqty());
undersreturn.add(s);
}
uadaptor = new Unders(ctx, undersreturn);
L.setAdapter(uadaptor);
builder.setView(L);
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Object rid = parent.getAdapter().getItem(position);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
L.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Object rid = parent.getAdapter().getItem(position);
}
});
AlertDialog d;
d = builder.create();
d.show();
}
正如您所看到的,我已经尝试了ListView上的ItemClickListerner,以及AlertDialog上的itemSelected。
我错过了什么?这些都没有击中Object rid = parent .... lines
答案 0 :(得分:0)
编辑 - 实际上忽略了这一点 - 它的崩溃!
好的,解决了。
你基本上必须在自定义适配器中执行此操作(据我所知),所以在我的情况下这有用:
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
final ViewHolder holder;
if (convertView == null) {
vi = inflater.inflate(R.layout.unders, null);
holder = new ViewHolder();
holder.id = (TextView) vi.findViewById(R.id.id);
holder.cartonID = (TextView) vi.findViewById(R.id.cartonID);
holder.Sku = (TextView) vi.findViewById(R.id.Sku);
holder.Sent = (TextView) vi.findViewById(R.id.Sent);
holder.add = (Button) vi.findViewById(R.id.add);
holder.Scanned = (TextView) vi.findViewById(R.id.Scanned);
holder.subtract = (Button) vi.findViewById(R.id.subtract);
vi.setTag(holder);
} else {
holder = (ViewHolder) vi.getTag();
}
if (Lines.size() < -0) {
holder.id.setText("No Unprocessed Deliveries");
} else {
tempValues = null;
tempValues = (UndersLM) Lines.get(position);
holder.id.setText(String.valueOf(tempValues.get_id()));
holder.cartonID.setText(String.valueOf(tempValues.get_cartonid())+ " | ");
holder.Sku.setText(String.valueOf(tempValues.get_sku()) + " | ");
holder.Sent.setText(String.valueOf(tempValues.get_sentqty()) + " | ");
holder.Scanned.setText(String.valueOf(tempValues.get_scannedqty()));
holder.add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new underCorrections(ctx,true,tempValues.get_cartonid(),tempValues.get_sku()).execute();
holder.Scanned.setText(String.valueOf(tempValues.get_scannedqty()+1));
}
});
}
return vi;
}