gpt4 book ai didi

android - 从与覆盖基本适配器android连接的 ListView 中获取选中的项目

转载 作者:行者123 更新时间:2023-11-29 22:01:28 24 4
gpt4 key购买 nike

我想在 String 数组中获取选中的项目列表。我已将 videoList 作为 ListView ,并用它实现了基本适配器并覆盖了 baseadapter 并使用复选框对其进行了自定义。
我已经试过了,但它给出了 nullpointerexception。

btnplay.setOnClickListener(new View.OnClickListener() 
{
SparseBooleanArray sparseBooleanArray = videolist.getCheckedItemPositions();

@Override
public void onClick(View v)
{

for(int i =0 ; i<videolist.getCount();i++)
{

if(sparseBooleanArray.get(i))
{

String str = (String) videolist.getItemAtPosition(sparseBooleanArray.keyAt(i));
Toast.makeText(getBaseContext(),str , 3).show();
}
}


}
});


public class VideoAdapter extends BaseAdapter
{
private Context vContext;
public VideoAdapter(Context c)
{
vContext = c;
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return count;
}

@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}

@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}

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

System.gc();
LayoutInflater inflater = (LayoutInflater) vContext.getSystemService(vContext.LAYOUT_INFLATER_SERVICE);
View view;
CheckBox cb;
String id = null;
if (convertView == null)
{
view = new View(vContext);

view = inflater.inflate(R.layout.row, null);

cb = (CheckBox) view.findViewById(R.id.checkBox1);


video_column_index = videocursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID);

videocursor.moveToPosition(position);


video_column_index = videocursor.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME);

videocursor.moveToPosition(position);

id = videocursor.getString(video_column_index);

video_column_index = videocursor.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE);

videocursor.moveToPosition(position);

id += " Size(KB):" + videocursor.getString(video_column_index);

cb.setText(id);
} else
view = convertView;
return view;
}

最佳答案

你在这里覆盖了很多重要的功能并且没有给它们实现。

例如,您有:

@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}

当您总是返回 null 时,您如何从 ListView 中检索项目?

也许您希望扩展更完整的适配器实现。尝试使用 ArrayAdapter 而不用空实现覆盖 getItem 等方法。您可以轻松地搜索如何使用 ArrayAdapter

关于android - 从与覆盖基本适配器android连接的 ListView 中获取选中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11806661/

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