我有一个onItemClickListener列表。在列表中是日期和地点。 我想将这个地方(点击时)传递给数据库处理程序,然后在新活动中获取具有相同位置的所有数据。
我在Android方面不太好,所以我问这是否有可能做到这一点?以及如何将带有地名的变量传递给dbHandler?
这是我的onItemClickListener
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_listview);
mainListView = (ListView) findViewById(R.id.ListViewItem);
dbHandler = new LogsDBHandler(this);
ArrayList<Logs> logsList = dbHandler.getAllLogsGroupedByPlace();
listAdapter = new LogsByPlaceAdapter(logsList);
mainListView.setAdapter(listAdapter);
mainListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(DisplayLogsByPlace.this, LogsList.class);
startActivity(intent);
}
});
}
private class LogsByPlaceAdapter extends BaseAdapter {
private LayoutInflater inflater;
private List<Logs> logsList;
public LogsByPlaceAdapter(List<Logs> logsList) {
inflater = LayoutInflater.from(DisplayLogsByPlace.this);
this.logsList = logsList;
}
@Override
public int getCount() {
return logsList.size();
}
@Override
public Object getItem(int position) {
return logsList.get(position);
}
@Override
public long getItemId(int position) {
return logsList.get(position).getId();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.activity_display_logs_by_place, parent, false);
}
Logs log = logsList.get(position);
((TextView) convertView.findViewById(R.id.textShowDate)).setText(log.getCreatedAt().toString());
((TextView) convertView.findViewById(R.id.textShowPlace)).setText(log.getPlace());
return convertView;
}
}
这是我的dbHandler,我将其称为此变量
public ArrayList<Logs> getAllLogsWithSamePlace(String place) {
ArrayList<Logs> logPlaceList = new ArrayList<Logs>();
String selectQuery = "SELECT * FROM " + TABLE_LOGS + " WHERE " + KEY_PLACE + "=" + place;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
...
}
return logPlaceList;
}
答案 0 :(得分:0)
mainListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// if your method is static
ArrayList<String> array = dbHandler.getAllLogsWithSamePlace(place);
// if not you shoud initialize the dbhandler
Intent intent = new Intent(DisplayLogsByPlace.this, LogsList.class);
intent.putArrayListExtra("sameplaceArray", array);
startActivity(intent);
}
});
public ArrayList<Logs> getAllLogsWithSamePlace(String place) {
ArrayList<Logs> logPlaceList = new ArrayList<Logs>();
String selectQuery = "SELECT * FROM " + TABLE_LOGS + " WHERE " + KEY_PLACE + "=" + place;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
...
}
return logPlaceList;
}
}
答案 1 :(得分:0)
mainListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(DisplayLogsByPlace.this, LogsList.class);
intent.putExtra("keyword","PlaceValue");
startActivity(intent);
}
});
之后在SecondActivity
获得这样的意图值
String place=getIntent().getExtra().getString("keyword");
然后调用dbHandler函数获取数据