我从列表中的sqlite获取数据,将它们显示在片段的ListView中。我首先在布局中创建了ListView,因为Android Studio没有根据id找到我的ListView,所以它没有工作。然后我以编程方式创建了ListView,它什么也没显示。奇怪的是,在这里输入另一个片段,我做了同样的事情,一切都很完美。
我的问题是:你们可以检查我的代码是否有错误或告诉我那里出了什么问题? 到目前为止,我已经尝试过我所知道的所有新手,但它并不起作用。任何帮助,将不胜感激。查找与此程序相关的编码附件。我正在使用片段导航抽屉...
主要片段
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.util.ArrayList;
import cm.opep.opep.DatabaseHelper;
import cm.opep.opep.R;
import cm.opep.opep.transfert.DbResponse;
import cm.opep.opep.transfert.DbTrajet;
import cm.opep.opep.transfert.PassagerAdapter;
public class DetailTrajetFragment extends Fragment {
DatabaseHelper helper;
PassagerAdapter ListAdapter;
String et_iduser, et_idchauffeur, et_idtrajet, et_placesvoulues, et_rechercheDateD, et_rechercheHeureH, et_rechercheVilleA, et_rechercheVilleD;
ArrayList<String> prenom_passager_arrayList = new ArrayList<>();
ArrayList<String> age_passager_arrayList = new ArrayList<>();
ArrayList<String> niveau_passager_arrayList = new ArrayList<>();
ArrayList<String> id_passager_arrayList = new ArrayList<>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_detail_trajet, container, false);
helper = new DatabaseHelper(getActivity());
getActivity().setTitle(String.valueOf("ID Trajet: " + getArguments().getString("idtrajet")));
initUI(v);
return v;
}
public void initUI(View v) {
final TextView cptevilledepart = v.findViewById(R.id.propositionvilledepart);
final TextView cptelieudepart = v.findViewById(R.id.cpte_lieudepart);
final TextView cptedatedepart = v.findViewById(R.id.propositionDateD);
final TextView cptedatearrivee = v.findViewById(R.id.cpte_date_arrivee);
final TextView cpteheuredepart = v.findViewById(R.id.propositionHeureD);
final TextView cptetaillebagage = v.findViewById(R.id.propositionbagage);
final TextView cpteflexibilite = v.findViewById(R.id.flexibilite);
final TextView cptedetour = v.findViewById(R.id.detour);
final TextView cpteprix = v.findViewById(R.id.cpte_prix);
final TextView cptecommentairechauffeur = v.findViewById(R.id.cpte_commentaire_chauffeur);
final TextView cpteplacesdisponibles = v.findViewById(R.id.cpte_places_disponibles);
final TextView cptevillearrivee = v.findViewById(R.id.propositionvillearrivee);
final TextView cptelieudepot = v.findViewById(R.id.cpte_lieu_depot);
final TextView cpteprenom = v.findViewById(R.id.prenom_chauffeur);
final TextView cpteage = v.findViewById(R.id.age_chauffeur);
final TextView cpteniveau = v.findViewById(R.id.niveau_chauffeur);
Button bouton_reserver = v.findViewById(R.id.bouton_réserver);
final TextView vide = v.findViewById(R.id.vide);
ListView liste_passag = new ListView(this.getActivity());
int i = 1226;
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, i, 0, 0);
liste_passag.setLayoutParams(layoutParams);
layoutParams.setMargins(0, i + 75, 0, 0);
vide.setLayoutParams(layoutParams);
et_iduser = getArguments().getString("id");
et_idchauffeur = getArguments().getString("idchauffeur");
et_idtrajet = getArguments().getString("idtrajet");
et_placesvoulues = getArguments().getString("placesvoulues");
et_rechercheVilleD = getArguments().getString("VilleDepart");
et_rechercheVilleA = getArguments().getString("VilleArrivee");
et_rechercheHeureH = getArguments().getString("Heure");
et_rechercheDateD = getArguments().getString("Date");
prenom_passager_arrayList.clear();
id_passager_arrayList.clear();
niveau_passager_arrayList.clear();
age_passager_arrayList.clear();
ArrayList<String> prenom_passager_arrayList = helper.getTrajetPrenomPassager(et_idtrajet);
ArrayList<String> id_passager_arrayList = helper.getTrajetIDPassager(et_idtrajet);
ArrayList<String> niveau_passager_arrayList = helper.getTrajetNiveauPassager(et_idtrajet);
ArrayList<String> age_passager_arrayList = helper.getTrajetAgePassager(et_idtrajet);
ListAdapter = new PassagerAdapter(this.getActivity(),
prenom_passager_arrayList,
id_passager_arrayList,
niveau_passager_arrayList,
age_passager_arrayList
);
liste_passag.setAdapter(ListAdapter);
liste_passag.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View child, int position, long id) {
}
});
}
适配器
package cm.opep.opep.transfert;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import cm.opep.opep.R;
public class PassagerAdapter extends BaseAdapter {
private Context contxt;
private ArrayList<String> prenom;
private ArrayList<String> idpassager;
private ArrayList<String> niveau;
private ArrayList<String> age;
//private ArrayList<String> avis;
//private ArrayList<String> note;
public PassagerAdapter(Context context1, ArrayList<String> et_prenom, ArrayList<String> et_idpassager, ArrayList<String> et_niveau, ArrayList<String> et_age) {
this.contxt = context1;
this.prenom = et_prenom;
this.idpassager = et_idpassager;
this.niveau = et_niveau;
this.age = et_age;
//this.avis = et_avis;
//this.note = et_note;
}
@Override
public int getCount() {
return prenom.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View child, ViewGroup parent) {
Holder holder;
LayoutInflater layoutInflater;
if(child == null) {
layoutInflater = (LayoutInflater) contxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
assert layoutInflater != null;
child = layoutInflater.inflate(R.layout.fragment_detail_resultat_recherche_passager, parent,false);
holder = new Holder();
holder.pranom = child.findViewById(R.id.prenom_passager);
holder.idpassenger = child.findViewById(R.id.idpassager);
holder.niv = child.findViewById(R.id.niveau_passager);
holder.old = child.findViewById(R.id.age_passager);
//holder.mind= child.findViewById(R.id.avis_passager);
//holder.rank= child.findViewById(R.id.note_passager);
child.setTag(holder);
}
else {
holder = (Holder) child.getTag();
}
holder.pranom.setText(prenom.get(position));
holder.idpassenger.setText(idpassager.get(position));
holder.niv.setText(niveau.get(position));
holder.old.setText(age.get(position));
//holder.mind.setText(avis.get(position));
//holder.rank.setText(note.get(position));
return child;
}
public class Holder {
TextView pranom;
TextView niv;
TextView old;
//TextView mind;
TextView idpassenger;
//TextView rank;
}
}
DatabaseHelper
public ArrayList<String> getTrajetPrenomPassager(String et_idtrajet) {
ArrayList<String> prenom_passager_arrayList = new ArrayList<>();
String selectQuery = "SELECT * FROM " + TABLE_NAME11 + " INNER JOIN " + TABLE_NAME1 + " ON " + COLUMN_ID_PASSAGER + " = " + COLUMN_ID_USER + " WHERE (" + COLUMN_ID_TRAJET + " = '" + et_idtrajet + "' AND " + COLUMN_VALIDATION_RESERVATION + " != 'Invalidé' AND " + COLUMN_DATE_ANNULATION_DU_PASSAGER + " IS NULL) ORDER BY " + COLUMN_DATE_DE_RESERVATION + " ASC";
db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
prenom_passager_arrayList.add(cursor.getString(cursor.getColumnIndex(COLUMN_PRENOM)));
} while (cursor.moveToNext());
cursor.close();
db.close();
}
return prenom_passager_arrayList;
}
public ArrayList<String> getTrajetIDPassager(String et_idtrajet) {
ArrayList<String> id_passager_arrayList = new ArrayList<>();
String selectQuery = "SELECT * FROM " + TABLE_NAME11 + " INNER JOIN " + TABLE_NAME1 + " ON " + COLUMN_ID_PASSAGER + " = " + COLUMN_ID_USER + " WHERE (" + COLUMN_ID_TRAJET + " = '" + et_idtrajet + "' AND " + COLUMN_VALIDATION_RESERVATION + " != 'Invalidé' AND " + COLUMN_DATE_ANNULATION_DU_PASSAGER + " IS NULL) ORDER BY " + COLUMN_DATE_DE_RESERVATION + " ASC";
db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
id_passager_arrayList.add(cursor.getString(cursor.getColumnIndex(COLUMN_ID_PASSAGER)));
} while (cursor.moveToNext());
cursor.close();
db.close();
}
return id_passager_arrayList;
}
public ArrayList<String> getTrajetNiveauPassager(String et_idtrajet) {
ArrayList<String> niveau_passager_arrayList = new ArrayList<>();
String selectQuery = "SELECT * FROM " + TABLE_NAME11 + " INNER JOIN " + TABLE_NAME1 + " ON " + COLUMN_ID_PASSAGER + " = " + COLUMN_ID_USER + " WHERE (" + COLUMN_ID_TRAJET + " = '" + et_idtrajet + "' AND " + COLUMN_VALIDATION_RESERVATION + " != 'Invalidé' AND " + COLUMN_DATE_ANNULATION_DU_PASSAGER + " IS NULL) ORDER BY " + COLUMN_DATE_DE_RESERVATION + " ASC";
db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
niveau_passager_arrayList.add(cursor.getString(cursor.getColumnIndex(COLUMN_NIVEAU)));
} while (cursor.moveToNext());
cursor.close();
db.close();
}
return niveau_passager_arrayList;
}
public ArrayList<String> getTrajetAgePassager(String et_idtrajet) {
ArrayList<String> age_passager_arrayList = new ArrayList<>();
String selectQuery = "SELECT * FROM " + TABLE_NAME11 + " INNER JOIN " + TABLE_NAME1 + " ON " + COLUMN_ID_PASSAGER + " = " + COLUMN_ID_USER + " WHERE (" + COLUMN_ID_TRAJET + " = '" + et_idtrajet + "' AND " + COLUMN_VALIDATION_RESERVATION + " != 'Invalidé' AND " + COLUMN_DATE_ANNULATION_DU_PASSAGER + " IS NULL) ORDER BY " + COLUMN_DATE_DE_RESERVATION + " ASC";
db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
age_passager_arrayList.add(cursor.getString(cursor.getColumnIndex(COLUMN_AGE)));
} while (cursor.moveToNext());
cursor.close();
db.close();
}
return age_passager_arrayList;
}