我目前正在开发一个应用,该应用在listView中显示Firebase的所有订单数据。我有一个问题,当Firebase数据更改时,列表视图将不会更新,当我在Firebase应用程序中删除行时崩溃( 错误:无法将类型java.util.HashMap的值转换为String < / em> )。谁能帮助我我错了?
MainActivity.java
public class MainActivity extends AppCompatActivity {
private DatabaseReference md;
private ListView maListView;
private ArrayList<OrdersModel> dataModels = new ArrayList<>();
private OrderCustomAdapter adapter;
Context c;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
c = this;
md = FirebaseDatabase.getInstance().getReference("PERA");
maListView = (ListView) findViewById(R.id.ma_list_view);
adapter = new OrderCustomAdapter((ArrayList<OrdersModel>) dataModels, c);
maListView.setAdapter((ListAdapter) adapter);
maListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final OrdersModel dataModel = dataModels.get(position);
View popupView = getLayoutInflater().inflate(R.layout.popup_layout, null);
final PopupWindow popupWindow = new PopupWindow(popupView,
AbsListView.LayoutParams.WRAP_CONTENT, AbsListView.LayoutParams.WRAP_CONTENT);
TextView tvpStart = (TextView) popupView.findViewById(R.id.tvpStart);
TextView tvpZiel = (TextView) popupView.findViewById(R.id.tvpZiel);
TextView tvpName = (TextView) popupView.findViewById(R.id.tvpName);
TextView tvpTelefonnummer = (TextView) popupView.findViewById(R.id.tvpTelefonnummer);
TextView tvpPreis = (TextView) popupView.findViewById(R.id.tvpPreis);
TextView tvpFahrer = (TextView) popupView.findViewById(R.id.tvpFahrer);
TextView tvpFahrertelefonnummer = (TextView) popupView.findViewById(R.id.tvpFahrertelefonnummer);
Button callButton = (Button) popupView.findViewById(R.id.btnCall);
tvpStart.setText(dataModel.getStart());
tvpZiel.setText(dataModel.getZiel());
tvpName.setText(dataModel.getName());
tvpTelefonnummer.setText(dataModel.getTelefonnummer());
tvpPreis.setText("€ " + dataModel.getPreis());
tvpFahrer.setText(dataModel.getFahrer());
tvpFahrertelefonnummer.setText(dataModel.getFahrerTelefonnummer());
callButton.setOnClickListener(new View.OnClickListener() {
Intent call = new Intent(Intent.ACTION_DIAL);
public void onClick(View v) {
// call(dataModel.getFahrerTelefonnummer());
call.setData(Uri.parse("tel:" + dataModel.getFahrerTelefonnummer()));
startActivity(call);
}
});
popupWindow.setFocusable(true);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
popupWindow.setWidth(width - 100);
popupWindow.setHeight(height - 100);
popupWindow.setContentView(popupView);
popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
}
});
md.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot postSnapshot, String s) {
OrdersModel ds = postSnapshot.getValue(OrdersModel.class);
dataModels.add(ds);
adapter.notifyDataSetChanged();
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
adapter.notifyDataSetChanged();
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
Map<String, Object> map = (Map<String, Object>) dataSnapshot.getValue();
String string = dataSnapshot.getValue(String.class);
dataModels.remove(map);
adapter.notifyDataSetChanged();
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
adapter.notifyDataSetChanged();
}
});
}
public void call(String number) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse(number));
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
return;
}
startActivity(callIntent);
}
}
OrderCustomAdapter.java
public class OrderCustomAdapter extends ArrayAdapter<OrdersModel> implements View.OnClickListener{
private ArrayList<OrdersModel> dataSet;
Context mContext;
// View lookup cache
private static class ViewHolder {
TextView txtDatumZeit;
TextView txtFahrer;
TextView txtPreis;
}
public OrderCustomAdapter(ArrayList<OrdersModel> data, Context context) {
super(context, R.layout.order_item, data);
this.dataSet = data;
this.mContext=context;
}
@Override
public void onClick(View v) {
int position=(Integer) v.getTag();
Object object= getItem(position);
OrdersModel dataModel=(OrdersModel)object;
}
private int lastPosition = -1;
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public View getView(int position, View convertView, ViewGroup parent) {
OrdersModel dataModel = getItem(position);
ViewHolder viewHolder;
final View result;
final OrdersModel s = this.getItem(position);
String dwa = s.getDriverConfirm();
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.order_item, parent, false);
viewHolder.txtDatumZeit = (TextView) convertView.findViewById(R.id.tvDatumZeit);
viewHolder.txtFahrer = (TextView) convertView.findViewById(R.id.tvFahrer);
viewHolder.txtPreis = (TextView) convertView.findViewById(R.id.tvPreis);
result=convertView;
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
result=convertView;
}
SimpleDateFormat df = new SimpleDateFormat("dd.MM.yyyy - HH:mm");
String hg = df.format(dataModel.getDatumZeit());
viewHolder.txtDatumZeit.setText(hg);
viewHolder.txtFahrer.setText(dataModel.getFahrer());
viewHolder.txtPreis.setText("€ "+dataModel.getPreis());
if (dwa.contentEquals("ja")) {
convertView.setBackgroundColor(Color.parseColor("#1FBB5E"));
}else{
convertView.setBackgroundColor(Color.parseColor("#FFFFFF"));
}
return convertView;
}
}
OrdersModel.java
public class OrdersModel {
public OrdersModel() {}
public String telefonnummer;
public String start;
public String adminConfirm;
public String url;
public String orderId;
public String fahrerTelefonnummer;
public String preis;
public String name;
public String fahrer;
public long datumZeit;
public String ziel;
public String driverConfirm;
public String getTelefonnummer() {
return telefonnummer;
}
public void setTelefonnummer(String telefonnummer) {
this.telefonnummer = telefonnummer;
}
public String getStart() {
return start;
}
public void setStart(String start) {
this.start = start;
}
public String getAdminConfirm() {
return adminConfirm;
}
public void setAdminConfirm(String adminConfirm) {
this.adminConfirm = adminConfirm;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getOrderId() {
return orderId;
}
public void setOrderId(String orderId) {
this.orderId = orderId;
}
public String getFahrerTelefonnummer() {
return fahrerTelefonnummer;
}
public void setFahrerTelefonnummer(String fahrerTelefonnummer) {
this.fahrerTelefonnummer = fahrerTelefonnummer;
}
public String getPreis() {
return preis;
}
public void setPreis(String preis) {
this.preis = preis;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getFahrer() {
return fahrer;
}
public void setFahrer(String fahrer) {
this.fahrer = fahrer;
}
public long getDatumZeit() {
return datumZeit;
}
public void setDatumZeit(long datumZeit) {
this.datumZeit = datumZeit;
}
public String getZiel() {
return ziel;
}
public void setZiel(String ziel) {
this.ziel = ziel;
}
public String getDriverConfirm() {
return driverConfirm;
}
public void setDriverConfirm(String driverConfirm) {
this.driverConfirm = driverConfirm;
}
}
order_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical">
<LinearLayout
android:id="@+id/rowOrders"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:orientation="horizontal">
<TextView
android:id="@+id/tvDatumZeit"
android:layout_width="420dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_weight="1"
android:text="" />
<TextView
android:id="@+id/tvPreis"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="" />
<TextView
android:id="@+id/tvFahrer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:paddingRight="20dp"
android:text="" />
</LinearLayout>
</RelativeLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="@+id/ma_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
popup_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E6044A7F">
<TextView
android:id="@+id/tvpName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="30dp"
android:text="Name"
android:textColor="#FFFFFF" />
<TextView
android:id="@+id/tvpTelefonnummer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/tvpName"
android:layout_below="@+id/tvpName"
android:text="Telefonnummer"
android:textColor="#FFFFFF" />
<TextView
android:id="@+id/tvpStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/tvpName"
android:layout_below="@+id/tvpTelefonnummer"
android:layout_marginRight="30dp"
android:layout_marginTop="5dp"
android:text="Start"
android:textColor="#ffffff" />
<TextView
android:id="@+id/tvpZiel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/tvpName"
android:layout_below="@+id/tvpStart"
android:layout_marginRight="30dp"
android:layout_marginTop="15dp"
android:text="Ziel"
android:textColor="#ffffff" />
<TextView
android:id="@+id/tvpPreis"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/tvpName"
android:layout_below="@+id/tvpZiel"
android:layout_marginTop="15dp"
android:text="Preis"
android:textColor="#ffffff" />
<TextView
android:id="@+id/tvpFahrer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/tvpName"
android:layout_below="@+id/tvpPreis"
android:layout_marginTop="30dp"
android:text="Fahrer"
android:textColor="#ffffff" />
<TextView
android:id="@+id/tvpFahrertelefonnummer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/tvpName"
android:layout_below="@+id/tvpFahrer"
android:text="Fahrer Telefonnummer"
android:textColor="#ffffff" />
<Button
android:id="@+id/btnCall"
style="@android:style/Widget.DeviceDefault.Light.Button.Inset"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignStart="@+id/tvpName"
android:layout_marginBottom="30dp"
android:layout_marginRight="30dp"
android:text="Fahrer anrufen" />
<Button
android:id="@+id/btnUberCOnfirm"
style="@android:style/Widget.DeviceDefault.Light.Button.Inset"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignStart="@+id/tvpName"
android:layout_marginBottom="109dp"
android:layout_marginRight="30dp"
android:text="UBER BESTÄTIGEN" />
</RelativeLayout>
有人可以帮我吗?
答案 0 :(得分:0)
您的列表未更新,因为适配器持有对旧项目的引用。致电
adapter.clear();
adapter.addAll(dataModels);
而不是notifyDataSetChanged()
。 Similar question
看起来像这行
String string = dataSnapshot.getValue(String.class);
正在导致崩溃。 string
永远都不是用户,可以将其删除。