我唯一需要的是将TextView
文本发送到所选Activity
项的另一个ListView
。我使用自定义ListView
Adapter
来使用JSON解析从Web服务获取数据。使用我当前的代码,无论我点击哪个项目,我每次都会从第一个ListView
项目中获取文本。
@Override
protected void onPostExecute(Void args) {
ArrayList<String> nameList = new ArrayList<>();
for (Details bean : details) {
nameList.add(bean.getResType());
aero(details);
}
}
}
private void aero(final List<Details> mList) {
for (Details bean : details) {
final String urlChar = bean.getUrl();// may be the problem is here
idno = String.valueOf(bean.getId()); // may be the problem is here
if (bean.getResType().equals("Notes")) {
final ListView listView;
View view = getView();
if (view != null) {
listView = (ListView) view.findViewById(R.id.list2);
CustomListViewAdapterNotes adapter = new CustomListViewAdapterNotes(getContext(),
R.layout.notes, mList);
listView.setAdapter(adapter);
// Assign adapter to ListView
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(getContext(), Main_Activity_for_Notes.class);
//add data to the Intent object
intent.putExtra("idnotes", idno);
intent.putExtra("urlchar", urlChar);
Toast.makeText(getActivity(),
"Click ListItem Number " + idno, Toast.LENGTH_LONG)
.show();
startActivity(intent);
}
});
}
}
}
}
答案 0 :(得分:1)
。随着我的代码,我每次都会得到第一个项目文本,无论如何 我点击的地方。
首先不需要初始化ListView,为ListView设置Adapter,并将setOnItemClickListener添加到for-loop
内的ListView。所以只需从代码中删除for循环。
如果在position
方法内部使用onItemClick方法,则设置点击行的点击项使用onItemClick
参数:
Details bean= mList.get(position);
String urlChar = bean.getUrl();
String idno = String.valueOf(bean.getId());
使用Intent将urlChar
和idno
传递给其他活动。
答案 1 :(得分:1)
更改这些行
Intent intent = new Intent(getContext(), Main_Activity_for_Notes.class);
//add data to the Intent object
intent.putExtra("idnotes", details.get(position).getId());
intent.putExtra("urlchar", details.get(position).getUrl());
Toast.makeText(getActivity(),
"Click ListItem Number " + details.get(position).getId(), Toast.LENGTH_LONG)
.show();
startActivity(intent);