#java #android #firebase #listview #arraylist
# #java #Android #огневая база #listview #arraylist
Вопрос:
Я пытаюсь показать данные, которые я получил из firebase, в listview. Но мой textview в listview не меняется. На этом выходе у меня есть два документа моей базы данных. Появляются два listview, но не могут изменить текст.Где я ошибаюсь?
Вот мой код пользовательского адаптера
public class MissionsAdapter extends ArrayAdapter<Missions> {
public MissionsAdapter(Context context, List<Missions> object){
super(context,0, object);
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
if(convertView == null){
convertView = ((Activity)getContext()).getLayoutInflater().inflate(R.layout.item_missions,parent,false);
}
Missions mission = getItem(position);
TextView titleTextView = (TextView) convertView.findViewById(R.id.mission_title);
TextView expTextView = (TextView) convertView.findViewById(R.id.mission_exp);
TextView dateTextView = (TextView) convertView.findViewById(R.id.mission_date);
TextView descriptionTextView = (TextView) convertView.findViewById(R.id.mission_description);
TextView bilgitext=(TextView) convertView.findViewById(R.id.mission_bilgi);
titleTextView.setText(mission.getTitle());
expTextView.setText(mission.getExp());
dateTextView.setText(mission.getDate());
descriptionTextView.setText(mission.getDescription());
bilgitext.setText(mission.getBilgi());
return convertView;
}
}
Missions.java
public class Missions {
public String title;
public String exp;
public String date;
public String description;
public String bilgi;
public Missions() {}
public Missions(String title, String exp, String date, String description, String bilgi) {
this.title = title;
this.exp = exp;
this.date = date;
this.description = description;
this.bilgi=bilgi;
}
public String getTitle(){
return title;
}
public String getExp(){
return exp;
}
public String getDate(){
return date;
}
public String getDescription(){
return description;
}
public String getBilgi(){
return bilgi;
}
}
item_missions.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<TextView
android:id="@ id/mission_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
android:textAppearance="?android:textAppearanceMedium"
android:hint="There will be a title"/>
<TextView
android:id="@ id/mission_exp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif"
android:textAppearance="?android:textAppearance"
android:hint="There will be exp"/>
<TextView
android:id="@ id/mission_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif"
android:textAppearance="?android:textAppearance"
android:hint="There will be a date"/>
<TextView
android:id="@ id/mission_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif"
android:textAppearance="?android:textAppearance"
android:hint="There will be a description"/>
<TextView
android:id="@ id/mission_bilgi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif"
android:textAppearance="?android:textAppearance"
android:hint="There will be a bilgi"/>
activity_main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@ id/missionHeader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Edit/Create mission"
android:gravity="center"
android:textStyle="bold"
android:textColor="@android:color/black"
android:textAllCaps="true"
android:textSize="30dp"
android:paddingTop="20dp"
/>
<ListView
android:id="@ id/missionList"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
И, наконец, MainActivity
public class MainActivity5 extends AppCompatActivity {
private final String COLLECTION_KEY = "İlanlar";
private TextView mHeaderView;
private ListView mMissionListView;
private FirebaseFirestore db;
private MissionsAdapter mMissionAdapter;
private ArrayList<Missions> mMissionsList;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main5);
mHeaderView = (TextView) findViewById(R.id.missionHeader);
mMissionListView = (ListView) findViewById(R.id.missionList);
mHeaderView.setText("Available Missions");
db = FirebaseFirestore.getInstance();
mMissionsList = new ArrayList<Missions>();
mMissionAdapter = new MissionsAdapter(MainActivity5.this, mMissionsList);
mMissionListView.setAdapter(mMissionAdapter);
db.collection(COLLECTION_KEY).get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
List<Missions> mMissionsList = new ArrayList<>();
if(task.isSuccessful()){
for(QueryDocumentSnapshot document : task.getResult()) {
Missions miss = document.toObject(Missions.class);
mMissionsList.add(miss);
}
ListView mMissionsListView = (ListView) findViewById(R.id.missionList);
MissionsAdapter mMissionAdapter = new MissionsAdapter(MainActivity5.this, mMissionsList);
mMissionsListView.setAdapter(mMissionAdapter);
} else {
Log.d("MissionActivity", "Error getting documents: ", task.getException());
}
}
});
mMissionAdapter.addAll(mMissionsList);
}
}
Комментарии:
1. Что вы подразумеваете под «не удается изменить текст»?
2. mMissionAdapter.notifyDataSetChanged(); добавьте это после добавления данных в arraylist