gpt4 book ai didi

android - 从微调器中选择项目时未显示 ListView

转载 作者:行者123 更新时间:2023-11-30 00:50:07 25 4
gpt4 key购买 nike

我正在创建两个微调器:一个用于城市,另一个用于该特定城市的区域。根据用户在上述微调器中所做的选择,将显示一个 ListView,该 ListView 对于用户在微调器中所做的不同选择组合会有所不同。我正在从存储在 Firebase 中的数据库中获取 Listview 的数据。 Java 代码中提到的 URL 每次都会更改。但是 ListView 本身没有显示。

具有微调器和 ListView 的布局如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="100dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select a City"
/>

<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/selectCity"
android:entries="@array/city_name"
android:textSize="20sp"
android:singleLine="false"
android:prompt="@string/city_title"
android:outlineProvider="background"

android:background="@android:drawable/btn_dropdown"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Area"
android:paddingTop="20sp"
/>
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/select_area"
android:background="@android:drawable/btn_dropdown"
>
</Spinner>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"

android:id="@+id/displayAllAstrologers"

></ListView>

</LinearLayout>

我用来处理微调器和 Listview 的 java fragment 代码是:

 package com.example.hsports.weddingplanner.Fragments;


import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;

import com.example.hsports.weddingplanner.Activities.FrontPage;
import com.example.hsports.weddingplanner.Adapters.displayingListCategories;
import com.example.hsports.weddingplanner.R;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;

/**
* Created by I324671 on 12/5/2016.
*/
public class Location_Selection extends Fragment {
ArrayList<String> listCategories=new ArrayList<>();

String categorySelected;

String citySelected="";
String areaSelected="";
public Location_Selection(String categorySelected)
{
this.categorySelected= this.categorySelected;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.city_selection_page,container,false);
final Spinner areaSelector=(Spinner)view.findViewById(R.id.select_area);
Spinner selectCity=(Spinner)view.findViewById(R.id.selectCity);
final ListView listView = (ListView) view.findViewById(R.id.displayAllAstrologers);
selectCity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, final View view, int position, long id) {

final ArrayList<String> listAreas = new ArrayList<String>();

String s = parent.getItemAtPosition(position).toString();
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference reference = database.getReference(s);

reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {

if (dataSnapshot.hasChildren()) {
for (DataSnapshot child : dataSnapshot.getChildren()) {
String areaId = child.getKey().toString();
String areaName = child.getValue().toString();
listAreas.add(areaName);
}
}
ArrayAdapter<String> adapter = new ArrayAdapter(view.getContext(),
android.R.layout.simple_spinner_item, listAreas);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
areaSelector.setAdapter(adapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});

}
@Override
public void onNothingSelected(AdapterView<?> parent) {

}
});
areaSelector.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
FirebaseDatabase database1 = FirebaseDatabase.getInstance();
DatabaseReference reference1 = database1.getReference("Astrologer-Hoodi/email");
reference1.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {

if (dataSnapshot.hasChildren()) {
for (DataSnapshot child : dataSnapshot.getChildren()) {
String getEmailNo = child.getKey().toString();
String getEmailId = child.getValue().toString();
listCategories.add(getEmailId);
}
}
listView.setAdapter(new displayingListCategories(getContext(), listCategories));
}
@Override
public void onCancelled(DatabaseError databaseError) {
}

});



}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}
});

return view;
}
}

我正在使用 baseAdapter 来扩充 xml 中的 ListView。所以扩展BaseAdapter的Adapter类如下:

package com.example.hsports.weddingplanner.Adapters;

import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.hsports.weddingplanner.R;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;

/**
* Created by I324671 on 12/5/2016.
*/
public class displayingListCategories extends BaseAdapter {

private Context mcontext;

public ArrayList<String> listCategories;

public displayingListCategories(Context context,ArrayList<String> listCategories) {
//super(context, c);
mcontext=context;
this.listCategories=listCategories;
}


@Override
public int getCount() {
return 0;
}

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {

View view= LayoutInflater.from(convertView.getContext()).inflate(R.layout.format_of_list_of_categories, parent, false);
ImageView imageView=(ImageView)view.findViewById(R.id.image_of_customer);
imageView.setImageResource(R.drawable.customer_photo);
TextView nameOfCustomer=(TextView)view.findViewById(R.id.name_of_customer);
TextView emailIdOfCustomer=(TextView)view.findViewById(R.id.email_id_of_customer);
TextView phoneOfCustomer=(TextView)view.findViewById(R.id.phone_no_of_customer);
nameOfCustomer.setText(listCategories.get(position));
emailIdOfCustomer.setText(listCategories.get(position));
phoneOfCustomer.setText(listCategories.get(position));
return view;
}
}

我没有显示 ListView 。我希望 ListView 对于微调器中的不同选择是不同的。

最佳答案

您是否在 selectCity onItemSelectListener 上使用了 DatabaseReference addValueEventListener。在您不添加任何值之前,不会调用 onDataChange。所以没有被调用,你的适配器还没有准备好填充下一个列表。

关于android - 从微调器中选择项目时未显示 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41197315/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com