大家好,我有一个简单的列表视图和一个扩展的 baseadapter,我想实现以下功能。
我有一个简单的列表项布局:1 个复选框和两个列表视图。
单击列表项(整个布局,而不仅仅是复选框)时,复选框状态应更改。
我怎样才能实现这个功能?
这是我到目前为止所做的:
我的适配器:
package ro.gebs.captoom.adapters;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import java.text.DecimalFormat;
import java.util.ArrayList;
import ro.gebs.captoom.R;
import ro.gebs.captoom.datamodel.Mileage;
import ro.gebs.captoom.datamodel.Receipt;
import ro.gebs.captoom.interfaces.MileageReceiptInterface;
import ro.gebs.captoom.utils.fonts.CustomFontTextView;
/**
* Created by Adrian on 9/19/13.
*/
public class SyncItemAdapter extends BaseAdapter {
private ArrayList<MileageReceiptInterface> data;
private Context context;
public SyncItemAdapter(Context context, ArrayList<MileageReceiptInterface> data) {
this.context = context;
this.data = data;
}
@Override
public int getCount() {
return data.size();
}
@Override
public MileageReceiptInterface getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final MileageReceiptInterface item = getItem(position);
ViewHolder holder = null;
if (convertView == null) {
LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (item != null) {
holder = new ViewHolder();
if (item.getMyType() == MileageReceiptInterface.I_AM_MILEAGE) {
convertView = li.inflate(R.layout.sync_mileage_item, parent, false);
} else {
convertView = li.inflate(R.layout.sync_receipt_item, parent, false);
}
holder.clickMe = (LinearLayout) convertView;
holder.itemDate = (CustomFontTextView) convertView.findViewById(R.id.item_date);
holder.itemStatus = (CheckBox) convertView.findViewById(R.id.item_status);
holder.itemValue = (CustomFontTextView) convertView.findViewById(R.id.value);
convertView.setTag(holder);
}
final ViewHolder finalHolder = holder;
holder.clickMe.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finalHolder.itemStatus.toggle();
}
});
} else {
holder = (ViewHolder) convertView.getTag();
}
DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(2);
if (item != null) {
if (item.getMyType() == MileageReceiptInterface.I_AM_MILEAGE) {
final Mileage mileage = (Mileage) item;
holder.itemDate.setText(mileage.getStartDate());
holder.itemValue.setText(df.format(mileage.getDistance() * mileage.getPrice()) + " " + mileage.getCurrency());
} else {
final Receipt receipt = (Receipt) item;
holder.itemDate.setText(receipt.getDate());
holder.itemValue.setText(df.format(receipt.getValue()) + " " + receipt.getCurrency());
}
}
return convertView;
}
private class ViewHolder {
LinearLayout clickMe;
CheckBox itemStatus;
CustomFontTextView itemDate;
CustomFontTextView itemValue;
}
}
在这里,我将我的适配器链接到我的列表:
final MergeAdapter all_adapter = new MergeAdapter();
ArrayList<Folder> folders = new FolderDataSource().selectAllFolders();
for (Folder folder : folders) {
final TreeMap<Long, ArrayList<MileageReceiptInterface>> sortedFolderItems = new ReceiptDataSource().loadUnsyncReceipts(folder.getId());
if (!sortedFolderItems.isEmpty()) {
final View header1 = inflater.inflate(R.layout.receipt_list_header, null);
assert header1 != null;
CustomFontTextView receipt_header_txt = (CustomFontTextView) header1.findViewById(R.id.receipt_header_txt);
receipt_header_txt.setText(folder.getTitle());
all_adapter.addView(header1);
for (long receiptTimeStamp : sortedFolderItems.descendingKeySet()) {
SyncItemAdapter folder_stuff = new SyncItemAdapter(mContext, sortedFolderItems.get(receiptTimeStamp));
all_adapter.addAdapter(folder_stuff);
}
}
}
folder_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(mContext, "You clicked an item", Toast.LENGTH_SHORT).show();
}
});
但是,我似乎无法正确捕获项目单击事件,因为当我单击列表中的任何项目时没有出现吐司...
有任何想法吗?
编辑:对我的适配器类的小改动,现在它工作得很好