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

时间:2018-09-23 12:06:32

标签: java android listview

如何在屏幕上显示数据 Result enter image description here

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

先谢谢您! :)

(编辑-我完全按照您的要求在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;
    }
}


 }

1 个答案:

答案 0 :(得分: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);

您现在可以运行代码。

相关问题