空列表框显示在列表视图中

时间:2019-02-19 08:05:42

标签: android listview

我在我的android项目中创建了一个列表视图,它在ListView的开头显示了一个空列表。从数据库中准确显示所有数据。但它会在列表视图的顶部创建一个空列表。例如,如果我们从数据库中获取一条记录,则会显示两条记录。如此处图片所示,第一条记录为空

enter image description here

我不知道故障出在自定义适配器或列表视图中。几天我一直在寻找解决方案,但找不到。请协助我完成项目。谢谢。 下面,我将过去我的自定义适配器代码和ListView

自定义适配器

else {      
LISTVIEW = (ListView) findViewById(R.id.listView1);

customAdapter = new CustomAdapter();
LISTVIEW.setAdapter(customAdapter);
LISTVIEW.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
LISTVIEW.setStackFromBottom(true);

}


static class ViewHolder {
    TextView ord_num;
    TextView cus_name;
    TextView cus_tel;
    TextView ord_status;
}

class CustomAdapter extends BaseAdapter {

    @Override
    public int getCount() {
        return ID_ArrayList.size() + 1;
    }


    @Override
    public int getViewTypeCount() {
        return getCount();
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }


    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return -1;
    }


  @Override
  public View getView(int i, View view, ViewGroup viewGroup) {


        ViewHolder holder = null;
        if (view == null) {

            view = getLayoutInflater().inflate(R.layout.listtemplate, viewGroup,false);

            holder = new ViewHolder();
            holder.ord_num = (TextView) findViewById(R.id.lblID);
            holder.cus_name = (TextView) findViewById(R.id.cus_name);
            holder.cus_tel = (TextView) findViewById(R.id.cus_tel);
            holder.ord_status = (TextView) findViewById(R.id.ord_status);
            view.setTag(holder);

        } else {
            holder = (ViewHolder) view.getTag();

        }


        try {

            holder.ord_num.setText("Order ID :" + ID_ArrayList.get(i));
            holder.cus_name.setText("Name : " + NAME_ArrayList.get(i));
            holder.cus_tel.setText("Telephone No : " + PHONE_NUMBER_ArrayList.get(i));
            holder.ord_status.setText("Status : " + STATUS_ArrayList.get(i));

        } catch (Exception ex) {
            System.out.println(ex);
        }

        final int a=i;

        view.findViewById(R.id.item_info).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, View_Items.class);

                String x = getItem(a).toString();
                String y = ID_ArrayList.get(Integer.parseInt(x)-1).toString();


                AlertDialog.Builder alt = new AlertDialog.Builder(MainActivity.this)
                        .setTitle("HI")
                        .setMessage(y);

                alt.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {


                    }
                });
                alt.show();

            }
        });

        return (view != null) ? view : null;

这是我的ListView

android:layout_width="match_parent"
android:layout_height="match_parent"

tools:context="com.a_hamoud.listview_sqlserver.MainActivity"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main">

<ListView

    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="8"

    >

</ListView>

请帮助我找到错误所在。 谢谢。

4 个答案:

答案 0 :(得分:0)

在设置文本之前,您必须检查一个编码,该编码在列表中的第一个索引处为空。

if (ID_ArrayList.get(i) != null){
holder.ord_num.setText("Order ID :" + ID_ArrayList.get(i));
        holder.cus_name.setText("Name : " + NAME_ArrayList.get(i));
        holder.cus_tel.setText("Telephone No : " + PHONE_NUMBER_ArrayList.get(i));
        holder.ord_status.setText("Status : " + STATUS_ArrayList.get(i));

}

 i dont know which field is null show you can check.

答案 1 :(得分:0)

适配器认为它的项目比实际多。删除getCount()中的+1:

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

答案 2 :(得分:0)

只需使用此方法更新您的getCount方法即可

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

答案 3 :(得分:0)

尝试更改:

        holder.ord_num = (TextView) findViewById(R.id.lblID);
        holder.cus_name = (TextView) findViewById(R.id.cus_name);
        holder.cus_tel = (TextView) findViewById(R.id.cus_tel);
        holder.ord_status = (TextView) findViewById(R.id.ord_status);

收件人:

        holder.ord_num = (TextView) view.findViewById(R.id.lblID);
        holder.cus_name = (TextView) view.findViewById(R.id.cus_name);
        holder.cus_tel = (TextView) view.findViewById(R.id.cus_tel);
        holder.ord_status = (TextView) view.findViewById(R.id.ord_status);

还要删除getCount()中的+1。希望有帮助!