gpt4 book ai didi

android - 用户从微调器中选择项目,csv 文件中的相应数字显示在 TextView 中

转载 作者:行者123 更新时间:2023-11-29 23:31:27 25 4
gpt4 key购买 nike

我是这样的,当用户按下 fab 时,一个 cardview 被添加到 recyclerview 布局中。在用户添加的每个卡片 View 中,都有一个微调器、复选框编辑文本和 TextView 。微调器由 csv 文件中的第一列填充。第一列是项目名称,第二列是与该产品相关的特定编号,其中一些可以相同

我以前有一个类似的应用程序,当用户从微调器中选择一个项目时,csv 文件中的对应编号会显示在 TextView 中。但是那没有 recyclerviews 或 cardviews 所以我现在尝试在我的代码中实现它(使用 recyclerview 和 cardviews)

然而,当我一直试图在我当前的 ProductAdapter (recyclerviewadapter) 代码中实现该代码时,我遇到了一个错误:

编译错误:

error: cannot find symbol method getResources() 

它来自这段代码

InputStream inputStream = getResources().openRawResource(R.raw.shopitems);
CSVFile csvFile = new CSVFile(inputStream);
final List<String> mSpinnerItems = csvFile.read();

这是我完整的 ProductAdapter.java 代码

public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ProductViewHolder> {

private Map<Integer, Integer> mSpinnerSelectedItem = new HashMap<Integer, Integer>();

private Map<String, String> numberItemValues = new HashMap<>();





private SearchableSpinner spinner;

//we are storing all the products in a list
private List<Product> productList;

private Activity create;


//TODO CODE FOR CSV FILE


/*InputStream inputStream = getResources().openRawResource(R.raw.shopitems);
CSVFile csvFile = new CSVFile(inputStream);
final List<String> mSpinnerItems = csvFile.read();*/


InputStream inputStream = null;
List<String> mSpinnerItems = null;
CSVFile csvFile = null;


//TODO END OF CODE FOR CSV FILE

public ProductAdapter(Activity activity) {
create = activity;

}


//getting the context and product list with constructor
public ProductAdapter(Activity activity, List<Product> productList) {
// this.mCtx = mCtx;

/* inputStream = create.getResources().openRawResource(R.raw.shopitems);
csvFile = new CSVFile(inputStream);
mSpinnerItems = csvFile.read();*/

create = activity;
this.productList = productList;
}

@Override
public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//inflating and returning our view holder
LayoutInflater inflater = LayoutInflater.from(create);
View view = inflater.inflate(R.layout.layout_products, null);
return new ProductViewHolder(view);
}

@Override
public void onBindViewHolder(final ProductViewHolder holder, final int position) {


// //getting the product of the specified position




ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(create, R.layout.item_spinner_layout,
Product.getSpinnerItemsList());
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

holder.spinner.setAdapter(spinnerArrayAdapter);

holder.spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int mPosition, long id) {
mSpinnerSelectedItem.put(position, mPosition);


TextView mTextView = view.findViewById(R.id.mSpinnerText);


//TODO CODE FOR GETTING AISLE NUMBER AND PUTTING IT IN THE TEXTVIEW
String currentItem = mSpinnerItems.get(position);
String aisleNumber = numberItemValues.get(currentItem);
holder.textView5.setText(aisleNumber);



}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}
});


//binding the data with the viewholder views
if (mSpinnerSelectedItem.containsKey(position)) {
holder.spinner.setSelection(mSpinnerSelectedItem.get(position));
}


holder.getView().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {


AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(create);


// set title
alertDialogBuilder.setTitle("Delete Item");

// set dialog message
alertDialogBuilder
.setMessage("Are you sure you want to delete this item?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, close
// current activity


holder.checkBox.setChecked(false);
holder.spinner.setSelection(0);



productList.remove(holder.getAdapterPosition());
notifyItemRemoved(holder.getAdapterPosition());

Toast.makeText(create, "Item removed.", Toast.LENGTH_LONG).show();


}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});

// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();

// show it
alertDialog.show();

}
});





}


@Override
public int getItemCount() {
return productList.size();
}


