我的列表视图没有填充,我的适配器有问题吗?

时间:2019-04-17 09:18:54

标签: android android-listview android-adapter

我目前正在尝试从一个表中提取数据,但是将来自不同sql查询的不同结果显示到同一活动的不同listview中。 当前它没有填充列表视图。

当我只有一个listview时它正在填充,但这不是我想要的,因为它仅显示一个sql查询

adapter.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:screenOrientation="portrait"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <TextView
        android:id="@+id/tvName"
        android:layout_width="92dp"
        android:layout_height="30dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        android:layout_weight="1"
        android:text="Name"
        android:textColor="#FEE851"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.016"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.14" />

    <TextView
        android:id="@+id/tvlearningstyle"
        android:layout_width="92dp"
        android:layout_height="30dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        android:layout_weight="1"
        android:text="Name"
        android:textSize="20sp"
        android:textColor="#FEE851"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.055"
        app:layout_constraintStart_toEndOf="@+id/tvName"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.14" />

</android.support.constraint.ConstraintLayout>

Adapterlist

package com.example.jcw3;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;

public class AdapterList extends BaseAdapter {
    ArrayList<String> n,p;
    GroupList l;
    LayoutInflater inflater;
    public AdapterList(GroupList data,ArrayList<String> name,ArrayList<String> learningstyle){
        this.l=data;
        this.n=name;
        this.p=learningstyle;
        this.inflater=LayoutInflater.from(data);
    }

    @Override    public int getCount() {
        return n.size();
    }

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

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

    @Override    public View getView(int i, View view, ViewGroup viewGroup) {
        view=inflater.inflate(R.layout.adapter,null);
        TextView tvname=(TextView)view.findViewById(R.id.tvName);
        TextView tvlearningstyle=(TextView)view.findViewById(R.id.tvlearningstyle);
        tvname.setText(n.get(i));
        tvlearningstyle.setText(p.get(i));
        return view;
    }

}

组列表

package com.example.jcw3;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;

import android.widget.ListView;
import android.view.View;

public class GroupList extends AppCompatActivity {
    ListView listView, listView2, listView3, listView4, listView5;
    SQLiteDatabase db;
    Button clear;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group_list);

        clear = (Button) findViewById(R.id.clear);

        db = openOrCreateDatabase("Mydata", Context.MODE_PRIVATE, null);

        onViewData();
        onViewData2();
        onViewData3();

        clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteAll();
                openStudents();
            }
        });

    }

    public void onViewData() {
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            int groupsize = bundle.getInt("groupsize");
        }

        ArrayList<String> listname = new ArrayList<String>();
        ArrayList<String> listLearningstyle = new ArrayList<String>();
        listname.clear();
        listLearningstyle.clear();
        Cursor c = db.rawQuery("select distinct NAME from id where LEARNINGSTYLE =Visual and LEARNINGSTYLE=VK order by NAME desc, LEARNINGSTYLE desc ", null);
        while (c.moveToNext()) {
            listname.add(c.getString(1));
            listLearningstyle.add(c.getString(2));
        }
        ListView listView = findViewById(R.id.lv1);
        listView.setAdapter(new AdapterList(GroupList.this, listname, listLearningstyle) {
        });
    } //shows the 1st list view

    public void onViewData2() {
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            int groupsize = bundle.getInt("groupsize");
        }

        ArrayList<String> listname = new ArrayList<String>();
        ArrayList<String> listLearningstyle = new ArrayList<String>();
        listname.clear();
        listLearningstyle.clear();
        Cursor c = db.rawQuery("select distinct NAME from id where LEARNINGSTYLE =Kinesthetic and LEARNINGSTYLE=VA order by NAME desc, LEARNINGSTYLE desc  ", null);
        while (c.moveToNext()) {
            listname.add(c.getString(1));
            listLearningstyle.add(c.getString(2));
        }

        ListView listView2 = findViewById(R.id.lv2);
        listView2.setAdapter(new AdapterList(GroupList.this, listname, listLearningstyle) {
        });
    } //shows the 2nd list view

    public void onViewData3() {
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            int groupsize = bundle.getInt("groupsize");
        }

        ArrayList<String> listname = new ArrayList<String>();
        ArrayList<String> listLearningstyle = new ArrayList<String>();
        listname.clear();
        listLearningstyle.clear();
        Cursor c = db.rawQuery("select distinct NAME from id where LEARNINGSTYLE =Aural and LEARNINGSTYLE=VAK order by NAME desc, LEARNINGSTYLE desc ", null);
        while (c.moveToNext()) {
            listname.add(c.getString(1));
            listLearningstyle.add(c.getString(2));
        }
        ListView listView3 = findViewById(R.id.lv3);
        listView3.setAdapter(new AdapterList(GroupList.this, listname, listLearningstyle) {
        });
    } //shows the 3rd list view




    public void deleteAll() {
        db.delete("id", null, null); //delete all rows in a table
    }

    public void openStudents() {
        Intent intent = new Intent(this, Students.class);
        startActivity(intent);
    }


}

第二个适配器列表

package com.example.jcw3;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;

