Android 在recyclerview中向下滚动,微调器中的选定项将更改
当用户按下FAB键时,recyclerview中会出现一个cardview。在cardview内部是一个带有一些其他元素的微调器。当我添加多个cardview并从微调器中选择一个项目时,然后我从晶圆厂添加另一个cardview,然后所有微调器选择的项目都会重置 我有一个方法productList.size(),它可能有助于保存微调器选择的值,但我不知道从这里开始该怎么做。谢谢 活动代码Android 在recyclerview中向下滚动,微调器中的选定项将更改,android,Android,当用户按下FAB键时,recyclerview中会出现一个cardview。在cardview内部是一个带有一些其他元素的微调器。当我添加多个cardview并从微调器中选择一个项目时,然后我从晶圆厂添加另一个cardview,然后所有微调器选择的项目都会重置 我有一个方法productList.size(),它可能有助于保存微调器选择的值,但我不知道从这里开始该怎么做。谢谢 活动代码 public class create extends AppCompatActivity {
public class create extends AppCompatActivity {
//a list to store all the products
List<Product> productList;
//the recyclerview
RecyclerView recyclerView;
Product mProduct;
private Map<String, String> numberItemValues = new HashMap<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create);
//opens csv
InputStream inputStream = getResources().openRawResource(R.raw.shopitems);
CSVFile csvFile = new CSVFile(inputStream);
final List<String> mSpinnerItems = csvFile.read();
//getting the recyclerview from xml
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
//initializing the productlist
productList = new ArrayList<>();
final Spinner spinner;
final EditText editText;
final CheckBox checkBox;
final TextView textView5;
spinner = findViewById(R.id.spinner);
editText = findViewById(R.id.editText);
checkBox = findViewById(R.id.checkBox);
textView5 = findViewById(R.id.textView5);
final ProductAdapter adapter = new ProductAdapter(this, productList);
//TODO FAB BUTTON
FloatingActionButton floatingActionButton =
(FloatingActionButton) findViewById(R.id.fab);
floatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
List<Product> mProductList = new ArrayList<>();
productList.add(mProduct);
if(adapter != null)
adapter.notifyDataSetChanged();
//Handle the empty adapter here
}
});
/* //TODO Add the spinner on item selected listener to get selected items
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
String currentItem = mSpinnerItems.get(position);
String aisleNumber = numberItemValues.get(currentItem);
//TODO you can use the above aisle number to add to your text view
//mTextviews.get(mTextviews.size() -1).setText(aisleNumber);
textView5.setText(aisleNumber);
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// do nothing
}
});*/
//setting adapter to recyclerview
recyclerView.setAdapter(adapter);
}
private class CSVFile {
InputStream inputStream;
public CSVFile(InputStream inputStream) {
this.inputStream = inputStream;
}
public List<String> read() {
// The problem with your adapter is solved now you need to do some work here its giving an error while reading the file
//why is it giving an error because it use to work fine before I added cardview and recyclerview
//Is the file there?
List<String> resultList = new ArrayList<String>();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
try {
String line;
while ((line = reader.readLine()) != null) {
String[] row = line.split(",");
//TODO I edited this part so that you'd add the values in our new hash map variable
numberItemValues.put(row[1], row[0]);
resultList.add(row[1]);
}
} catch (IOException e) {
Log.e("Main", e.getMessage());
} finally {
try {
inputStream.close();
} catch (IOException e) {
Log.e("Main", e.getMessage());
}
}
return resultList;
}
}
}
public class Product {
private static String editText;
private static Boolean checkBox;
private static String textView5;
// The following list is useless unless u don't use it somewhere so lets create getters and setters for this item
public static List<String> spinnerItemsList = new ArrayList<String>();
public Product(List spinner, String editText, Boolean checkBox, String textView5) {
this.editText = editText;
this.spinnerItemsList = spinner;
this.checkBox = checkBox;
this.textView5 = textView5;
}
public static String getEdittext () {
return editText;
}
public static boolean getCheckbox () {
return checkBox;
}
public static String getTextview () {
return textView5;
}
public static List<String> getSpinnerItemsList () {
return spinnerItemsList;
}
}
公共类创建活动{
//存储所有产品的列表
列出产品清单;
//回收视图
回收视图回收视图;
产品MPProduct;
private Map numberItemValues=new HashMap();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.create);
//打开csv
InputStream InputStream=getResources().openRawResource(R.raw.shopitems);
CSVFile CSVFile=新的CSVFile(inputStream);
最终列表mSpinnerItems=csvFile.read();
//从xml获取recyclerview
recyclerView=(recyclerView)findViewById(R.id.recycler\u视图);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(新的LinearLayoutManager(本));
//初始化产品列表
productList=新的ArrayList();
最终微调器微调器;
最终编辑文本编辑文本;
最终复选框;
最终文本视图文本视图5;
微调器=findViewById(R.id.spinner);
editText=findViewById(R.id.editText);
checkBox=findviewbyd(R.id.checkBox);
textView5=findViewById(R.id.textView5);
最终ProductAdapter=新ProductAdapter(此,productList);
//TODO晶圆厂按钮
浮动操作按钮浮动操作按钮=
(浮动操作按钮)findViewById(R.id.fab);
floatingActionButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
List MPProductList=新的ArrayList();
productList.add(MPProduct);
if(适配器!=null)
adapter.notifyDataSetChanged();
//在这里处理空适配器
}
});
/*//TODO在选定项侦听器上添加微调器以获取选定项
spinner.setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView父视图、视图selectedItemView、整型位置、长id){
字符串currentItem=mspinnerietems.get(位置);
字符串aisleNumber=numberItemValues.get(currentItem);
//TODO您可以使用上面的通道号添加到文本视图中
//mTextviews.get(mTextviews.size()-1).setText(aisleNumber);
textView5.setText(aisleNumber);
}
@凌驾
未选择公共无效(AdapterView父视图){
//无所事事
}
});*/
//将适配器设置为recyclerview
recyclerView.setAdapter(适配器);
}
私有类CSVFile{
输入流输入流;
公共CSVFile(InputStream InputStream){
this.inputStream=inputStream;
}
公共列表读取(){
//适配器的问题已经解决,现在您需要在这里做一些工作,因为读取文件时会出现错误
//为什么它会给出一个错误,因为在我添加cardview和recyclerview之前,它可以正常工作
//档案在那儿吗?
List resultList=new ArrayList();
BufferedReader reader=新的BufferedReader(新的InputStreamReader(inputStream));
试一试{
弦线;
而((line=reader.readLine())!=null){
字符串[]行=行。拆分(“,”);
//TODO我编辑了这一部分,以便在新的哈希映射变量中添加值
numberItemValues.put(行[1],行[0]);
结果列表.add(第[1]行);
}
}捕获(IOE异常){
Log.e(“Main”,e.getMessage());
}最后{
试一试{
inputStream.close();
}捕获(IOE异常){
Log.e(“Main”,e.getMessage());
}
}
返回结果列表;
}
}
}
product.java代码
public class create extends AppCompatActivity {
//a list to store all the products
List<Product> productList;
//the recyclerview
RecyclerView recyclerView;
Product mProduct;
private Map<String, String> numberItemValues = new HashMap<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create);
//opens csv
InputStream inputStream = getResources().openRawResource(R.raw.shopitems);
CSVFile csvFile = new CSVFile(inputStream);
final List<String> mSpinnerItems = csvFile.read();
//getting the recyclerview from xml
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
//initializing the productlist
productList = new ArrayList<>();
final Spinner spinner;
final EditText editText;
final CheckBox checkBox;
final TextView textView5;
spinner = findViewById(R.id.spinner);
editText = findViewById(R.id.editText);
checkBox = findViewById(R.id.checkBox);
textView5 = findViewById(R.id.textView5);
final ProductAdapter adapter = new ProductAdapter(this, productList);
//TODO FAB BUTTON
FloatingActionButton floatingActionButton =
(FloatingActionButton) findViewById(R.id.fab);
floatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
List<Product> mProductList = new ArrayList<>();
productList.add(mProduct);
if(adapter != null)
adapter.notifyDataSetChanged();
//Handle the empty adapter here
}
});
/* //TODO Add the spinner on item selected listener to get selected items
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
String currentItem = mSpinnerItems.get(position);
String aisleNumber = numberItemValues.get(currentItem);
//TODO you can use the above aisle number to add to your text view
//mTextviews.get(mTextviews.size() -1).setText(aisleNumber);
textView5.setText(aisleNumber);
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// do nothing
}
});*/
//setting adapter to recyclerview
recyclerView.setAdapter(adapter);
}
private class CSVFile {
InputStream inputStream;
public CSVFile(InputStream inputStream) {
this.inputStream = inputStream;
}
public List<String> read() {
// The problem with your adapter is solved now you need to do some work here its giving an error while reading the file
//why is it giving an error because it use to work fine before I added cardview and recyclerview
//Is the file there?
List<String> resultList = new ArrayList<String>();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
try {
String line;
while ((line = reader.readLine()) != null) {
String[] row = line.split(",");
//TODO I edited this part so that you'd add the values in our new hash map variable
numberItemValues.put(row[1], row[0]);
resultList.add(row[1]);
}
} catch (IOException e) {
Log.e("Main", e.getMessage());
} finally {
try {
inputStream.close();
} catch (IOException e) {
Log.e("Main", e.getMessage());
}
}
return resultList;
}
}
}
public class Product {
private static String editText;
private static Boolean checkBox;
private static String textView5;
// The following list is useless unless u don't use it somewhere so lets create getters and setters for this item
public static List<String> spinnerItemsList = new ArrayList<String>();
public Product(List spinner, String editText, Boolean checkBox, String textView5) {
this.editText = editText;
this.spinnerItemsList = spinner;
this.checkBox = checkBox;
this.textView5 = textView5;
}
public static String getEdittext () {
return editText;
}
public static boolean getCheckbox () {
return checkBox;
}
public static String getTextview () {
return textView5;
}
public static List<String> getSpinnerItemsList () {
return spinnerItemsList;
}
}
公共类产品{
私有静态字符串编辑文本;
私有静态布尔复选框;
私有静态字符串textView5;
//下面的列表是无用的,除非你不在某个地方使用它,所以让我们为这个项目创建getter和setter
公共静态列表spinnerItemsList=新ArrayList();
公共产品(列表微调器、字符串编辑文本、布尔复选框、字符串文本视图5){
this.editText=editText;
this.spinnerItemsList=微调器;
this.checkBox=复选框;
this.textView5=textView5;
}
公共静态字符串getEdittext(){
返回编辑文本;
}
公共静态布尔getCheckbox(){
返回复选框;
}
公共静态字符串getTextview(){
返回文本视图5;
}
公共静态列表getSpinnerItemsList(){
返回喷丝板;
}
}
productadapter.java
public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ProductViewHolder> {
//this context we will use to inflate the layout
private Context mCtx;
private Spinner spinner;
//we are storing all the products in a list
private List<Product> productList;
//getting the context and product list with constructor
public ProductAdapter(Context mCtx, List<Product> productList) {
this.mCtx = mCtx;
this.productList = productList;
}
@Override
public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//inflating and returning our view holder
LayoutInflater inflater = LayoutInflater.from(mCtx);
View view = inflater.inflate(R.layout.layout_products, null);
return new ProductViewHolder(view);
}
@Override
public void onBindViewHolder(ProductViewHolder holder, int position) {
//getting the product of the specified position
Product product = productList.get(position);
//binding the data with the viewholder views
//MyListAdapter adapter = new MyListAdapter(mCtx, R.layout.listrow, R.id.txtid, Product.spinnerItemsList);
//spinner.setAdapter(adapter);
//Try using Simple ArrayAdapter if all you need to display is one text
//Lets see
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String> (mCtx, android.R.layout.simple_spinner_item, Product.getSpinnerItemsList());
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//Did it work?
//yes but when I press the spinner I do not see the items that usually comes up also the layout of Oit Oipen the screen let me take the screen shot
//how do i do that? the screen is open on my phone ok wait click the spinner yes
//There is no item??
//nothing comes up. not even a dropdown. but before i had it like it was populated by items in a csv.
//Okay wait let me get your old code
//also, I added cardviews in my phone but i try to scroll up and I cannot? i thought you could scroll in recyclerview?
//Yes you can scroll it should work
holder.spinner.setAdapter(spinnerArrayAdapter);
}
@Override
public int getItemCount() {
return productList.size();
}
class ProductViewHolder extends RecyclerView.ViewHolder {
Spinner spinner;
EditText editText;
TextView textView5;
CheckBox checkBox;
public ProductViewHolder(View itemView) {
super(itemView);
spinner = itemView.findViewById(R.id.spinner);
editText = itemView.findViewById(R.id.editText);
textView5 = itemView.findViewById(R.id.textView5);
checkBox = itemView.findViewById(R.id.checkBox);
}
}
}
公共类ProductAdapter扩展了RecyclerView.Adapter{
//我们将使用此上下文来扩大布局
私有上下文mCtx;
私人纺纱机;
//我们正在将所有产品储存在一个列表中
私有列表产品列表;
//使用构造函数获取上下文和产品列表
公共产品适配器(上下文mCtx,列表productList){
this.mCtx=mCtx;
this.productList=productList;
mSpinnerSelectedItem.put(position, selectedIndex);