最近我一直试图用ExpandableListView
重新组织一些菜单,并让它工作,但硬编码。现在我正在寻找一种从strings.xml
获取值的方法,并遇到了一些问题。
这是我的ATM。
这是标题。
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
headersList = new ArrayList<>();
String[] parentHeaders = getResources().getStringArray(R.array.headers);
List<String> parentHeaders = new ArrayList<String>(Arrays.asList(headers));
headersList.addAll(parentHeaders);
HashMap<String, List<String>> allChildItems = returnGroupedChildItems();
expandableListView = (ExpandableListView)findViewById(R.id.expandableListView);
ExpandableListViewAdapter expandableListViewAdapter = new ExpandableListViewAdapter(getApplicationContext(), headersList, allChildItems);
expandableListView.setAdapter(expandableListViewAdapter);
}
而不是那样,我想用我的strings-arrays中的以下片段填充它:
<string-array name = "headers">
<item>@string/parent1</item>
<item>@string/parent2</item>
<item>@string/parent3</item>
</string-array>
最后,我为孩子们准备了这个:
private HashMap<String, List<String>> returnGroupedChildItems()
{
HashMap<String, List<String>> childList = new HashMap<String, List<String>>();
List<String> parent1 = new ArrayList<String>();
String[] child1 = getResources().getStringArray(R.array.parent1);
List<String> childHeaders1 = new ArrayList<String>(Arrays.asList(child1));
parent1.addAll(childHeaders1);
List<String> parent2 = new ArrayList<String>();
String[] child2 = getResources().getStringArray(R.array.parent2);
List<String> childHeaders2 = new ArrayList<String>(Arrays.asList(child2));
parent1.addAll(childHeaders2);
List<String> parent3 = new ArrayList<String>();
String[] child3 = getResources().getStringArray(R.array.parent3);
List<String> childHeaders3 = new ArrayList<String>(Arrays.asList(child3));
parent3.addAll(childHeaders3);
childList.put(headersList.get(0), parent1);
childList.put(headersList.get(1), parent2);
childList.put(headersList.get(2), parent3);
return childList;
}
同样,我有3个其他字符串数组,包含所有这些值。
<string-array name = "parent1">
<item>@string/child1_1</item>
<item>@string/child1_2</item>
<item>@string/child1_3</item>
<item>@string/child1_4</item>
</string-array>
<string-array name = "parent2">
<item>@string/child2_1</item>
<item>@string/child2_2</item>
<item>@string/child2_3</item>
<item>@string/child2_4</item>
</string-array>
<string-array name = "parent3">
<item>@string/child3_1</item>
<item>@string/child3_2</item>
<item>@string/child3_3</item>
<item>@string/child3_4</item>
</string-array>
尝试使用getResources().getStringArray(R.array.parent1)
,但它不起作用。
这也是我的适配器,如果有帮助的话。
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import java.util.HashMap;
import java.util.List;
public class ExpandableListViewAdapter extends BaseExpandableListAdapter
{
private Context context;
private List<String> parentDataSource;
private HashMap<String, List<String>> childDataSource;
public ExpandableListViewAdapter(Context context, List<String> childParent, HashMap<String, List<String>> child)
{
this.context = context;
this.parentDataSource = childParent;
this.childDataSource = child;
}
@Override
public int getGroupCount()
{
return this.parentDataSource.size();
}
@Override
public int getChildrenCount(int groupPosition)
{
return this.childDataSource.get(this.parentDataSource.get(groupPosition)).size();
}
@Override
public Object getGroup(int groupPosition)
{
return parentDataSource.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition)
{
return this.childDataSource.get(parentDataSource.get(groupPosition)).get(childPosition);
}
@Override
public long getGroupId(int groupPosition)
{
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition)
{
return childPosition;
}
@Override
public boolean hasStableIds()
{
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
{
View view = convertView;
if(view == null)
{
LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.parent_layout, parent, false);
}
String parentHeader = (String)getGroup(groupPosition);
TextView parentItem = (TextView)view.findViewById(R.id.parent_layout);
parentItem.setText(parentHeader);
return view;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
{
View view = convertView;
if(view == null)
{
LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.child_layout, parent, false);
}
String childName = (String)getChild(groupPosition, childPosition);
TextView childItem = (TextView)view.findViewById(R.id.child_layout);
childItem.setText(childName);
return view;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition)
{
return true;
}
}
最后,设法把它放在一起。我可以直接从字符串填充我的列表。所以,我的最后一个问题是,如何创建一个循环来检查我的父数组,获取它的名称并使用相应的数组填充每个子项?
编辑3:我的循环上有一些变化。它仍然是硬编码的,但它不需要额外的变量(虽然使用了很多 ifs )。
int size = headersList.size();
for (int i = 1; i <= size; i++)
{
List<String> childList = new ArrayList<>();
if (i == 1)
{
String[] child = getResources().getStringArray(R.array.parent1);
List<String> childHeaders = new ArrayList<>(Arrays.asList(child));
childList.addAll(childHeaders);
} else if (i == 2)
{
String[] child = getResources().getStringArray(R.array.parent2);
List<String> childHeaders = new ArrayList<>(Arrays.asList(child));
childList.addAll(childHeaders);
} else if (i == 3)
{
String[] child = getResources().getStringArray(R.array.parent3);
List<String> childHeaders = new ArrayList<>(Arrays.asList(child));
childList.addAll(childHeaders);
}
childContent.put(headersList.get(i - 1), childList);
}
正如我所说,这是有效的,并使用期望的值填充我的列表,但我想在没有所有条件的情况下执行它(我只需要动态地将每个父#array传递给我的 child String )。
所以,我的最后一个问题是,有没有办法在循环的每次迭代中检索headersList.get(i-1),并在getStringArray()中使用该名称?
答案 0 :(得分:0)
getResources().getStringArray(R.array.parent1)
将返回String []
而不是List<String>
。要将String []
转换为列表,请使用以下
String [] parent1 = getResources().getStringArray(R.array.parent1);
List<String> list = new ArrayList<String>(Arrays.asList(parent1));
答案 1 :(得分:0)
您可以尝试重载构造函数
public ExpandableListViewAdapter(Context context, List<String> childParent, HashMap<String, List<String>> child)
{
this.context = context;
this.parentDataSource = childParent;
this.childDataSource = child;
}
public ExpandableListViewAdapter(Context context, String[] childParent, HashMap<String, String[]> child)
{
this.context = context;
this.parentDataSource = Arrays.asList(childParent);
this.childDataSource = new HashMap<>();
for (Entry<String, String[]> entry : child.entrySet()) {
childDataSource.put(entry.getKey(), Arrays.asList(entry.getValue());
}
}
并回答
但是我想在没有所有条件的情况下这样做(我只需要动态地将每个父#array传递给我的子String)。
所以,我的最后一个问题是,有没有办法在循环的每次迭代中检索headersList.get(i-1),并在getStringArray()中使用该名称?
是的,getResources().getIdentifier
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Resources res = getResources();
// Build Hashmap
Map<String, String[]> allChildItems = new HashMap<>();
String[] headersList = res.getStringArray(R.array.headers);
for (String header : headersList) {
int resId = res.getIdentifier(header, "array", getPackageName());
allChildItems.put(header, res.getStringArray(resId));
}
// Build adapter
ExpandableListViewAdapter expandableListViewAdapter = new ExpandableListViewAdapter(MainActivity.this, headersList, allChildItems);
expandableListView.setAdapter(expandableListViewAdapter);
}
(代码未经测试)