我无法将数据发送到第二个活动。 mainactivity必须向第二个活动发送img url和img title,但不是。 在gridview中我看到了他们两个。 但是给我空数据,我不知道为什么
主要活动:( clicklistener)
// Click event for single list row
gridView.setOnItemClickListener(new OnItemClickListener() {
@SuppressWarnings("unchecked")
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
HashMap<String, String> map2 = (HashMap<String, String>) gridView.getAdapter().getItem(position);
Intent in = new Intent(getApplicationContext(), FullSize.class);
in.putExtra(KEY_THUMB_URL, map2.get(KEY_THUMB_URL));
in.putExtra(KEY_TITLE, map2.get(KEY_TITLE));
startActivity(in);
}
});
LazyAdapter:
public class LazyAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return data.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.grid_row, null);
TextView title = (TextView)vi.findViewById(R.id.title2); // title
ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image2); // thumb image
HashMap<String, String> song = new HashMap<String, String>();
song = data.get(position);
// Setting all values in listview
title.setText(song.get(CustomizedListView.KEY_TITLE));
imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL), thumb_image);
return vi;
}
public ImageLoader getImageLoader() {
return imageLoader;
}
}
完整的主要活动:
public class CustomizedListView extends Activity {
// All static variables
static final String URL = "http://mysite .com/images/rss.xml";
// XML node keys
static final String KEY_SONG = "song"; // parent node
static final String KEY_ID = "id";
static final String KEY_TITLE = "item";
static final String KEY_THUMB_URL = "thumb_url";
GridView gridView;
LazyAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML from URL
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_SONG);
// looping through all song nodes <song>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_ID, parser.getValue(e, KEY_ID));
map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
// adding HashList to ArrayList
songsList.add(map);
}
gridView= (GridView) this.findViewById(R.id.list2);
// Getting adapter by passing xml data ArrayList
adapter=new LazyAdapter(this, songsList);
gridView.setAdapter(adapter);
// Click event for single list row
gridView.setOnItemClickListener(new OnItemClickListener() {
@SuppressWarnings("unchecked")
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
HashMap<String, String> map2 = (HashMap<String, String>) gridView.getAdapter().getItem(position);
Intent in = new Intent(getApplicationContext(), FullSize.class);
in.putExtra(KEY_THUMB_URL, map2.get(KEY_THUMB_URL));
in.putExtra(KEY_TITLE, map2.get(KEY_TITLE));
startActivity(in);
}
});
}
}
答案 0 :(得分:0)
您在第二次Activity
中访问了您的额外错误。密钥不是"KEY_TITLE"
,而是"item"
。删除引号并访问变量KEY_TITLE
。