class ProductViewHolder extends RecyclerView.ViewHolder {

SearchableSpinner spinner;
EditText editText;
TextView textView5;
CheckBox checkBox;
LinearLayout linearLayout;
View rootView;


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);
rootView = itemView.findViewById(R.id.linearLayout);


checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// makes the set disappear when checkbox is ticked.
if(isChecked){

checkBox.setChecked(false);
spinner.setSelection(0);

productList.remove(getAdapterPosition());
notifyItemRemoved(getAdapterPosition());



Toast.makeText(create, "Done!", Toast.LENGTH_LONG).show();
}

}
});



}

public View getView() {
return rootView;
}

}

//TODO CODE FOR CSV FILE
private class CSVFile {
InputStream inputStream;

public CSVFile(InputStream inputStream) {
this.inputStream = inputStream;
}

public List<String> read() {
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(",");

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;
}
}


}

create.java 代码,这是我添加卡片和 fab 等的主要 Activity

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<>();
private Map<Integer, Integer> mSpinnerSelectedItem = new HashMap<Integer, Integer>();





@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<>();
productList.add(new Product(mSpinnerItems, "Test Edit Text",false, "Text String 2"));




final ProductAdapter adapter = new ProductAdapter(this, productList);

//TODO FAB BUTTON
FloatingActionButton floatingActionButton =
findViewById(R.id.fab);


floatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
productList.add(mProduct);
if(adapter != null)
adapter.notifyDataSetChanged();


//Handle the empty adapter here

}
});









//setting adapter to recyclerview
recyclerView.setAdapter(adapter);






}



private class CSVFile {
InputStream inputStream;

public CSVFile(InputStream inputStream) {
this.inputStream = inputStream;
}

public List<String> read() {
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;
}
}



}

最佳答案

适配器知道 getResources() 的概念。这需要来自 ActivityContext 对象。所以这样做:

InputStream inputStream = null;
List<String> mSpinnerItems = null;
CSVFile csvFile = null;

然后在您的公共(public) ProductAdapter() 方法中正确初始化 InputStream

public ProductAdapter(Activity activity, List<Product> productList) {
create = activity;
//INFO:: Now initialize the InputStream
inputStream = create.getResources().openRawResource(R.raw.shopitems);
csvFile = new CSVFile(inputStream);
mSpinnerItems = csvFile.read();
this.productList = productList;
}

当然,您可以创建一个 string-array 资源并用它填充微调器 View 。

<string-array name="my_values">
<item>Hello</item>
<item>World</item>
<item>Back</item>
<item>Again</item>
</string-array>

然后在布局文件中:

<Spinner 
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:entries="@array/my_values"
/>

.


编辑

使用更新后的代码,您可以删除:

InputStream inputStream = null;
List<String> mSpinnerItems = null;
CSVFile csvFile = null;

来自 ProductAdapter

NullPointerException 错误发生在行:

String currentItem = mSpinnerItems.get(position);

是因为mSpinnerItems不再被使用而被设置为null。因此,为了在微调器中获得选择的值,请执行以下操作:

    holder.spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int mPosition, long id) {
mSpinnerSelectedItem.put(position, mPosition);

TextView mTextView = view.findViewById(R.id.mSpinnerText);


//TODO CODE FOR GETTING AISLE NUMBER AND PUTTING IT IN THE TEXTVIEW
//SearchableSpinner spinner = (SearchableSpinner)view;
String currentItem = holder.spinner.getItemAtPosition(mPosition).toString();
// or try
// spinner.getAdapter.getItem(mPosition).toString();
String aisleNumber = numberItemValues.get(currentItem);
holder.textView5.setText(aisleNumber);
}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}
});

.


新编辑:

ProductAdapter 中使用:

 public ProductAdapter(Activity activity, List<Product> productList, HashMap<String, String> numberList) {
numberItemValues = numberList;
create = activity;
this.productList = productList;
}

并在“创建”Activity 中更改这一行:

final ProductAdapter  adapter = new ProductAdapter(this, productList, numberItemValues);

关于android - 用户从微调器中选择项目,csv 文件中的相应数字显示在 TextView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52599483/

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