我为列表视图项创建了一个构造函数DataPembeli
,然后创建自定义列表视图适配器DataPembeliListAdapter
并使用像这样的构造函数:
txtid.setText(dataPembeliItems.get(position).getId()); // THIS IS THE ERROR LINE
txtket1.setText(dataPembeliItems.get(position).getNama());
txtket2.setText(dataPembeliItems.get(position).getAlamat());
txtket3.setText(dataPembeliItems.get(position).getNohp());
在DataPembeliFragment
课程中,我使用过它:
public class DataPembeliFragment extends Fragment {
private ListView listPembeli;
private ImageButton btnTambah;
// slide data items
private int[] idDataPembeli;
private String[] namaDataPembeli, alamatDataPembeli, noHpDataPembeli;
private ArrayList<DataPembeli> dataPembeliItems;
private DataPembeliListAdapter adapter;
public DataPembeliFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_data_pembeli, container, false);
listPembeli = (ListView) rootView.findViewById(R.id.list_sliderdata);
btnTambah = (ImageButton) rootView.findViewById(R.id.btnTambah);
btnTambah.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
showEditDialog();
}
});
dataPembeliItems = new ArrayList<DataPembeli>();
idDataPembeli = new int[10];
namaDataPembeli = new String[10];
alamatDataPembeli = new String[10];
noHpDataPembeli = new String[10];
idDataPembeli[0] = 1;
namaDataPembeli[0] = "ryno";
alamatDataPembeli[0]= "padang";
noHpDataPembeli[0] = "0812874512";
dataPembeliItems.add(new DataPembeli(idDataPembeli[0], namaDataPembeli[0], alamatDataPembeli[0], noHpDataPembeli[0]));
idDataPembeli[1] = 2;
namaDataPembeli[1] = "yezu";
alamatDataPembeli[1]= "padang";
noHpDataPembeli[1] = "0819232211";
dataPembeliItems.add(new DataPembeli(idDataPembeli[1], namaDataPembeli[1], alamatDataPembeli[1], noHpDataPembeli[1]));
idDataPembeli[2] = 3;
namaDataPembeli[2] = "test";
alamatDataPembeli[2]= "padangs";
noHpDataPembeli[2] = "0819232xx";
dataPembeliItems.add(new DataPembeli(idDataPembeli[2], namaDataPembeli[2], alamatDataPembeli[2], noHpDataPembeli[2]));
listPembeli.setOnItemClickListener(new SlideMenuClickListener());
adapter = new DataPembeliListAdapter(getActivity().getApplicationContext(),dataPembeliItems);
listPembeli.setAdapter(adapter);
return rootView;
}
}
当我调用片段时,我在上面标记的行上的DataPembeliAdapter的logcat中得到ResourceNotFoundException
。(在评论“//这是错误行”)
答案 0 :(得分:1)
setText
方法需要显示String
参数,但是您放置int
值,因此他尝试从资源中获取字符串。您只需要txtid.setText(Integer.toString(dataPembeliItems.get(position).getId()))
。