gpt4 book ai didi

android - 复制微调器的问题 - Android Studio

转载 作者:行者123 更新时间:2023-11-29 23:43:12 24 4
gpt4 key购买 nike

我在尝试复制微调器时遇到问题(按下按钮时)。以下是我的 create.java 代码和我的 MyListAdapter.java 代码。我的代码中是否遗漏了一些明显的东西?我一直在尝试追踪穆罕默德的所作所为 here .感谢您的帮助。

创建.java

public class create extends AppCompatActivity {


Button buttontest;
private LinearLayout mLinearLayout;

private ArrayList<Spinner> mSpinners;





@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);




mSpinners = new ArrayList<>();

mLinearLayout = findViewById(R.id.my_linearLayout);

mLinearLayout.addView(makeSpinner()); // First spinner

Button duplicateSpinner = findViewById(R.id.bt_duplicate);
duplicateSpinner.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

Spinner spinner = makeSpinner();
mLinearLayout.addView(spinner); // Add another spinner

}
});

Button getSpinner = findViewById(R.id.bt_getSpinner);
getSpinner.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

for (int i = 0; i < mSpinners.size(); i++) { // Read all spinners
Spinner spinner = mSpinners.get(i);
Log.i("TAG", spinner.getSelectedItem().toString());
}
}
});
}

private Spinner makeSpinner() {


//opens csv
InputStream inputStream = getResources().openRawResource(R.raw.shopitems);
CSVFile csvFile = new CSVFile(inputStream);
List<String> itemList = csvFile.read();
MyListAdapter adapter = new MyListAdapter(this, R.layout.listrow, R.id.txtid, itemList);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setAdapter(adapter);

return spinner;



}

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(",");
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;
}
}}

mylistadapter代码

public class MyListAdapter extends ArrayAdapter<String> {
int groupid;
List<String> items;
Context context;
String path;

public MyListAdapter(Context context, int vg, int id, List<String> items) {
super(context, vg, id, (List<String>) items);
this.context = context;
groupid = vg;
this.items = items;

}

static class ViewHolder {
public TextView textid;
public TextView textname;

}

@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
{





View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(groupid, parent, false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.textid = (TextView) rowView.findViewById(R.id.txtid);
viewHolder.textname = (TextView) rowView.findViewById(R.id.txtname);
rowView.setTag(viewHolder);
}
// Fill data in the drop down.
ViewHolder holder = (ViewHolder) rowView.getTag();
String row = items.get(position);
//holder.textid.setText(row[0]); prints aisle number, dont need

holder.textname.setText(row);


return rowView;
}

}

}

最佳答案

如下更改您的 makeSpinner() 方法:

private Spinner makeSpinner() {
//opens csv
InputStream inputStream = getResources().openRawResource(R.raw.shopitems);
CSVFile csvFile = new CSVFile(inputStream);
List<String> itemList = csvFile.read();

//Create new spinner
Spinner spinner = new Spinner(this, Spinner.MODE_DROPDOWN);

// Setup layout
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
spinner.setLayoutParams(layoutParams);
MyListAdapter adapter = new MyListAdapter(this, R.layout.listrow, R.id.txtid, itemList);

spinner.setAdapter(adapter);

//Add it to your list of spinners so you can retrieve their data when you click the getSpinner button
mSpinners.add(spinner);
return spinner;
}

此外,此 R.id.spinner 目前无用。

编辑:回答评论中的问题:同时添加按钮或复选框

创建复选框方法:

private CheckBox makeCheckbox() {
//Create new Checkbox
CheckBox checkbox = new CheckBox(this);

// Setup layout
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
checkbox.setLayoutParams(layoutParams);
checkbox.setText("YourCheckboxText"); //TODO Change to what you want
return checkbox
}

创建 AppCompatButton 方法:

private AppCompatButton makeButton() {
//Create new Button
AppCompatButton button = new AppCompatButton(this);

// Setup layout
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
button.setLayoutParams(layoutParams);
button.setText("YourButtonText"); //TODO Change to what you want
return button;
}

每当您想添加按钮或复选框时,添加此代码:

    //Add a new button
AppCompatButton newButton = makeButton();
mLinearLayout.addView(newButton); // Add another button

//Add a new checkbox
CheckBox newCheckbox = makeCheckbox();
mLinearLayout.addView(newCheckbox); // Add another checkbox

关于android - 复制微调器的问题 - Android Studio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51701385/

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