Search code examples
javaandroidandroid-alertdialogcustom-adapterlistitem

Get value from selected item in AltertDialog listview


I have alter dialog that contains listview with custom adapter populated by some items. Each item has a name and id.

This is item holder:

public class TOperaterHolder {

private String NazivOperatera;
private String IDOperater;

public TOperaterHolder(String nazivOperater, String idOperater) {
    this.NazivOperatera = nazivOperater;
    this.IDOperater = idOperater;
}

public String getNazivOperatera() {
    return NazivOperatera;
}

public void setNazivOperatera(String nazivOperatera) {
    NazivOperatera = nazivOperatera;
}

public String getIDOperater() {
    return IDOperater;
}

public void setIDOperater(String IDOperater) {
    this.IDOperater = IDOperater;
}

}

and this is the custom adapter:

public class CustomAdapterTOperateri extends BaseAdapter {

ArrayList<TOperaterHolder> mData;
Context mContext;
LayoutInflater inflater;


public CustomAdapterTOperateri(ArrayList<TOperaterHolder> data, Context context) {
    mData = data;
    mContext = context;
    inflater = LayoutInflater.from(mContext);

}

public class ViewHolder {
    TextView txtNazivTOperater;
}

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

@Override
public TOperaterHolder getItem(int position) {
    return null;
}

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

@Override
public View getView(int position, View view, ViewGroup parent) {

    if (view == null) {
             LayoutInflater mInflater = (LayoutInflater) mContext
                .getSystemService(mContext.LAYOUT_INFLATER_SERVICE);

        view = mInflater.inflate(R.layout.inflate_red, null);
    }

    TextView tvTitle = (TextView) view.findViewById(R.id.txtNazivTOperater);
    View v = (View) view.findViewById(R.id.listViewTOperater);
    tvTitle.setText(mData.get(position).getNazivOperatera());

    return view;

}

}

This part is placed in activity where I have method to show alter dialog with listview:

 private void AlertListItems()
{
    AlertDialog.Builder dialog = new AlertDialog.Builder(IzborRazduzivanje.this);
    dialog.setTitle("List Title");
    View customView = LayoutInflater.from(IzborRazduzivanje.this).inflate(
            R.layout.inflate, null, false);
    ListView listView = (ListView) customView.findViewById(R.id.listViewTOperater);

    ArrayList<TOperaterHolder> itemList = new ArrayList<TOperaterHolder>();
    itemList.add(new TOperaterHolder("J", "1"));
    itemList.add(new TOperaterHolder("B", "2"));
    itemList.add(new TOperaterHolder("C", "3"));


    CustomAdapterTOperateri mAdapter = new CustomAdapterTOperateri(itemList, IzborRazduzivanje.this);
    listView.setAdapter(mAdapter);
    listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    dialog.setView(customView);
    dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub

        }
    });
    dialog.show();

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            TOperaterHolder item = (TOperaterHolder)adapterView.getItemAtPosition(i);
            System.out.println("Selected item: " + item.getIDOperater());
        }
    });

    }

When the alert dialog is open I see all three items but when I click on any of them I get an error like

java.lang.NullPointerException: TOperaterHolder.getIDOperater()' on a null object reference

What could be wrong here?


Solution

  • Use

      TOperaterHolder item = itemList.get(i);
    

    instead of

      TOperaterHolder item = (TOperaterHolder)adapterView.getItemAtPosition(i);
    

    in the onItemClickListener