0

数据如何在屏幕上显示 结果 在此处输入图像描述

我希望这些数据在屏幕上显示为列表视图。我有一些与 Java 中的列表视图相关的代码,并且 xml 文件上还有列表视图和文本视图的代码。我尝试将结果显示为列表视图,但没有成功。

先感谢您!:)

(已编辑 - 我已经在 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;
    }
}


 }
4

1 回答 1

1

首先将 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);

您现在可以运行您的代码。

于 2018-09-23T13:55:18.567 回答