#android #android-listview #android-adapter
#Android #android-listview #android-адаптер
Вопрос:
В настоящее время я пытаюсь извлечь данные из одной таблицы, но отображаю разные результаты из разных 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>
Список адаптеров
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);
}
}
2nd 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 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;
}
}
2-й список групп
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:
Выполните следующие изменения в своем классе адаптера:
@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;
}
Надеюсь, это поможет.
Комментарии:
1. К сожалению, я получил тот же результат, в listview ничего не отображается. Может быть, потому, что я использую несколько адаптеров для извлечения из одной таблицы?