仅获取列表视图的最后一项
Getting only last item of List View
我已经在 ListFragment
中解析了 XML,它工作正常,但 OnListItemClick
只抛出最后一项,我搜索过但没有找到任何东西符合我的情况。
这是里面 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);
}
});
谢谢!
请试试
新闻 = newss.get(位置)
并在您的 onListItemClick 中使用它可能会起作用..
`@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);
}`
我已经在 ListFragment
中解析了 XML,它工作正常,但 OnListItemClick
只抛出最后一项,我搜索过但没有找到任何东西符合我的情况。
这是里面 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);
}
});
谢谢!
请试试 新闻 = newss.get(位置) 并在您的 onListItemClick 中使用它可能会起作用..
`@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);
}`