我无法弄清楚为什么ImageView不会出现在ListView
中Left side is the result, Right side is in the editor
以下是XML代码:
`<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="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/default_service_name" />
<ImageView
android:id="@+id/ivLed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="false"
android:contentDescription="@string/ic_led"
android:cropToPadding="false"
android:visibility="visible"
app:srcCompat="@mipmap/ic_led" />
<ImageView
android:id="@+id/ivLedDisp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/ic_led_display"
android:visibility="visible"
app:srcCompat="@mipmap/ic_leddisp" />
<ImageView
android:id="@+id/ivSpeaker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|right"
android:contentDescription="@string/ic_speaker"
android:foregroundGravity="top|right"
android:visibility="visible"
app:srcCompat="@mipmap/ic_speaker" />
<ImageView
android:id="@+id/ivServo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/ic_servo"
android:visibility="visible"
app:srcCompat="@mipmap/ic_servo" />
<ImageView
android:id="@+id/ivMotor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|right"
android:contentDescription="@string/ic_motor"
android:foregroundGravity="top|right"
android:visibility="visible"
app:srcCompat="@mipmap/ic_motor" />
</LinearLayout>
<TextView
android:id="@+id/tvDesc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/default_service_desc" />
`
无论我使用什么图像,从哪个来源......它都没有显示...... 我试图通过代码再次使其隐藏和可见,但仍然无法工作:(
抱歉这个愚蠢的问题,但这是我第一次做Android应用程序
EDIT1: 活动类 `public class ServiceList扩展了AppCompatActivity {
private ListView lvServices;
private ServiceAdaptor adaptor;
private List<Service> mServicesList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_service_list);
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference rootRef = database.getReference();
DatabaseReference servicesRef = rootRef.child("services");
lvServices = (ListView)findViewById(R.id.mListView_services);
mServicesList = new ArrayList<>();
servicesRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (mServicesList.size() > 0)
mServicesList.clear();
for (DataSnapshot id : dataSnapshot.getChildren()) {
Service service = id.getValue(Service.class);
mServicesList.add(service);
Toast.makeText(getApplicationContext(), "It's WORKING!" + service, Toast.LENGTH_SHORT).show();
}
adaptor = new ServiceAdaptor(getApplicationContext(),mServicesList);
lvServices.setAdapter(adaptor);
}
@Override
public void onCancelled(DatabaseError databaseError) {
String error = databaseError.getDetails();
}
});`
使用firebase,我只更改TextViews,不触及图像
EDIT2:
Service.java
public class Service {
private int id;
private String name = "Name";
private String desc = "Description";
private boolean led = false;
private boolean ledDisplay = false;
private boolean servo = false;
private boolean motor = false;
private boolean speaker = false;
public Service() {
}
//Constructors
public Service(String name, String desc, boolean led, boolean ledDisplay, boolean servo, boolean motor, boolean speaker) {
this.name = name;
this.desc = desc;
this.led = led;
this.ledDisplay = ledDisplay;
this.servo = servo;
this.motor = motor;
this.speaker = speaker;
}
//Set/Get
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public boolean isLed() {
return led;
}
public void setLed(boolean led) {
this.led = led;
}
public boolean isLedDisplay() {
return ledDisplay;
}
public void setLedDisplay(boolean ledDisplay) {
this.ledDisplay = ledDisplay;
}
public boolean isServo() {
return servo;
}
public void setServo(boolean servo) {
this.servo = servo;
}
public boolean isMotor() {
return motor;
}
public void setMotor(boolean motor) {
this.motor = motor;
}
public boolean isSpeaker() {
return speaker;
}
public void setSpeaker(boolean speaker) {
this.speaker = speaker;
}
}
和ServiceAdaptor.java
public class ServiceAdaptor extends BaseAdapter {
private Context mContext;
private List<Service> mServicesList;
//Constructors
public ServiceAdaptor(Context mContext, List<Service> mServicesList) {
this.mContext = mContext;
this.mServicesList = mServicesList;
}
@Override
public int getCount() {
return mServicesList.size();
}
@Override
public Object getItem(int position) {
return mServicesList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = View.inflate(mContext, R.layout.services, null);
TextView tvName = (TextView) v.findViewById(R.id.tvName);
TextView tvDesc = (TextView) v.findViewById(R.id.tvDesc);
ImageView ivLed = (ImageView) v.findViewById(R.id.ivLed);
ImageView ivLedDisp = (ImageView) v.findViewById(R.id.ivLedDisp);
ImageView ivServo = (ImageView) v.findViewById(R.id.ivServo);
ImageView ivMotor = (ImageView) v.findViewById(R.id.ivMotor);
ImageView ivSpeaker = (ImageView) v.findViewById(R.id.ivSpeaker);
tvName.setText(mServicesList.get(position).getName());
tvDesc.setText(mServicesList.get(position).getDesc());
if (!mServicesList.get(position).isLed()) ivLed.setVisibility(View.INVISIBLE);
if (!mServicesList.get(position).isLedDisplay()) ivLedDisp.setVisibility(View.INVISIBLE);
if (!mServicesList.get(position).isServo()) ivServo.setVisibility(View.INVISIBLE);
if (!mServicesList.get(position).isMotor()) ivMotor.setVisibility(View.INVISIBLE);
if (!mServicesList.get(position).isSpeaker()) ivSpeaker.setVisibility(View.INVISIBLE);
v.setTag(mServicesList.get(position).getId());
return v;
}
}
这是他们正在接收的数据
`{ "services" : [ null, { "desc" : "Playing the speaker",
"led" : false,
"ledDisp" : false,
"motor" : false,
"name" : "Sing",
"servo" : false,
"speaker" : true},
{ "desc" : "1322",
"led" : true,
"ledDisp" : true,
"motor" : false,
"name" : "Mike",
"servo" : false,
"speaker" : false
} ]
}`
只是为了检查布尔检查是否有效我改变了图像的可见性,只是改变了TextView的颜色并且有效,所以问题在于图像
答案 0 :(得分:0)
尝试将其替换为:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Hello, world" />
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@mipmap/ic_launcher"
android:layout_toEndOf="@id/editText"/>
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@mipmap/ic_launcher"
android:layout_toEndOf="@id/imageView"/>
</RelativeLayout>
如果它不起作用,我会给你另一个建议。