如何在列表视图上制作可点击的项目以及我应该选择什么适配器?
How to make clickable item on listview and what adapter should i choose?
这是脚本:
public class LecturAct extends ListActivity {
private static final String AR_ID = "id_lec";
private static final String AR_LC = "lec_nm";
private static final String AR_CLS = "class";
private static final String EXTRA_DATA = "idr";
JSONArray lec = null;
ArrayList<HashMap<String, String>> lec_list = new ArrayList<HashMap<String, String>>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_lec);
Intent quiz = getIntent();
String id = quiz.getStringExtra(EXTRA_DATA);
String link_url = "http://xxxx/xxxx.php?id="+id;
JSONParser jParser = new JSONParser();
JSONObject json = jParser.gtJson(link_url);
try {
lec = json.getJSONArray("lec");
for(int i = 0; i < lec.length(); i++){
JSONObject ar =lec.getJSONObject(i);
String lectid = ar.getString(AR_ID);
String lecname = ar.getString(AR_LC);
String class = ar.getString(AR_CLS);
HashMap<String, String> map = new HashMap<String, String>();
map.put(AR_ID, lectid);
map.put(AR_LC, lecname);
map.put(AR_CLS, class);
lec_list.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
this.adapter_listview();
}
我试图在我的列表视图上制作一个可点击的项目,这是脚本:
public void adapter_listview() {
ListAdapter adapter = new SimpleAdapter(this, lec_list,
R.layout.list_item,
new String[] { AR_ID, AR_CLS, AR_LC}, new int[] {
R.id.title, R.id.content, R.id.code});
setListAdapter(adapter);
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
String cd = ((TextView) view.findViewById(R.id.code)).getText().toString();
Intent in = new Intent(LecturAct.this, Quis.class);
in.putExtra(AR_ID, cd);
startActivity(in);
}
});
}
但是当我点击一个项目时,它总是把我带到错误的方向,总是把我带到最后一个项目。如果列表中的项目是
- 物理
- 生物学
- 运动
- 社交
- 数学
然后,当我单击生物学或我选择的任何内容时,它总是会引导我进入 "math" 页面。所以我该怎么做?我使用了错误的适配器吗?请帮助我,给我解决方案来解决这个问题:(
例如在LecturAct
中重写protected void onListItemClick (ListView l, View v, int position, long id)
protected void onListItemClick (ListView l, View v, int position, long id) {
String cd = getListAdapter().getItem(position);
Intent in = new Intent(LecturAct.this, Quis.class);
in.putExtra(AR_ID, cd);
startActivity(in);
}
My example like yours.Just change your object casting.Or String.
catalogListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Object obj=(Object)((String) parent.getAdapter().getItem(position));
String ss=obj.toString();
Intent i = new Intent(getActivity(), PdfReader.class);
i.putExtra("catalogPdfUrl", "" +ss;
getActivity().startActivity(i);
}
});
这个答案与阿明的相似,但差别不大。示例代码:
@Override
protected void onListItemClick (ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String mlecture p = (String) l.getItemAtPosition(position);
...
in.putExtra(AR_ID, lecture);
startActivity(in);
}
备注:
- 获取ListView参数(l),获取row/item位置开始。
- 您不能使用 view.findViewById 方法,因为 ListView (l) 已经有 UI 对象。
- 使用覆盖 onListItemClick,因为使用了 ListActivity。
我更仔细地检查了你的代码。我怀疑数据排列不正确
更改自:
new String[] { AR_ID, AR_CLS, AR_LC},
new int[] { R.id.title, R.id.content, R.id.code});
收件人:
new String[] { AR_ID, AR_CLS, AR_LC},
new int[] { R.id.code, R.id.title, R.id.content });
我换了R.id.code。我认为这与 in.putExtra(AR_ID, cd) 兼容。由于我不知道 Quiz.class.
,所以没有好的方法可以确定
这是脚本:
public class LecturAct extends ListActivity {
private static final String AR_ID = "id_lec";
private static final String AR_LC = "lec_nm";
private static final String AR_CLS = "class";
private static final String EXTRA_DATA = "idr";
JSONArray lec = null;
ArrayList<HashMap<String, String>> lec_list = new ArrayList<HashMap<String, String>>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_lec);
Intent quiz = getIntent();
String id = quiz.getStringExtra(EXTRA_DATA);
String link_url = "http://xxxx/xxxx.php?id="+id;
JSONParser jParser = new JSONParser();
JSONObject json = jParser.gtJson(link_url);
try {
lec = json.getJSONArray("lec");
for(int i = 0; i < lec.length(); i++){
JSONObject ar =lec.getJSONObject(i);
String lectid = ar.getString(AR_ID);
String lecname = ar.getString(AR_LC);
String class = ar.getString(AR_CLS);
HashMap<String, String> map = new HashMap<String, String>();
map.put(AR_ID, lectid);
map.put(AR_LC, lecname);
map.put(AR_CLS, class);
lec_list.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
this.adapter_listview();
}
我试图在我的列表视图上制作一个可点击的项目,这是脚本:
public void adapter_listview() {
ListAdapter adapter = new SimpleAdapter(this, lec_list,
R.layout.list_item,
new String[] { AR_ID, AR_CLS, AR_LC}, new int[] {
R.id.title, R.id.content, R.id.code});
setListAdapter(adapter);
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
String cd = ((TextView) view.findViewById(R.id.code)).getText().toString();
Intent in = new Intent(LecturAct.this, Quis.class);
in.putExtra(AR_ID, cd);
startActivity(in);
}
});
}
但是当我点击一个项目时,它总是把我带到错误的方向,总是把我带到最后一个项目。如果列表中的项目是
- 物理
- 生物学
- 运动
- 社交
- 数学
然后,当我单击生物学或我选择的任何内容时,它总是会引导我进入 "math" 页面。所以我该怎么做?我使用了错误的适配器吗?请帮助我,给我解决方案来解决这个问题:(
例如在LecturAct
中重写protected void onListItemClick (ListView l, View v, int position, long id)
protected void onListItemClick (ListView l, View v, int position, long id) {
String cd = getListAdapter().getItem(position);
Intent in = new Intent(LecturAct.this, Quis.class);
in.putExtra(AR_ID, cd);
startActivity(in);
}
My example like yours.Just change your object casting.Or String.
catalogListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Object obj=(Object)((String) parent.getAdapter().getItem(position));
String ss=obj.toString();
Intent i = new Intent(getActivity(), PdfReader.class);
i.putExtra("catalogPdfUrl", "" +ss;
getActivity().startActivity(i);
}
});
这个答案与阿明的相似,但差别不大。示例代码:
@Override
protected void onListItemClick (ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String mlecture p = (String) l.getItemAtPosition(position);
...
in.putExtra(AR_ID, lecture);
startActivity(in);
}
备注:
- 获取ListView参数(l),获取row/item位置开始。
- 您不能使用 view.findViewById 方法,因为 ListView (l) 已经有 UI 对象。
- 使用覆盖 onListItemClick,因为使用了 ListActivity。
我更仔细地检查了你的代码。我怀疑数据排列不正确
更改自:
new String[] { AR_ID, AR_CLS, AR_LC},
new int[] { R.id.title, R.id.content, R.id.code});
收件人:
new String[] { AR_ID, AR_CLS, AR_LC},
new int[] { R.id.code, R.id.title, R.id.content });
我换了R.id.code。我认为这与 in.putExtra(AR_ID, cd) 兼容。由于我不知道 Quiz.class.
,所以没有好的方法可以确定