Right now I have a dialog box displaying a listview of "Locations" through a custom adapter. Now, when a person clicks "More Info" I want to populate that information on the main activity. So, what I need to do is, on button click -> exit dialog box and display that specific location's data. My question is: how can I exit the dialog box from inside my LocationAdapter class? Basically, I want to do this
fullListing.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
textViewName.setText(location.name);
}
});
But the dialog and textview are in a different activity from the Location Adapter. Any way I can do this? Thanks.
Main Activity
public class MainActivity extends Activity {
LocationAdapter adapter;
ArrayList<Location> arrayOfLocations;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView myTV = (TextView) findViewById(R.id.textView1);
final Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Does it already exist?");
final Location myLocation = new Location(MainActivity.this, 1, null, "Place Title", "Place Description", null, null, null);
arrayOfLocations = new ArrayList<Location>();
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
adapter = new LocationAdapter(this, arrayOfLocations);
adapter.add(new Location(MainActivity.this, 1, bm, "Title 1", "Description 1", "1", "1", "1"));
adapter.add(new Location(MainActivity.this, 1, bm, "Title 2", "Description 2", "2", "2", "2"));
adapter.add(new Location(MainActivity.this, 1, bm, "Title 3", "Description 3", "3", "3", "3"));
adapter.add(new Location(MainActivity.this, 1, bm, "Title 4", "Description 4", "4", "4", "4"));
final ListView listView = (ListView) dialog.findViewById(R.id.listView2);
listView.setAdapter(adapter);
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myTV.setText(myLocation.name);
dialog.dismiss();
}
});
dialog.show();
}
}
Location Adapter Class
public class LocationAdapter extends ArrayAdapter<Location> {
public LocationAdapter(Context context, ArrayList<Location> locations) {
super(context, R.layout.item_location, locations);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
final Location location = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(
R.layout.item_location, parent, false);
}
// Lookup view for data population
TextView tvName = (TextView) convertView.findViewById(R.id.tvName);
TextView tvDetails = (TextView) convertView
.findViewById(R.id.tvDetails);
TextView tvDistance = (TextView) convertView
.findViewById(R.id.tvDistance);
TextView tvHours = (TextView) convertView.findViewById(R.id.tvHours);
TextView tvUpdatedTime = (TextView) convertView
.findViewById(R.id.tvUpdatedTime);
ImageView ivIcon = (ImageView) convertView.findViewById(R.id.imgIcon);
Button fullListing = (Button) convertView
.findViewById(R.id.fullListing);
fullListing.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
}
});
// Populate the data into the template view using the data object
tvName.setText(location.name);
tvDetails.setText(location.details);
tvDistance.setText(location.distance);
tvHours.setText(location.hours);
tvUpdatedTime.setText(location.updatedTime);
ivIcon.setImageBitmap(location.icon);
// Return the completed view to render on screen
return convertView;
}
}