gpt4 book ai didi

android - ListView 中的微调器

转载 作者:行者123 更新时间:2023-11-30 03:55:04 26 4
gpt4 key购买 nike

我有一个 ListView (多选)。它工作完美。我为此使用了自定义适配器。每行都有一个文本和一个复选框。标记的项目将作为结果显示在 TextView 中。

我想在 ListView 行中添加一个微调器。我得到空点异常错误。提前致谢。

public class MainActivity extends Activity {

ListView myListView;
Button getResult;
Spinner spinner;

private ArrayList<String> dayOfWeekList = new ArrayList<String>();

private void initDayOfWeekList(){
dayOfWeekList.add("Sunday");
dayOfWeekList.add("Monday");
dayOfWeekList.add("Tuesday");
dayOfWeekList.add("Wednesday");
dayOfWeekList.add("Thursday");
dayOfWeekList.add("Friday");
dayOfWeekList.add("Saturday");

}

String[] androidBooks =
{
"Hello",
"Professional",
"Unlocking"
};

MyArrayAdapter myArrayAdapter;
ArrayAdapter<String> spinnerAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initDayOfWeekList();
setContentView(R.layout.activity_main);
spinnerAdapter = new ArrayAdapter<String> (this,
android.R.layout.simple_spinner_item, androidBooks);




myListView = (ListView)findViewById(R.id.list);

myArrayAdapter = new MyArrayAdapter(
this,
R.layout.row,
android.R.id.text1,
dayOfWeekList
);

myListView.setAdapter(myArrayAdapter);
myListView.setOnItemClickListener(myOnItemClickListener);

getResult = (Button)findViewById(R.id.getresult);
getResult.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
String result = "";

/*
//getCheckedItemPositions
List<Integer> resultList = myArrayAdapter.getCheckedItemPositions();
for(int i = 0; i < resultList.size(); i++){
result += String.valueOf(resultList.get(i)) + " ";
}
*/

//getCheckedItems
List<String> resultList = myArrayAdapter.getCheckedItems();
for(int i = 0; i < resultList.size(); i++){
result += String.valueOf(resultList.get(i)) + "\n";
}

myArrayAdapter.getCheckedItemPositions().toString();
Toast.makeText(
getApplicationContext(),
result,
Toast.LENGTH_LONG).show();
}});

}

OnItemClickListener myOnItemClickListener
= new OnItemClickListener(){

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
myArrayAdapter.toggleChecked(position);

}};

private class MyArrayAdapter extends ArrayAdapter<String>{

private HashMap<Integer, Boolean> myChecked = new HashMap<Integer, Boolean>();

public MyArrayAdapter(Context context, int resource,
int textViewResourceId, List<String> objects) {
super(context, resource, textViewResourceId, objects);

for(int i = 0; i < objects.size(); i++){
myChecked.put(i, false);
}
}

public void toggleChecked(int position){
if(myChecked.get(position)){
myChecked.put(position, false);
}else{
myChecked.put(position, true);
}

notifyDataSetChanged();
}

public List<Integer> getCheckedItemPositions(){
List<Integer> checkedItemPositions = new ArrayList<Integer>();

for(int i = 0; i < myChecked.size(); i++){
if (myChecked.get(i)){
(checkedItemPositions).add(i);
}
}

return checkedItemPositions;
}

public List<String> getCheckedItems(){
List<String> checkedItems = new ArrayList<String>();

for(int i = 0; i < myChecked.size(); i++){
if (myChecked.get(i)){
(checkedItems).add(dayOfWeekList.get(i));
}
}

return checkedItems;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;

spinner = (Spinner) row.findViewById(R.id.planets_spinner);
spinner.setAdapter(spinnerAdapter);

if(row==null){
LayoutInflater inflater=getLayoutInflater();
row=inflater.inflate(R.layout.row, parent, false);
}

CheckedTextView checkedTextView = (CheckedTextView)row.findViewById(R.id.text1);
checkedTextView.setText(dayOfWeekList.get(position));

Boolean checked = myChecked.get(position);
if (checked != null) {
checkedTextView.setChecked(checked);
}

return row;
}

}

}

最佳答案

你的代码在 getView(..) 中乱序

您正在尝试在膨胀 View 之前检索您的微调器。如果 convertView 为 null(这将是第一次通过您的列表),您的微调器将找不到。您不能在某些东西上调用 findViewById null..将您的操作顺序更改为以下内容

 @Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;

if(row==null){
LayoutInflater inflater=getLayoutInflater();
row=inflater.inflate(R.layout.row, parent, false);
}

spinner = (Spinner) row.findViewById(R.id.planets_spinner);
spinner.setAdapter(spinnerAdapter);
CheckedTextView checkedTextView = (CheckedTextView)row.findViewById(R.id.text1);
checkedTextView.setText(dayOfWeekList.get(position));

Boolean checked = myChecked.get(position);
if (checked != null) {
checkedTextView.setChecked(checked);
}

return row;
}

请注意我如何将检索微调器移动到检查 View 是否为空并在需要时膨胀

关于android - ListView 中的微调器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13475557/

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