我有一个回收站视图,我正在将数据传递给recyclerviewadapter,但是回收站视图没有显示任何数据。
ProdyctsRecyclerViewAdapter-它正在setList中获取数据,但不显示
请问我在哪里做错了
谢谢 R
class ProductsRecyclerViewAdapter(private val clickListener: (Product) -> Unit): RecyclerView.Adapter<ProductsMyViewHolder>() {
private val productsList = ArrayList<Product>()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProductsMyViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val binding: ListItemBinding =
DataBindingUtil.inflate(layoutInflater, R.layout.products_list_item, parent, false)
return ProductsMyViewHolder(binding)
}
override fun getItemCount(): Int {
return productsList.size
}
override fun onBindViewHolder(holder: ProductsMyViewHolder, position: Int) {
holder.bind(productsList[position], clickListener)
}
fun setList(products: List<Product>) {
productsList.clear()
productsList.addAll(products)
}
}
class ProductsMyViewHolder(val binding: ListItemBinding): RecyclerView.ViewHolder(binding.root) {
fun bind(product: Product, clickListener: (Product) -> Unit) {
binding.nameTextView.text = product.name
binding.emailTextView.text = product.catagory
binding.listItemLayout.setOnClickListener {
clickListener(product)
}
}
}
ProductsFragment
class ProductsFragment: Fragment() {
private lateinit var binding: ProductsBinding
private lateinit var navController: NavController
private lateinit var productsViewModel: ProductsViewModel
private lateinit var adapter: ProductsRecyclerViewAdapter
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = DataBindingUtil.inflate(inflater, R.layout.products, container, false)
val dao = SubscriberDatabase.getInstance(requireActivity().applicationContext).productDAO
val repository = ProductRepository(dao)
val factory = ProductsViewModelFactory(repository, requireActivity().applicationContext)
productsViewModel = ViewModelProvider(this, factory).get(ProductsViewModel::class.java)
binding.productsViewModel = productsViewModel
binding.lifecycleOwner = this
val view = binding.root
return view
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
navController = Navigation.findNavController(view)
initRecyclerView()
productsViewModel.navigateScreen.observe(viewLifecycleOwner, EventObserver {
navController.navigate(it)
})
}
private fun initRecyclerView() {
binding.productsRecyclerView.layoutManager = LinearLayoutManager(context)
adapter = ProductsRecyclerViewAdapter ({ selectedItem: Product -> listItemClicked(selectedItem)})
displayProductssList()
}
private fun displayProductssList() {
productsViewModel.products.observe(viewLifecycleOwner, Observer {
Log.i("MYTAG", it.toString())
adapter.setList(it)
adapter.notifyDataSetChanged()
})
}
private fun listItemClicked(product: Product) {
Toast.makeText(context, "Selected name is ${product.name}", Toast.LENGTH_LONG).show()
//productsViewModel.initUpdateAndDelete(subscriber)
}
}
ProductsViewModel
class ProductsViewModel (
private val repository: ProductRepository,
private val context: Context
): ViewModel() {
val products = repository.products
}
Products_list_item
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:clickable="true"
android:focusable="true"
app:cardBackgroundColor="@color/colorPrimary"
app:cardCornerRadius="10dp"
app:cardElevation="10dp" >
<LinearLayout
android:id="@+id/product_list_item_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/product_name_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="name"
android:textColor="#FFFFFF"
android:textSize="30sp"
android:textStyle="bold" />
<TextView
android:id="@+id/product_catagory_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="catagory"
android:textColor="#FFFFFF"
android:textSize="24sp"
android:textStyle="bold" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
</layout>
谢谢Parag Pawar的回答 作为参考,进行了以下更改
private fun initRecyclerView() {
binding.productsRecyclerView.layoutManager = LinearLayoutManager(context)
adapter = ProductsRecyclerViewAdapter ({ selectedItem: Product -> listItemClicked(selectedItem)})
binding.productsRecyclerView.adapter = adapter //ADDED THIS LINE
displayProductssList()
}
在productsRecyclerView中,绑定错误,应该是ProductsListItemBinding
val binding: ProductsListItemBinding =
DataBindingUtil.inflate(layoutInflater, R.layout.products_list_item, parent, false)
ProductsMyViewHolder(val绑定:ProductsListItemBinding):
答案 0 :(得分:2)
您尚未将适配器设置为回收站视图。在初始化适配器后,在initRecyclerView()中设置适配器。
private fun initRecyclerView() {
binding.productsRecyclerView.layoutManager = LinearLayoutManager(context)
adapter = ProductsRecyclerViewAdapter ({ selectedItem: Product -> listItemClicked(selectedItem)})
//notice this
binding.productsRecyclerView.adapter = adapter
displayProductssList()
}
答案 1 :(得分:0)
在适配器的setList()方法中添加notifyDataSetChanged()方法
答案 2 :(得分:0)
我不明白您为什么要清除列表并同时添加它们。
const Home = () => {
const grocers = useSelector(state => state.grocers)
const dispatch = useDispatch()
const [map, setMap] = useState(null)
const mapContainer = useRef(null)
useEffect(() => {
dispatch(gotGrocers())
}, [])
useEffect(
() => {
mapboxgl.accessToken =
'personal-token'
const initializeMap = ({setMap, mapContainer}) => {
const map = new mapboxgl.Map({
container: mapContainer.current,
style: 'mapbox://styles/mapbox/streets-v11', // stylesheet location
center: coordinates,
zoom: zoomLevel
})
map.on('load', () => {
setMap(map)
map.resize()
})
map.on('load', () => {
map.addSource('bronx-grocers', {
type: 'geojson',
data: GeoJSON
})
map.addLayer(pointStyles)
})
}
if (!map) initializeMap({setMap, mapContainer})
},
[map]
)
return (
<div>
<div>
<div>
Longitude: {coordinates[0]} | Latitude: {coordinates[1]} | Zoom Level:{' '}
{zoomLevel}
</div>
</div>
<div ref={el => (mapContainer.current = el)} style={mapStyles} />
</div>
)
}
export default Home
如果您想先清除列表,则应在循环外进行