我正在制作两个旋转器:一个用于城市,另一个用于特定城市的区域。根据用户在上述微调器中所做的选择,将显示ListView,这对于用户在微调器中所做的选择的不同组合将是不同的。我从存储在Firebase中的数据库获取Listview的数据。 java代码中提到的URL每次都会改变。但是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片段代码是:
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 clas如下:
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对于微调器中的不同选择是不同的。
答案 0 :(得分:1)
您是否在selectCity onItemSelectListener上使用了DatabaseReference addValueEventListener。在您不添加任何值之前,不会调用onDataChange。所以没有被调用,你的适配器还没有准备好填充下一个列表。