gpt4 book ai didi

android - 空列表框显示在 ListView 中

转载 作者:行者123 更新时间:2023-11-30 05:04:43 26 4
gpt4 key购买 nike

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

enter image description here

我不知道错误出在自定义适配器或 ListView 中。几天来我一直在寻找解决方案,但找不到。请协助我完成项目。谢谢。下面我将通过我的自定义适配器代码和 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>

请帮我看看哪里出错了。谢谢。

最佳答案

尝试改变:

        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。希望对您有所帮助!

关于android - 空列表框显示在 ListView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54761381/

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