1

I'm creating an app with one Mainactivity and two class. It contains Listview adapter to show list of name with images and details. when click upon any image ...Alert Dialog Box will pop up and from option ,i want user could able to call or send email.

Earlier I used the code for Mainactivity class and it pretty much works but with ListviewAdapter it crashes.

Here is my Class

 
public class ListViewAdapter extends BaseAdapter {

 // Declare Variables
 Context mContext;
 LayoutInflater inflater;
 private List<WorldPopulation> worldpopulationlist = null;
 private ArrayList<WorldPopulation> arraylist;

 public ListViewAdapter(Context context,
   List<WorldPopulation> worldpopulationlist) {
  mContext = context;
  this.worldpopulationlist = worldpopulationlist;
  inflater = LayoutInflater.from(mContext);
  this.arraylist = new ArrayList<WorldPopulation>();
  this.arraylist.addAll(worldpopulationlist);
 }

 public class ViewHolder {
  TextView rank;
  TextView country;
  TextView population;
  ImageView flag;
 }

 @Override
 public int getCount() {
  return worldpopulationlist.size();
 }

 @Override
 public WorldPopulation getItem(int position) {
  return worldpopulationlist.get(position);
 }

 @Override
 public long getItemId(int position) {
  return position;
 }

 public View getView(final int position, View view, ViewGroup parent) {
  final ViewHolder holder;
  if (view == null) {
   holder = new ViewHolder();
   view = inflater.inflate(R.layout.listview_item, null);
   // Locate the TextViews in listview_item.xml
   holder.rank = (TextView) view.findViewById(R.id.rank);
   holder.country = (TextView) view.findViewById(R.id.country);
   holder.population = (TextView) view.findViewById(R.id.population);
   // Locate the ImageView in listview_item.xml
   holder.flag = (ImageView) view.findViewById(R.id.flag);
   view.setTag(holder);
  } else {
   holder = (ViewHolder) view.getTag();
  }
  // Set the results into TextViews
  holder.rank.setText(worldpopulationlist.get(position).getRank());
  holder.country.setText(worldpopulationlist.get(position).getCountry());
  holder.population.setText(worldpopulationlist.get(position)
    .getPopulation());
  // Set the results into ImageView
  holder.flag.setImageResource(worldpopulationlist.get(position)
    .getFlag());

  final String email = worldpopulationlist.get(position).getRank().toString();
  final String phone = worldpopulationlist.get(position).getCountry().toString();
  // Listen for ListView Item Click
  final View finalView = view;
  view.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View arg0) {
    CharSequence options[] = new CharSequence[]{"Email", "Call"};

    final AlertDialog.Builder builder = new AlertDialog.Builder(mContext);

    builder.setTitle("Select Options");
    builder.setItems(options, new DialogInterface.OnClickListener() {
     @Override
     public void onClick(DialogInterface dialogInterface, int i) {

      //Click Event for each item.
      if(i == 0){

       Intent intent = new Intent(Intent.ACTION_SEND);
       intent.setType("plain/text");
       intent.putExtra(Intent.EXTRA_EMAIL, new String[] { email });
       intent.putExtra(Intent.EXTRA_SUBJECT, "subject");
       intent.putExtra(Intent.EXTRA_TEXT, "mail body");
       mContext.startActivity(Intent.createChooser(intent, ""));

      }

      if(i == 1){
       Intent callIntent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"+phone));
       // callIntent.setData(Uri.parse("tel:"+uri));
       callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
       mContext.startActivity(callIntent);

      }

     }
    });

    builder.show();
   }
  });

  return view;
 }

 // Filter Class
 public void filter(String charText) {
  charText = charText.toLowerCase(Locale.getDefault());
  worldpopulationlist.clear();
  if (charText.length() == 0) {
   worldpopulationlist.addAll(arraylist);
  } else {
   for (WorldPopulation wp : arraylist) {
    if (wp.getCountry().toLowerCase(Locale.getDefault())
      .contains(charText)) {
     worldpopulationlist.add(wp);
    }
   }
  }
  notifyDataSetChanged();
 }

}

and error log is

    Process: com.nepalpolice.test, PID: 30475
    android.content.res.Resources$NotFoundException: Resource ID #0x0
        at android.content.res.Resources.getValue(Resources.java:1123)
        at android.content.res.Resources.loadXmlResourceParser(Resources.java:2309)
        at android.content.res.Resources.getLayout(Resources.java:939)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:395)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
        at android.support.v7.app.AlertController$AlertParams.createListView(AlertController.java:988)
        at android.support.v7.app.AlertController$AlertParams.apply(AlertController.java:964)
        at android.support.v7.app.AlertDialog$Builder.create(AlertDialog.java:981)
        at android.support.v7.app.AlertDialog$Builder.show(AlertDialog.java:1005)
        at com.nepalpolice.test.ListViewAdapter$1.onClick(ListViewAdapter.java:125)
        at android.view.View.performClick(View.java:4438)
        at android.view.View$PerformClick.run(View.java:18422)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5018)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)
Please help. Thanks in advance.
Bir Nepali
  • 429
  • 1
  • 8
  • 20

1 Answers1

4

Android has its normal classes, like AlertDialog, and then it has support versions of some of those classes, one being AlertDialog. This is to provide backwards compatibility with older Android versions that may not have these classes natively. However it can get confusing sometimes.

The tutorial you linked is using an Activity and the normal AlertDialog. However, you are using an Activity and the support AlertDialog. This won't work. AlertDialog depends on a theme or style to know what it should make itself look like. Since you're using a normal Activity, the support AlertDialog is unable to infer what it should look like because Activity doesn't use support package themes.

There are a few things you can do to fix this.

The simplest way to fix this would be to just change from using the support AlertDialog to using the native one. Delete import android.support.v7.app.AlertDialog; from your Adapter class and then reimport the AlertDialog, making sure you don't import the support version again.

The second simplest way would be to follow this answer and add a specific AppCompat theme for your AlertDialog. However, this won't fix any future issues you may run into by mixing AppCompat and normal classes.

The most involved (but also most compatible) way of fixing this is to convert to using AppCompatActivity. You'll have to change your Activity to extend AppCompatActivity instead, and also change your theme (in styles.xml) to have a parent of Theme.AppCompat.Light (remove .Light if you want a dark theme).

TheWanderer
  • 16,775
  • 6
  • 49
  • 63
  • wow such a details explanation...hasn't tested yet but totally get it. I'll tell you if it works or not. Thanks again. – Bir Nepali Sep 06 '18 at 21:59