public class AdapterList2 extends BaseAdapter {
    ArrayList<String> n,p;
    GroupList2 l;
    LayoutInflater inflater;
    public AdapterList2(GroupList2 data,ArrayList<String> name,ArrayList<String> learningstyle){
        this.l=data;
        this.n=name;
        this.p=learningstyle;
        this.inflater=LayoutInflater.from(data);
    }

    @Override    public int getCount() {
        return n.size();
    }

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

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

    @Override    public View getView(int i, View view, ViewGroup viewGroup) {
        view=inflater.inflate(R.layout.adapter,null);
        TextView tvname=(TextView)view.findViewById(R.id.tvName);
        TextView tvlearningstyle=(TextView)view.findViewById(R.id.tvlearningstyle);
        tvname.setText(n.get(i));
        tvlearningstyle.setText(p.get(i));
        return view;
    }

}

第二个群组列表

package com.example.jcw3;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;

import android.widget.ListView;
import android.view.View;

public class GroupList2 extends AppCompatActivity {
    ListView listView, listView2, listView3, listView4, listView5;
    SQLiteDatabase db;
    Button clear;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group_list);

        clear = (Button) findViewById(R.id.clear);

        db = openOrCreateDatabase("Mydata", Context.MODE_PRIVATE, null);

        onViewData();
        onViewData2();
        onViewData3();
        onViewData4();

        clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteAll();
                openStudents();
            }
        });

    }

    public void onViewData() {
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            int groupsize = bundle.getInt("groupsize");
        }

        ArrayList<String> listname = new ArrayList<String>();
        ArrayList<String> listLearningstyle = new ArrayList<String>();
        listname.clear();
        listLearningstyle.clear();
        Cursor c = db.rawQuery("select distinct NAME from id where LEARNINGSTYLE =Visual and LEARNINGSTYLE=VA order by NAME desc, LEARNINGSTYLE desc ", null);
        while (c.moveToNext()) {
            listname.add(c.getString(1));
            listLearningstyle.add(c.getString(2));
        }
        ListView listView = findViewById(R.id.lv1);
        listView.setAdapter(new AdapterList2(GroupList2.this, listname, listLearningstyle) {
        });
    } //shows the 1st list view

    public void onViewData2() {
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            int groupsize = bundle.getInt("groupsize");
        }

        ArrayList<String> listname = new ArrayList<String>();
        ArrayList<String> listLearningstyle = new ArrayList<String>();
        listname.clear();
        listLearningstyle.clear();
        Cursor c = db.rawQuery("select distinct NAME from id where LEARNINGSTYLE =Kinesthetic and LEARNINGSTYLE=VK order by NAME desc, LEARNINGSTYLE desc  ", null);
        while (c.moveToNext()) {
            listname.add(c.getString(1));
            listLearningstyle.add(c.getString(2));
        }

        ListView listView2 = findViewById(R.id.lv2);
        listView2.setAdapter(new AdapterList2(GroupList2.this, listname, listLearningstyle) {
        });
    } //shows the 2nd list view

    public void onViewData3() {
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            int groupsize = bundle.getInt("groupsize");
        }

        ArrayList<String> listname = new ArrayList<String>();
        ArrayList<String> listLearningstyle = new ArrayList<String>();
        listname.clear();
        listLearningstyle.clear();
        Cursor c = db.rawQuery("select distinct NAME from id where LEARNINGSTYLE =Aural and LEARNINGSTYLE=VA order by NAME desc, LEARNINGSTYLE desc ", null);
        while (c.moveToNext()) {
            listname.add(c.getString(1));
            listLearningstyle.add(c.getString(2));
        }
        ListView listView3 = findViewById(R.id.lv3);
        listView3.setAdapter(new AdapterList2(GroupList2.this, listname, listLearningstyle) {
        });
    } //shows the 3rd list view

    public void onViewData4() {
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            int groupsize = bundle.getInt("groupsize");
        }

        ArrayList<String> listname = new ArrayList<String>();
        ArrayList<String> listLearningstyle = new ArrayList<String>();
        listname.clear();
        listLearningstyle.clear();
        Cursor c = db.rawQuery("select distinct NAME from id where LEARNINGSTYLE =VA and LEARNINGSTYLE=VK order by NAME desc, LEARNINGSTYLE desc ", null);
        while (c.moveToNext()) {
            listname.add(c.getString(1));
            listLearningstyle.add(c.getString(2));
        }
        ListView listView4 = findViewById(R.id.lv4);
        listView4.setAdapter(new AdapterList2(GroupList2.this, listname, listLearningstyle) {
        });
    } //shows the 4th list view




    public void deleteAll() {
        db.delete("id", null, null); //delete all rows in a table
    }

    public void openStudents() {
        Intent intent = new Intent(this, Students.class);
        startActivity(intent);
    }


}

我希望它运行查询,然后填充到正确的列表视图中, 但是,应该显示结果的活动只是空白。

1 个答案:

答案 0 :(得分:0)

在适配器类中进行以下更改:

String amount=""; 
PayPalPayment payment=new PayPalPayment(new BigDecimal(String.valueOf(amount)));

希望这会有所帮助。