我的列表视图没有填充,我的适配器有问题吗?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我的列表视图没有填充,我的适配器有问题吗?相关的知识,希望对你有一定的参考价值。
我目前正试图从一个表中提取,但是将不同sql查询的不同结果显示在同一活动的不同列表视图中。目前它没有填充列表视图。
当我只有一个列表视图时它会填充,但它不是我想要的,因为它只显示一个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;
}
}
GROUPLIST
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);
}
}
我希望它运行查询,然后填充到正确的列表视图,然而会发生什么,应该显示结果的活动只是空白。
答案
在适配器类中进行以下更改:
@Override public Object getItem(int i) {
return n.get(i);
}
@Override public long getItemId(int i) {
return i;
}
@Override public View getView(int i, View view, ViewGroup viewGroup) {
view=inflater.inflate(R.layout.adapter,viewGroup,false);
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;
}
希望它会有所帮助。
以上是关于我的列表视图没有填充,我的适配器有问题吗?的主要内容,如果未能解决你的问题,请参考以下文章