【问题标题】:Why List view is not getting printed in the Activity?为什么列表视图没有在活动中打印?
【发布时间】:2022-10-15 01:01:32
【问题描述】:

我已经创建了我使用 SQLite UserDB 数据库的应用程序,并且我已经成功地将数据插入到用户表中,现在我需要在第二个活动中打印该数据,因此我在第一页上调用意图并使用 onclick 侦听器我要去下一个我创建的活动和这样的 XML 页面:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DisplayData">
<ListView
    android:id="@+id/listview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="0dp"/>
Now Using ArrayAdpater I need to print every user Data in to this Activity thus I created list_item.xml file for custom array adapter
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/namelist"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Mustafa Dahodwala"
        android:textSize="20sp"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"/>
    <TextView
        android:id="@+id/emaillist"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="mustafamandviwala45@gmail.com"
        android:textSize="18sp"
        android:layout_marginLeft="15dp"/>

</LinearLayout>

同样在 DisplayData.java 文件中,我调用了适配器,它帮助我遍历列表:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_data);

        MyDBHelper db=new MyDBHelper(this);
        ArrayList<DataModel> list=db.fetchuser();

        try {

            UserAdapter adapter = new UserAdapter(this, R.layout.list_item, list);

            ListView listView = findViewById(R.id.listview);

            listView.setAdapter(adapter);

        }catch (Exception e){
            Log.e("MyError",e.toString());
        }



}

UserAdapter 类用于执行此操作,我在这里调用了

public class UserAdapter extends ArrayAdapter<DataModel> {
private ArrayList<DataModel> list;
public UserAdapter(@NonNull Context context, int resource, ArrayList<DataModel> list) {
    super(context, resource);
    this.list=list;
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

    if (convertView==null){
        convertView=LayoutInflater.from(getContext()).inflate(R.layout.list_item,parent,false);

    }
    TextView name=convertView.findViewById(R.id.namelist);
    TextView email=convertView.findViewById(R.id.emaillist);

    name.setText(list.get(position).name);
    email.setText(list.get(position).number);
    return convertView;
    }
    }

但是当我运行这个应用程序时,它没有显示任何错误,但也没有在其中显示数据我什至检查我的 ArrayList 是否正确地从数据库中获取数据但是是的它正在正确地完成它的工作我不知道我哪里出错了请帮我解决一下这个。

【问题讨论】:

    标签: java android android-adapter


    【解决方案1】:

    嗨 Mustafa 首先,这段代码真的很旧,并且在主线程上工作,这很糟糕我建议您使用 Room 和 Recycle 视图它们可以很好地协同工作,因为您需要在构造函数中使用将数组数据传递给 ArrayAdapter super(context, 0, list);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-27
      • 2022-01-07
      • 2023-04-01
      • 1970-01-01
      相关资源
      最近更新 更多