gpt4 book ai didi

java - 具有 2 个对象适配器的 ListView 无法正常工作

转载 作者:行者123 更新时间:2023-12-02 11:20:47 24 4
gpt4 key购买 nike

您好,我尝试使用 2 个对象调用创建 ListView (客户,问题)

所以,当我运行应用程序时,问题是我看到 ListView 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>

感谢您的帮助!

最佳答案

您不应为对象类型创建适配器。你的问题是这段代码:

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());

}

您尝试在同一 View 中显示客户详细信息和问题详细信息,但对象可以是客户也可以是产品。它不能同时存在,因此您的 View 将显示客户详细信息或问题详细信息。

在我看来,您应该创建一个名为 CustomerProblem 的新类,它捕获您需要的有关每个客户及其问题的所有信息。然后您可以为 CustomerProblem 创建一个新的适配器。

我还建议您研究 ViewHolder 模式。

关于java - 具有 2 个对象适配器的 ListView 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49931491/

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