您好我尝试使用2个对象调用(Customer, Problem
)
因此,当我运行应用程序时,问题是我看到列表视图like this,并且对于需要的示例,我希望看到这样:
第1行.Eran Peer
0544634631
屏幕保护器替换
第2行.Eran同行
0544634632
音量灵活
而不喜欢照片。
客户问题页面代码为:
package com.example.eranp.clientpage;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
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;
import java.util.List;
import java.util.Objects;
public class CustomersProblemsPage extends Activity {
ListView listViewCustomersProblems;
List<Object> problemsCustomers;
DatabaseReference databaseCustomers, databaseProblem;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_customers_problems_page);
listViewCustomersProblems = (ListView)findViewById(R.id.LVCustomerProblem);
databaseCustomers = FirebaseDatabase.getInstance().getReference("customer");
databaseProblem = FirebaseDatabase.getInstance().getReference("problem");
problemsCustomers = new ArrayList<>();
Intent intent = getIntent();
}
@Override
protected void onStart() {
super.onStart();
databaseCustomers.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
//getting customer
Customer customer = postSnapshot.getValue(Customer.class);
//adding customer to the list
problemsCustomers.add(customer);
}
CustomerProblemAdapter customerProblemAdapter = new CustomerProblemAdapter(CustomersProblemsPage.this, (ArrayList<Object>) problemsCustomers);
//attaching adapter to the listview
listViewCustomersProblems.setAdapter(customerProblemAdapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
databaseProblem.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
problemsCustomers.clear();
//iterating through all the nodes
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
//getting customer
Problem problem = postSnapshot.getValue(Problem.class);
//adding customer to the list
problemsCustomers.add(problem);
}
CustomerProblemAdapter customerProblemAdapter = new CustomerProblemAdapter(CustomersProblemsPage.this, (ArrayList<Object>) problemsCustomers);
//attaching adapter to the listview
listViewCustomersProblems.setAdapter(customerProblemAdapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
适配器代码:
package com.example.eranp.clientpage;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.Inflater;
import static android.graphics.Color.BLUE;
import static android.graphics.Color.GREEN;
import static android.graphics.Color.RED;
import static android.graphics.Color.blue;
import static com.example.eranp.clientpage.R.layout.customer_row;
/**
* Created by Eran P on 16/04/2018.
*/
public class CustomerProblemAdapter extends ArrayAdapter<Object> {
private Activity context;
List<Object> objects ;
public CustomerProblemAdapter(Activity context, ArrayList<Object> items) {
super(context, R.layout.customer_problem_row, items);
this.context = context;
this.objects = items;
}
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
// Create holder
LayoutInflater inflater = context.getLayoutInflater();
View listViewItem = inflater.inflate(R.layout.customer_problem_row, null, true);
TextView fName = (TextView) listViewItem.findViewById(R.id.textVFNameCp);
TextView lName = (TextView) listViewItem.findViewById(R.id.textVLnameCp);
TextView phone = (TextView)listViewItem.findViewById(R.id.textVTeleleCp);
TextView proDeviceShort = (TextView)listViewItem.findViewById(R.id.pro_device_det_shortCp);
Object a = (Object) getItem(position);
if (a instanceof Problem) {
proDeviceShort.setText(((Problem) a).getProDevDetShort());
if (((Problem) a).getUrgency() == 1)
listViewItem.setBackgroundColor(RED);
if (((Problem) a).getUrgency() == 2) {
listViewItem.setBackgroundColor(BLUE);
}
if (((Problem) a).getUrgency() == 3)
listViewItem.setBackgroundColor(GREEN);
}
if (a instanceof Customer) {
fName.setText(((Customer) a).getfName());
lName.setText(((Customer) a).getlName());
phone.setText(((Customer) a).getPhoneNum());
}
return listViewItem;
}
}
Xml ListView:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context="com.example.eranp.clientpage.CustomersProblemsPage">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/LVCustomerProblem">
</ListView>
</LinearLayout>
XML问题客户行
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context="com.example.eranp.clientpage.CustomerAdapter">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:text="@string/fName"
android:textSize="25dp"
android:id="@+id/textVFNameCp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textVLnameCp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:text="@string/lName"
android:textSize="25dp" />
</LinearLayout>
<TextView
android:text="@string/telephone"
android:textSize="15dp"
android:id="@+id/textVTeleleCp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24dp"
android:id="@+id/pro_device_det_shortCp"
android:gravity="center"/>
</LinearLayout>
谢谢你的帮助!
答案 0 :(得分:1)
您不应为Object类型创建适配器。你的问题就是这段代码:
if (a instanceof Problem) {
proDeviceShort.setText(((Problem) a).getProDevDetShort());
if (((Problem) a).getUrgency() == 1)
listViewItem.setBackgroundColor(RED);
if (((Problem) a).getUrgency() == 2) {
listViewItem.setBackgroundColor(BLUE);
}
if (((Problem) a).getUrgency() == 3)
listViewItem.setBackgroundColor(GREEN);
}
if (a instanceof Customer) {
fName.setText(((Customer) a).getfName());
lName.setText(((Customer) a).getlName());
phone.setText(((Customer) a).getPhoneNum());
}
您尝试在同一视图中显示客户详细信息和问题详细信息,但对象可以是客户或产品。它不能同时存在,因此您的视图将显示客户详细信息或问题详细信息。
在我看来,您应该创建一个名为CustomerProblem的新类,它捕获有关每个客户及其问题的所有信息。然后,您可以为CustomerProblem创建一个新的Adapter。
我还建议您研究ViewHolder模式。