从AltertDialog listview中的选定项目中获取价值

时间:2018-03-01 20:00:26

标签: java android android-alertdialog custom-adapter listitem

我有一个包含listview的对话框,其中包含由某些项填充的自定义适配器。每个项目都有一个名称和ID。

这是项目持有者:

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;
}

}

这是自定义适配器:

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;

}

}

此部分放在活动中,我有方法用listview显示alter dialog:

 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());
        }
    });

    }

当警报对话框打开时,我会看到所有这三个项目,但当我点击其中任何一项时,我会收到类似

的错误
  

java.lang.NullPointerException:TOperaterHolder.getIDOperater()&#39;在空对象引用上

这里可能有什么问题?

1 个答案:

答案 0 :(得分:1)

使用

frame.BackStack.RemoveAt(frame.BackStackDepth-1) 

而不是

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

中的