gpt4 book ai didi

java - 从 AltertDialog ListView 中的所选项目获取值

转载 作者:行者123 更新时间:2023-12-02 11:34:25 25 4
gpt4 key购买 nike

我有一个更改对话框,其中包含带有由某些项目填充的自定义适配器的 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;

}

}

这部分放置在 Activity 中,我可以在其中使用 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());
}
});

}

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

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

这里可能出了什么问题?

最佳答案

使用

  TOperaterHolder item = itemList.get(i);

而不是

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

onItemClickListener

关于java - 从 AltertDialog ListView 中的所选项目获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49057759/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com