我是Android新手,我无法找到使用自定义适配器在列表视图中选择行的方法
Adapter class
public class HouseHoldExpenceAdapter extends BaseAdapter
implements View.OnClickListener {
LayoutInflater layoutInflater;
private List<HouseHoldExpence>houseHoldExpences;
private Context context;
public static final String ecategory="new_category";
public static final String econtent="new_content";
public static final String edate="new_date";
public static final String esum="expence_sum";
public static final String e_id="expenceId";
public HouseHoldExpenceAdapter(Context
context,List<HouseHoldExpence> houseHoldExpences)
{
this.houseHoldExpences=houseHoldExpences;
this.context=context;
}
@Override
public int getCount() {
return houseHoldExpences.size();
}
@Override
public HouseHoldExpence getItem(int position) {
return houseHoldExpences.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup
parent) {
View row = convertView;
ViewHolder viewHolder;
if(row==null)
{
layoutInflater=(LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = layoutInflater.inflate(R.layout.householdexpence, null);
viewHolder=new ViewHolder(row);
viewHolder.textViewCategory=
(TextView)row.findViewById(R.id.category);
viewHolder.textViewContent = (TextView)
row.findViewById(R.id.conntent);
viewHolder.textviewSum = (TextView)
row.findViewById(R.id.totalSum);
row.setTag(viewHolder);
}
else
{
viewHolder = (ViewHolder) row.getTag();
}
viewHolder.textViewCategory.setTextColor(Color.RED);
viewHolder.textViewCategory.setText
(houseHoldExpences.get(position).getCategory());
viewHolder.textViewContent.setText
(String.valueOf(houseHoldExpences.get(position).getContent()));
viewHolder.textviewSum.setText(String.
valueOf(houseHoldExpences.get(position).getSum()));
viewHolder.DeleteButton.setTag(position);
viewHolder.updateButton.setTag(position);
viewHolder.textViewCategory.setTextColor(Color.RED);
viewHolder.textViewContent.setTextColor(Color.RED);
viewHolder.textviewSum.setTextColor(Color.RED);
row.setOnClickListener(this);
return row;
}
@Override
public void onClick(View v) {
//How to call updateExpence(int position)
}
private class ViewHolder
{
TextView textViewCategory;
TextView textViewContent;
TextView textviewSum;
ImageButton DeleteButton;
Button updateButton;
private ViewHolder(View view) {
updateButton = (Button) view.findViewById(R.id.Update);
DeleteButton = (ImageButton) view.findViewById(R.id.Delete);
DeleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
new AlertDialog.Builder(context)
.setTitle("Delete entry")
.setMessage("Are you sure you want to delete
this entry?")
.setPositiveButton(android.R.string.yes, new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface
dialog, int which) {
remove((int)v.getTag());
}
})
.setNegativeButton(android.R.string.no, new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface
dialog, int which) {
// do nothing
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}
});
// updateButton.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(final View v) {
//
//
// updateExpence((int) v.getTag());
/ / }
// });
}
}
public void updateExpence(int position)
{
HouseHoldExpence houseHoldExpence=houseHoldExpences.get(position);
Intent intent=new Intent(context, HouseHoldExpence_update.class);
intent.putExtra(ecategory,houseHoldExpence.getCategory());
intent.putExtra(econtent,houseHoldExpence.getContent());
intent.putExtra(edate,houseHoldExpence.getDate());
intent.putExtra(esum, houseHoldExpence.getSum());
intent.putExtra(e_id, houseHoldExpence.getId());
context.startActivity(intent);
}
public void remove(int position)
{
HouseHoldExpence
houseHoldExpence=houseHoldExpences.get(position);
DatabaseManager.getInstance(context)
.deleteHoudeHoldeExpence(houseHoldExpence);
houseHoldExpences.remove(position);
notifyDataSetChanged();
}
public void setHouseHoldExpence
(List<HouseHoldExpence>houseHoldExpence)
{
this.houseHoldExpences=houseHoldExpence;
notifyDataSetChanged();
}
}
使用自定义适配器的列表视图 当我点击列表视图中的一行时,它显示所有列表项如何调用更新方法并传递适配器类中的位置..任何人都请帮助我....
答案 0 :(得分:0)
find the view in your getView()
of your row , and setOnClickListener
on it.
write the code of onClick in your adapter's getView()
method.
答案 1 :(得分:0)
你正在实施错误的倾听者。你应该实现OnItemClickListener
公共类HouseHoldExpenceAdapter扩展了BaseAdapter 实现AdapterView.OnItemClickListener
这将要求您实现一个类,该类提供单击项目的视图和位置
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//postion is the position of clicked item
// call the method from here
}
尝试在具有列表视图的类中实现OnItemCLickListener。这使工作更轻松。至少对我而言。
答案 2 :(得分:0)
in the adapter class
viewHolder.textViewCategory.setTag(position);
row.setOnClickListener(this);
@Override
public void onClick(View v) {
//How to call updateExpence(int position)
ViewHolder viewHolder = (ViewHolder) v.getTag();;
int i= (int) viewHolder.textViewCategory.getTag();
updateExpence(i);
}
这将有效......
答案 3 :(得分:0)
您应该使用OnItemClickListener并将其分配给列表视图。
listview.setOnItemClickListener(new CustomOnItemClickListener());
可以在此处找到完整的代码示例:Get Selected Items from ListView