LvTags.setAdapter()如何加入public ArrayList<HashMap<String, String>> jList = new ArrayList<HashMap<String, String>>();
时间: 2023-07-09 07:15:32 浏览: 45
要想将 `jList` 中的数据添加到 `LvTags` 中,你需要创建一个 `SimpleAdapter` 对象,并将其设置到 `LvTags` 中。下面是示例代码:
```java
// 创建 SimpleAdapter 对象
SimpleAdapter adapter = new SimpleAdapter(this, jList,
android.R.layout.simple_list_item_1, new String[] { "tag" },
new int[] { android.R.id.text1 });
// 设置适配器
LvTags.setAdapter(adapter);
```
在上面的代码中:
- `this` 表示当前上下文对象;
- `jList` 是你之前创建的 `ArrayList` 对象,其中包含了你要显示的数据;
- `android.R.layout.simple_list_item_1` 是 Android 系统提供的一个简单的列表项布局;
- `new String[] { "tag" }` 表示你要显示哪个字段的数据,这里是 `"tag"`;
- `new int[] { android.R.id.text1 }` 表示你要将数据显示到哪个控件上,这里是 `android.R.id.text1`,也就是列表项布局中的 `TextView` 控件。
你可以根据自己的需求,修改相应的参数。
相关问题
listview的LvTags.setAdapter()如何加入public ArrayList<HashMap<String, String>> jList = new ArrayList<HashMap<String, String>>();
要将 `jList` 中的数据添加到 `ListView` 中,需要进行以下步骤:
1. 创建一个 `SimpleAdapter` 对象,将 `jList` 中的数据绑定到 `ListView` 的控件上。
```java
SimpleAdapter adapter = new SimpleAdapter(this,
jList, // 数据源
android.R.layout.simple_list_item_1, // 列表项布局文件
new String[] { "title" }, // 数据源中的字段名
new int[] { android.R.id.text1 }); // 列表项布局文件中的控件ID
```
2. 将 `adapter` 对象设置到 `ListView` 中。
```java
ListView LvTags = findViewById(R.id.lv_tags);
LvTags.setAdapter(adapter);
```
在上述代码中,`ListView` 的布局文件中应该包含一个 ID 为 `lv_tags` 的 `ListView` 控件,可以通过 `findViewById` 方法获取该控件对象。
`jList` 是一个 `ArrayList` 对象,其中包含多个 `HashMap` 元素,每个 `HashMap` 对象都包含多个键值对,表示列表项中显示的数据。在本示例中,`HashMap` 中只包含一个键值对,键为 `"title"`,值为列表项中显示的文本数据。
如果需要在列表项中显示多个数据,可以在 `HashMap` 中添加多个键值对,同时在 `SimpleAdapter` 构造函数的第四个和第五个参数中添加相应的数据源字段名和控件 ID。
示例代码如下:
```java
SimpleAdapter adapter = new SimpleAdapter(this,
jList, // 数据源
R.layout.list_item, // 列表项布局文件
new String[] { "title", "time" }, // 数据源中的字段名
new int[] { R.id.tv_title, R.id.tv_time }); // 列表项布局文件中的控件ID
```
在上述代码中,`list_item` 是自定义的列表项布局文件,其中包含两个用于显示数据的 `TextView` 控件,分别具有 ID 为 `tv_title` 和 `tv_time`。`SimpleAdapter` 对象的第四个参数为一个字符串数组,包含了两个字段名 `"title"` 和 `"time"`,分别对应 `jList` 中每个 `HashMap` 对象中的键名。第五个参数为一个整型数组,包含了两个控件 ID `R.id.tv_title` 和 `R.id.tv_time`,分别对应列表项布局文件中的两个 `TextView` 控件。
android如何向Fragment传递一个List<HashMap<String, String>>,并能在页面用显示
要向Fragment传递一个List<HashMap<String, String>>,可以使用Bundle对象将数据打包,然后通过setArguments()方法将Bundle对象传递给Fragment。接下来在Fragment中通过getArguments()方法获取传递的Bundle对象,从而获取List<HashMap<String, String>>数据。
下面是示例代码:
在Activity中:
```java
List<HashMap<String, String>> dataList = new ArrayList<>();
// 添加数据到dataList
Bundle bundle = new Bundle();
bundle.putSerializable("dataList", (Serializable) dataList);
MyFragment myFragment = new MyFragment();
myFragment.setArguments(bundle);
```
在Fragment中:
```java
List<HashMap<String, String>> dataList = new ArrayList<>();
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getArguments();
if (bundle != null) {
dataList = (List<HashMap<String, String>>) bundle.getSerializable("dataList");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.my_fragment_layout, container, false);
// 在页面中显示dataList数据
ListView listView = view.findViewById(R.id.list_view);
SimpleAdapter adapter = new SimpleAdapter(getActivity(), dataList, R.layout.list_item_layout,
new String[]{"title", "content"}, new int[]{R.id.title_tv, R.id.content_tv});
listView.setAdapter(adapter);
return view;
}
```
在上述示例代码中,我们使用Bundle对象将List<HashMap<String, String>>数据打包,然后通过setArguments()方法将Bundle对象传递给Fragment。在Fragment中,我们通过getArguments()方法获取传递的Bundle对象,从而获取List<HashMap<String, String>>数据,并在页面中显示数据。这里我们使用了ListView和SimpleAdapter来显示数据,你也可以根据需要使用其他控件和适配器。