我有空列表,即使其中有一些项目。可能是我没有把它放在正确的位置。
我在此声明:
public class MainActivity extends Activity implements OnClickListener {
....
ArrayList<Pers_Ordre> ListOrdres;
....
然后onCreate:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
ListOrdres = new ArrayList<Pers_Ordre>();
....
我有按钮扫描扫描并将其放入我的列表和按钮voir以查看列表。
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_Scan:
{
IntentIntegrator scanIntegrator = new IntentIntegrator(this);
scanIntegrator.initiateScan();
}
break;
case R.id.btn_Signe:
{
Intent intent = new Intent(this, CaptureSignature.class);
intent.putExtra(The_CodeDesti, DestiCode);
startActivityForResult(intent,SIGNATURE_ACTIVITY);
}
break;
case R.id.btn_Voir:
{
DateTxt.setText(LedateFormat.format(Ledate));
GPSTxt.setText(TempAddr);
m_adapter = new CustomListAdapter(this, ListOrdres);
lv_Ordre.setAdapter(m_adapter);
然后在每次扫描后,我将数据放入我的列表,当我调试它时,数据被放入我的列表中
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
//retrieve scan result
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanningResult != null) {
//we have a result
String scanContent = scanningResult.getContents();
if (scanContent != null)
{
Pers_Ordre OrdreItem = new Pers_Ordre();
String[] TempOrdre = scanContent.split("_");
OrdreItem.setLe_CodeClient(TempOrdre[0]);
OrdreItem.setLe_CodeDest(TempOrdre[1]);
OrdreItem.setLe_NoOrdre(TempOrdre[2]);
OrdreItem.setLe_Ordre_1(TempOrdre[3]);
OrdreItem.setLe_Ordre_2(TempOrdre[4]);
OrdreItem.setLe_Date(LedateFormat.format(Ledate));
OrdreItem.setLe_GPS(TempAddr);
ListOrdres.add(OrdreItem);
但是当我点击voir查看我的列表时,我得到了空列表。
我的CustomListAdapter:
public class CustomListAdapter extends BaseAdapter{
Context mContext;
private ArrayList<Pers_Ordre> Pers_Ordre_arraylist;
private LayoutInflater layoutInflater;
public CustomListAdapter(Context context,ArrayList<Pers_Ordre> Client_Pers_list) {
mContext = context;
layoutInflater = LayoutInflater.from(mContext);
this.Pers_Ordre_arraylist = Client_Pers_list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return Pers_Ordre_arraylist.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return Pers_Ordre_arraylist.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.list_row_layout, null);
holder = new ViewHolder();
holder.NoOrdreView = (TextView) convertView.findViewById(R.id.txt_NoOrdre);
holder.Ordre1View = (TextView) convertView.findViewById(R.id.txt_Ordre1);
holder.Ordre2View = (TextView) convertView.findViewById(R.id.txt_Ordre2);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Pers_Ordre OrdreItem = (Pers_Ordre)Pers_Ordre_arraylist.get(position);
holder.NoOrdreView.setText("No Ordre: "+OrdreItem.getLe_NoOrdre());
holder.Ordre1View.setText("Ordre "+ OrdreItem.getLe_Ordre_1());
holder.Ordre2View.setText("sur " +OrdreItem.getLe_Ordre_2());
return convertView;
}
static class ViewHolder {
TextView NoOrdreView;
TextView Ordre1View;
TextView Ordre2View;
}
更新: 当我把m_adapter.notifyDataSetChanged();在ListOrdres.add(OrdreItem)之后;它崩溃了程序 但是当我把m_adapter.notifyDataSetChanged();
case R.id.btn_Voir:
{
DateTxt.setText(LedateFormat.format(Ledate));
GPSTxt.setText(TempAddr);
m_adapter = new CustomListAdapter(this, ListOrdres);
m_adapter.notifyDataSetChanged();
lv_Ordre.setAdapter(m_adapter);
它工作,但现在我有另一个问题,我可以扫描双项目,我想在我的列表上的所有项目独特
答案 0 :(得分:1)
请提供CustomListAdapter的代码,以便我了解您如何实现它。在从适配器添加或删除数据后,必须将notifyDataSetChanged()调用到已更改数据集的notity Adapter。