Currently, I am using a list view with 4 columns using an adapter within the code. When I click an item here, I want to look up only some columns of the items that were clicked, so is there a way?
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.city_list);
db = new DBHelper(this);
list = (ListView)findViewById(R.id.city_list);
db.getListContents();
final ArrayList<InfoClass> weatherList = new ArrayList<InfoClass>();
Cursor data = db.getListContents();
if(data.getCount() == 0){
Toast.makeText(this, "There are no contents in this list!",Toast.LENGTH_LONG).show();
}else{
while(data.moveToNext()){
InfoClass newInfo = new InfoClass(data.getString(1), data.getString(2), data.getString(3), data.getString(4));
weatherList.add(newInfo);
DBAdapter dbAdapter = new DBAdapter(this, weatherList);
list.setAdapter(dbAdapter);
}
}
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
/* /* TextView item_latitude = (TextView)findViewById(R.id.item_latitude);
TextView item_longitude = (TextView)findViewById(R.id.item_longitude);
data_longitude = item_latitude.getText().toString();
data_latitude = item_longitude.getText().toString();*/
Intent intent = new Intent(CityList.this, MainActivity.class);
intent.putExtra("data_latitude", data_latitude);
intent.putExtra("data_longitude", data_longitude);
setResult(Activity.RESULT_OK, intent);
finish();
}
});
}
First, you must register listener(OnItemClickListener)
to detect one item being clicked in the ListView.
You can create a listener as an anonymous class, or you can create an object and register it.
If you implement it as an anonymous class, you can do it as follows.
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Position is the clicked position.
// You can take it out of the collection and use it properly.
Toast.makeText(getApplicationContext(), itemList.get(position).getSomethingColumn(), Toast.LENGTH_LONG).show();
// Additional parts
InfoClass selectedInfoClass = weatherList.get(position);
selectedInfoClass.getXXX();
}
});
© 2024 OneMinuteCode. All rights reserved.