0

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.

screenshot

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;
}
}
user1282637
  • 1,827
  • 5
  • 27
  • 56
  • Any reason you can't set it up in a way that you pass the dialog into the adapter, like adapter = new LocationAdapter(this, arrayOfLocations,dialog); – Shadesblade Jul 17 '14 at 18:41
  • Have you tried just starting the activity from the onClickListener and giving it the data you want to display? You can dismiss the dialog afterwards. With the proper activity flags I believe this would work. Are you using a fragment for your dialog? – RoraΖ Jul 17 '14 at 18:43
  • Hm...will try that. Is it possible to then control the same dialog box from within the Adapter class? – user1282637 Jul 17 '14 at 18:43
  • I am not using a fragment for the dialog – user1282637 Jul 17 '14 at 18:44
  • Ok. I've used DialogFragments before, and used the second answer (68 votes) to share data between fragments and activities. It's very easy to implement and fairly scalable. http://stackoverflow.com/questions/9343241/passing-data-between-a-fragment-and-its-container-activity – RoraΖ Jul 17 '14 at 18:51

0 Answers0