将数组类型更改为 int 后,ListAdapter 拒绝申请
ListAdapter refuses application after changing array type to int
将列表项的数组类型更改为 'int' 后,我的列表适配器拒绝应用它。我该如何解决这个问题?
ListData.java
public class ListData {
public static final String[][] items = {
{R.string.america,R.string.america_description},
{R.string.europe, R.string.europe_description},
};
}
在FilterListFragment.java内:
private void initialize(View view) {
//Set up list view
mAdapter = new ItemListAdapter(getData(ListData.items), getActivity());
ListView lvItems = (ListView) view.findViewById(R.id.lv_items);
lvItems.setAdapter(mAdapter);
}
(ListData.items)
显示为红色下划线,我收到此错误:
(java.lang.string[]) in FilterListFragment cannot be applied to (int[][])
ItemListadapter.java
public class ItemListAdapter extends BaseAdapter implements Filterable {
private List<String> mData;
private List<String> mFilteredData;
private LayoutInflater mInflater;
private ItemFilter mFilter;
public ItemListAdapter (List<String> data, Context context) {
mData = data;
mFilteredData = data;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return mFilteredData.size();
}
@Override
public String getItem(int position) {
return mFilteredData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
String strItem = mFilteredData.get(position);
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.item_row, parent, false);
holder = new ViewHolder();
holder.mTitle = (TextView) convertView.findViewById(R.id.item_title);
holder.mDescription = (TextView) convertView.findViewById(R.id.item_description);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.mTitle.setText(strItem);
holder.mDescription.setText(strItem);
return convertView;
}
@Override
public Filter getFilter() {
if (mFilter == null) {
mFilter = new ItemFilter();
}
return mFilter;
}
/**
* View holder
*/
static class ViewHolder {
private TextView mTitle;
private TextView mDescription;
}
/**
* Filter for filtering list items
*/
private class ItemFilter extends Filter {
/**
* Invoked on a background thread. This is where all the filter logic should go
* @param constraint the constraint to filter on
* @return the resulting list after applying the constraint
*/
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (TextUtils.isEmpty(constraint)) {
results.count = mData.size();
results.values = mData;
} else {
//Create a new list to filter on
List<String> resultList = new ArrayList<>();
for (String str : mData) {
if (str.toLowerCase().contains(constraint.toString().toLowerCase())) {
resultList.add(str);
}
}
results.count = resultList.size();
results.values = resultList;
}
return results;
}
/**
* Runs on ui thread
* @param constraint the constraint used for the result
* @param results the results to display
*/
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results.count == 0) {
notifyDataSetInvalidated();
} else {
mFilteredData = (ArrayList<String>)results.values;
notifyDataSetChanged();
}
}
}
}
您的 getData
需要一个 String[]
并且您正在 int
上提供一个矩阵。您应该更改其签名,以便接受 int[][]
或更好的 int[]
。如果您不想更改适配器,让 getData
将资源的 id 转换为字符串。例如
public List<String> getData(int[] res) {
ArrayList<String> resStrings = new ArrayList<>();
for (int i : res) {
resStrings.add(getString(i));
}
return resStrings;
}
或者如果您想使用 int[][]
public List<String> getData(int[][] res) {
ArrayList<String> resStrings = new ArrayList<>();
for (int[] i : res) {
for (int j : i) {
resStrings.add(getString(j));
}
}
return resStrings;
}
将列表项的数组类型更改为 'int' 后,我的列表适配器拒绝应用它。我该如何解决这个问题?
ListData.java
public class ListData {
public static final String[][] items = {
{R.string.america,R.string.america_description},
{R.string.europe, R.string.europe_description},
};
}
在FilterListFragment.java内:
private void initialize(View view) {
//Set up list view
mAdapter = new ItemListAdapter(getData(ListData.items), getActivity());
ListView lvItems = (ListView) view.findViewById(R.id.lv_items);
lvItems.setAdapter(mAdapter);
}
(ListData.items)
显示为红色下划线,我收到此错误:
(java.lang.string[]) in FilterListFragment cannot be applied to (int[][])
ItemListadapter.java
public class ItemListAdapter extends BaseAdapter implements Filterable {
private List<String> mData;
private List<String> mFilteredData;
private LayoutInflater mInflater;
private ItemFilter mFilter;
public ItemListAdapter (List<String> data, Context context) {
mData = data;
mFilteredData = data;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return mFilteredData.size();
}
@Override
public String getItem(int position) {
return mFilteredData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
String strItem = mFilteredData.get(position);
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.item_row, parent, false);
holder = new ViewHolder();
holder.mTitle = (TextView) convertView.findViewById(R.id.item_title);
holder.mDescription = (TextView) convertView.findViewById(R.id.item_description);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.mTitle.setText(strItem);
holder.mDescription.setText(strItem);
return convertView;
}
@Override
public Filter getFilter() {
if (mFilter == null) {
mFilter = new ItemFilter();
}
return mFilter;
}
/**
* View holder
*/
static class ViewHolder {
private TextView mTitle;
private TextView mDescription;
}
/**
* Filter for filtering list items
*/
private class ItemFilter extends Filter {
/**
* Invoked on a background thread. This is where all the filter logic should go
* @param constraint the constraint to filter on
* @return the resulting list after applying the constraint
*/
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (TextUtils.isEmpty(constraint)) {
results.count = mData.size();
results.values = mData;
} else {
//Create a new list to filter on
List<String> resultList = new ArrayList<>();
for (String str : mData) {
if (str.toLowerCase().contains(constraint.toString().toLowerCase())) {
resultList.add(str);
}
}
results.count = resultList.size();
results.values = resultList;
}
return results;
}
/**
* Runs on ui thread
* @param constraint the constraint used for the result
* @param results the results to display
*/
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results.count == 0) {
notifyDataSetInvalidated();
} else {
mFilteredData = (ArrayList<String>)results.values;
notifyDataSetChanged();
}
}
}
}
您的 getData
需要一个 String[]
并且您正在 int
上提供一个矩阵。您应该更改其签名,以便接受 int[][]
或更好的 int[]
。如果您不想更改适配器,让 getData
将资源的 id 转换为字符串。例如
public List<String> getData(int[] res) {
ArrayList<String> resStrings = new ArrayList<>();
for (int i : res) {
resStrings.add(getString(i));
}
return resStrings;
}
或者如果您想使用 int[][]
public List<String> getData(int[][] res) {
ArrayList<String> resStrings = new ArrayList<>();
for (int[] i : res) {
for (int j : i) {
resStrings.add(getString(j));
}
}
return resStrings;
}