我需要从sqlite数据库填充listview。现在,当我单击列表中的行时,它将显示在详细视图中。我想从sqlite表中获取行ID,我想传递给下一个intent.How can I获取sqlite表的id。
这是我的代码,
public class Message_Adapter extends ArrayAdapter<Message> {
Activity activity;
int layoutResourceId;
Message user;
ArrayList<Message> data = new ArrayList<Message>();
public Message_Adapter(Activity act, int layoutResourceId,
List<Message> data) {
super(act, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.activity = act;
this.data = (ArrayList<Message>) data;
notifyDataSetChanged();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
UserHolder holder = null;
if (row == null) {
LayoutInflater inflater = LayoutInflater.from(activity);
row = inflater.inflate(layoutResourceId, parent, false);
holder = new UserHolder();
holder.fromnum = (TextView) row.findViewById(R.id.fromno);
holder.tonum = (TextView) row.findViewById(R.id.tonum);
holder.body= (TextView) row.findViewById(R.id.msgbody);
holder.date = (TextView) row.findViewById(R.id.msgdate);
holder.delete = (Button) row.findViewById(R.id.btn_delete);
holder.status = (TextView) row.findViewById(R.id.staty);
row.setTag(holder);
} else {
holder = (UserHolder) row.getTag();
}
user = data.get(position);
holder.delete.setTag(user.getID());
holder.fromnum.setText(user.getmsgfrom());
holder.tonum.setText(user.getto());
holder.body.setText(user.getmsg());
holder.date.setText(user.getdate());
holder.status.setText(user.getstatus());
itemClickListener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view,
int pos, long arg3) {
int product = user.getID();
// Launching new Activity on selecting single List Item
Intent i = new Intent(getApplicationContext(), DetailedMsg.class);
// sending data to new activity
i.putExtra("Product", product);
startActivity(i);
}
};
holder.delete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(final View v) {
// TODO Auto-generated method stub
// show a message while loader is loading
AlertDialog.Builder adb = new AlertDialog.Builder(activity);
adb.setTitle("Delete?");
adb.setMessage("Are you sure you want to delete ");
final int user_id = Integer.parseInt(v.getTag().toString());
adb.setNegativeButton("Cancel", null);
adb.setPositiveButton("Ok",
new AlertDialog.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
// MyDataObject.remove(positionToRemove);
DatabaseHandler dBHandler = new DatabaseHandler(
activity.getApplicationContext());
dBHandler.Delete_Contact(user_id);
InboxActivity.this.onResume();
}
});
adb.show();
}
});
return row;
}
}
class UserHolder {
TextView fromnum;
TextView tonum;
TextView body;
TextView date;
TextView status;
Button delete;
}
答案 0 :(得分:0)
long arg3
是id。所以这应该有效:
.....
i.putExtra("idInDB", arg3);
.....