我是Android开发的初学者,正在开发一个小项目,以显示设备上现有传感器的列表。但我可以显示传感器列表。我调试了,当我运行程序时,我发现有18个传感器,但我无法在Android屏幕上显示它们。
listItem.java代码:
public class ListItem {
private String title;
public ListItem(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private List<ListItem> SensorItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.sensorsRV);
SensorItems = new ArrayList<ListItem>();
SensorManager sensorManager = (SensorManager) this.getSystemService(this.SENSOR_SERVICE);
List sensorList = sensorManager.getSensorList(Sensor.TYPE_ALL);
for(int i = 0; i < sensorList.size(); i++){
ListItem currentSensor = new ListItem(((Sensor)(sensorList.get(i))).getName());
SensorItems.add(currentSensor);
}
adapter = new MyAdapter(SensorItems);
recyclerView.setAdapter(adapter);
}
}
MyAdapter.java
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
List <ListItem> listItems;
private Context context;
public MyAdapter(List<ListItem> listItems) {
this.listItems = listItems;
}
//creates whenever our view holder is created
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item,parent,false);
return new ViewHolder(view);
}
//this method is called after onCreateViewHolder to bind data to view holder to show actuall data to recycler view
@Override
public void onBindViewHolder(MyAdapter.ViewHolder holder, int position) {
ListItem listItem = listItems.get(position);
holder.headingTextView.setText(listItem.getTitle());
}
@Override
public int getItemCount() {
return listItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
public TextView headingTextView;
Context context;
public LinearLayout myLayout;
public ViewHolder(View itemView) {
super(itemView);
myLayout = (LinearLayout) itemView;
headingTextView = (TextView) itemView.findViewById(R.id.textVeiwHead);
context = itemView.getContext();
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="com.example.sahar.devicesensors.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="30dp"
android:text="List of Device's Sensors"
android:textSize="16dp"
android:textStyle="bold"
android:textColor="@android:color/holo_blue_dark"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/sensorsRV"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp" />
</LinearLayout>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:padding="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/textVeiwHead"
android:text="Heading"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
如果有人可以给我一些提示,为什么我无法获得列表显示我真的很感激。
答案 0 :(得分:1)
在MainActivity上试试这个:
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
LayoutManager的构造函数接收上下文。 Context包含有关应用程序的全局信息。
活动从Context扩展,因此您可以使用活动。
答案 1 :(得分:1)
在将数据显示到回收站视图后,对主要活动进行一些更改。
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private MyAdapter adapter;
private List<ListItem> SensorItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.sensorsRV);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
SensorItems = new ArrayList<ListItem>();
SensorManager sensorManager = (SensorManager) this.getSystemService(this.SENSOR_SERVICE);
List sensorList = sensorManager.getSensorList(Sensor.TYPE_ALL);
for (int i = 0; i < sensorList.size(); i++) {
ListItem currentSensor = new ListItem(((Sensor) (sensorList.get(i))).getName());
SensorItems.add(currentSensor);
}
adapter = new MyAdapter(SensorItems);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
答案 2 :(得分:0)
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(mContext);
recyclerView.setLayoutManager(layoutManager);
继续setAdapter。
答案 3 :(得分:0)
setAdapter
您必须在LayoutManager
之前wrap_content
,因为适配器取决于layout_height
。并且,您最好在list_item.xml
的根节点中使用~(\.png|gif|jpe?g|bmp)$~
if (preg_match('~(\.png|gif|jpe?g|bmp)$', $data->files)) {
echo 'image only;'
}
else {
echo 'image + other types';
}
。
答案 4 :(得分:0)
我发现您没有在回收站视图中设置布局管理器。所以您可以通过xml设置布局管理器,如下所示: -
<android.support.v7.widget.RecyclerView
xmlns:app="http://schemas.android.com/apk/res-auto"
app:layoutManager="android.support.v7.widget.LinearLayoutManager" >
或者您也可以使用以下代码执行上述操作: -
//To show vertical scrolling list
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
//To show horizontal scrolling list
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(linearLayoutManager);