我在ListFragment
内部解析了一个XML,它运行正常,但是OnListItemClick
只抛出了最后一项,我搜索了但是我找不到任何符合我案例的内容。< / p>
这是onPostExecute
:
XMLDOMParser parser = new XMLDOMParser();
InputStream stream = new ByteArrayInputStream(xml.getBytes());
Document doc = parser.getDocument(stream);
NodeList nodeList = doc.getElementsByTagName(KEY_ITEM);
newss = new ArrayList<News>();
news = null;
for (int i = 0; i < nodeList.getLength(); i++) {
news = new News();
Element e = (Element) nodeList.item(i);
news.setId(parser.getValue(e, KEY_ID));
news.setDate(parser.getValue(e, KEY_DATE));
news.setTitle(parser.getValue(e, KEY_TITLE));
news.setDescription(parser.getValue(e, KEY_DESC));
newss.add(news);
}
listViewAdapter = new NewsAdapter(getActivity(), newss);
getListView().setAdapter(listViewAdapter);
pDialog.dismiss();
位于同一片段内的 OnItemClick
:
`@Override
public void onListItemClick(ListView l, View v, int position, long id) {
Toast.makeText(getActivity(), "List: "+news.getId(), Toast.LENGTH_SHORT).show();
Intent in = new Intent(getActivity(), SingleNewsActivity.class);
in.putExtra(KEY_ID, news.getId());
in.putExtra(KEY_DATE, news.getDate());
in.putExtra(KEY_TITLE, news.getTitle());
in.putExtra(KEY_DESC, news.getDescription());
startActivity(in);
}`
我的NewsAdapter
:
public class NewsAdapter extends BaseAdapter {
Activity context;
List<News> newss;
public NewsAdapter (Activity context, List<News> newss) {
this.context = context;
this.newss = newss;
}
// private View Holder class
private class ViewHolder {
TextView tvNewsDate;
TextView TvNewsTitle;
}
public int getCount() {
return newss.size();
}
public Object getItem(int position) {
return newss.get(position);
}
public long getItemId(int position) {
return newss.indexOf(getItem(position));
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater inflater = context.getLayoutInflater();
if (convertView == null) {
convertView = inflater.inflate(R.layout.news_item, null);
holder = new ViewHolder();
holder.tvNewsDate = (TextView) convertView.findViewById(R.id.tvNewsDate);
holder.TvNewsTitle = (TextView) convertView.findViewById(R.id.tvNewsTitle);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
News newss = (News) getItem(position);
holder.tvNewsDate.setText(newss.getDate());
holder.TvNewsTitle.setText(newss.getTitle());
return convertView;
}
}`
ImageView
setOnClickListner
在我的另一个适配器中有同样的问题:
holder.ivGPS.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Uri gmmIntentUri = Uri.parse("google.navigation:q=" + parkCoordinates);
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
context.startActivity(mapIntent);
}
});
谢谢!
答案 0 :(得分:0)
请尝试 news = newss.get(position) 并在你的onListItem上使用它可能有效..
`@Override
public void onListItemClick(ListView l, View v, int position, long id) {
news = newss.get(position)
Intent in = new Intent(getActivity(), SingleNewsActivity.class);
in.putExtra(KEY_ID, news.getId());
in.putExtra(KEY_DATE, news.getDate());
in.putExtra(KEY_TITLE, news.getTitle());
in.putExtra(KEY_DESC, news.getDescription());
startActivity(in);
}`