java - 如何将下面显示的结果转换为 ListView?

标签 java android listview

数据如何在屏幕上呈现 Result enter image description here

我希望这些数据在屏幕上以 ListView 的形式呈现。我有一些与Java中的 ListView 相关的代码,并且xml文件上还有 ListView 和 TextView 的代码。我尝试将结果显示为 ListView ,但没有成功。

提前谢谢您! :)

(已编辑 - 我已经在 Android Studio 中编写了 customlayout.xml 和 Activitymain.xml 的代码,完全按照您给出的那样)

 public class Viewstudent extends AppCompatActivity {

DatabaseManager myDb;
EditText sid, fn, ln, ge, cs, ag, ad;

ArrayList<String> student_id = new ArrayList<>();
ArrayList<String> student_name = new ArrayList<>();
ArrayList<String> student_lastname = new ArrayList<>();



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main4);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle(null);

    myDb = new DatabaseManager(this);



    sid = (EditText)findViewById(R.id.sid);
    fn = (EditText)findViewById(R.id.fn);
    ln = (EditText)findViewById(R.id.ln);
    ge = (EditText)findViewById(R.id.ge);
    cs = (EditText)findViewById(R.id.cs);
    ag = (EditText)findViewById(R.id.ag);
    ad = (EditText)findViewById(R.id.ad);

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

    CustomAdapter customAdapter = new CustomAdapter();
    listView.setAdapter(customAdapter);


    Cursor res = myDb.getAllDataStudent();
    while(res.moveToNext()){
        student_id.add(res.getString(0));
        student_name.add(res.getString(1));
        student_lastname.add(res.getString(2));
    }

}

class CustomAdapter extends BaseAdapter {

    @Override
    public int getCount() {
        return student_id.size();//return count of array
    }

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

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //Set the general style
        convertView = getLayoutInflater().inflate(R.layout.customlayout,null);
        TextView id = (TextView)convertView.findViewById(R.id.sid);
        TextView name = (TextView)convertView.findViewById(R.id.fn);
        TextView lastname = (TextView)convertView.findViewById(R.id.ln);

        id.setText(student_id.get(position));
        name.setText(student_name.get(position));
        lastname.setText(student_lastname.get(position));

        return convertView;
    }
}


 }

最佳答案

首先将ListView放入activity_main.xml文件中。

    <ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/listView"></ListView>

然后在MainActivity.java文件中。

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

将所有变量声明为数组列表。

    ArrayList<String> student_id = new Arraylist<>();
    ArrayList<String> student_name = new Arraylist<>();
    ArrayList<String> student_rollno = new Arraylist<>(); 

然后在 onCreate() 方法上

    Cursor res = myDb.getAllDataStudent();
     while(res.moveToNext()){
    student_id.add(res.getString(0));
    student_name.add(res.getString(1));
    student_rollno.add(res.getString(2));
    }

在布局文件夹中创建一个新的布局文件customlayout.xml。 在此文件中创建所需的 TextView,例如。请记住,样式和位置将成为您的 ListView 的通用

    <TextView
    android:id="@+id/sid"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView"/>
<TextView
    android:id="@+id/name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView"/>
<TextView
    android:id="@+id/rollno"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView"/>

在MainActivity.java中创建CustomAdapter类

    class CustomAdaper extends BaseAdapter {

    @Override
    public int getCount() {
        return student_id.size();//return count of array
    }

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

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //Set the general style
        convertView = getLayoutInflater().inflate(R.customlayout,null);
        TextView id = (TextView)convertView.findViewById(R.id.sid);
        TextView name = (TextView)convertView.findViewById(R.id.name);
        TextView rollno = (TextView)convertView.findViewById(R.id.rollno);

        id.setText(student_id.get(position));
        name.setText(student_name.get(position));
        rollno.setText(student_rollno.get(position));

        return convertView;
    }
}

现在在下面的onCreate方法中

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

添加以下代码。

    CustomAdaper customAdaper = new CustomAdaper();
    lisview.setAdapter(customAdapter);

您现在可以运行代码。

关于java - 如何将下面显示的结果转换为 ListView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52465946/

相关文章:

java - Proguard 删除 Android 应用程序中的注释

java - 格式化 Java for 循环

java - 内联网应用程序的签名小程序

java - Android抽屉导航点击事件问题

JavaFx ListView CellLayout 缩放

java - 如何在Android Studio中对 ListView 中的数据进行排序

javascript - 样式化 jQuery 移动 ListView

java - 如何在 Spring Java 配置中创建 Jcache?

java - 使用 Intent 导航后重新加载 MainActivity

java - zxing onActivityResults 未触发