android 里面我往ListView里面添加数据成功但是ListView里面显示不出来
packagecom.shopping.android;importjava.util.ArrayList;importjava.util.HashMap;importj...
package com.shopping.android;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import com.shopping.android.R;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class Advert extends ShoppingActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.advert);
ListView listview = (ListView) findViewById(R.id.listView1);
List<HashMap<String, Object>> map = new ArrayList<HashMap<String, Object>>();
SimpleAdapter sa = new SimpleAdapter(this, map, R.layout.advert,
new String[] { "img", "text1", "text2" }, new int[] { R.id.ima,
R.id.tv1, R.id.tv2 });
listview.setAdapter(sa);
HashMap<String, Object> hash = new HashMap<String, Object>();
hash.put("img", R.drawable.ic_launcher);
hash.put("text1", "广告1");
hash.put("text2", "广告1广告1广告1");
map.add(hash);
hash = new HashMap<String, Object>();
hash.put("title", "广告2");
hash.put("info", "广告2广告2广告2");
hash.put("img", R.drawable.ic_launcher);
map.add(hash);
hash = new HashMap<String, Object>();
hash.put("title", "广告3");
hash.put("info", "广告3广告3广告3");
hash.put("img", R.drawable.ic_launcher);
map.add(hash);
hash = new HashMap<String, Object>();
hash.put("title", "广告4");
hash.put("info", "广告4广告4广告4");
hash.put("img", R.drawable.ic_launcher);
map.add(hash);
}
}
hash = new HashMap<String, Object>();
hash.put("img", "广告4");
hash.put("text1", "广告4广告4广告4");
hash.put("text2", R.drawable.ic_launcher);
map.add(hash);
SimpleAdapter sa = new SimpleAdapter(this, map, R.layout.advertlist,
new String[] { "img", "text1", "text2" }, new int[] { R.id.ima,
R.id.tv1, R.id.tv2 });
listview.setAdapter(sa);
}
}
放下来了,但是还是只有一条数据啊?求解答 展开
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import com.shopping.android.R;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class Advert extends ShoppingActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.advert);
ListView listview = (ListView) findViewById(R.id.listView1);
List<HashMap<String, Object>> map = new ArrayList<HashMap<String, Object>>();
SimpleAdapter sa = new SimpleAdapter(this, map, R.layout.advert,
new String[] { "img", "text1", "text2" }, new int[] { R.id.ima,
R.id.tv1, R.id.tv2 });
listview.setAdapter(sa);
HashMap<String, Object> hash = new HashMap<String, Object>();
hash.put("img", R.drawable.ic_launcher);
hash.put("text1", "广告1");
hash.put("text2", "广告1广告1广告1");
map.add(hash);
hash = new HashMap<String, Object>();
hash.put("title", "广告2");
hash.put("info", "广告2广告2广告2");
hash.put("img", R.drawable.ic_launcher);
map.add(hash);
hash = new HashMap<String, Object>();
hash.put("title", "广告3");
hash.put("info", "广告3广告3广告3");
hash.put("img", R.drawable.ic_launcher);
map.add(hash);
hash = new HashMap<String, Object>();
hash.put("title", "广告4");
hash.put("info", "广告4广告4广告4");
hash.put("img", R.drawable.ic_launcher);
map.add(hash);
}
}
hash = new HashMap<String, Object>();
hash.put("img", "广告4");
hash.put("text1", "广告4广告4广告4");
hash.put("text2", R.drawable.ic_launcher);
map.add(hash);
SimpleAdapter sa = new SimpleAdapter(this, map, R.layout.advertlist,
new String[] { "img", "text1", "text2" }, new int[] { R.id.ima,
R.id.tv1, R.id.tv2 });
listview.setAdapter(sa);
}
}
放下来了,但是还是只有一条数据啊?求解答 展开
3个回答
展开全部
你代码中的setContentView(R.layout.advert),
SimpleAdapter sa = new SimpleAdapter(this, map, R.layout.advert,
new String[] { "img", "text1", "text2" }, new int[] { R.id.ima,
R.id.tv1, R.id.tv2 })
错误应该在SimpleAdapter构造函数中的第3个参数。
public class MainActivity extends Activity {
List<HashMap<String,Object>> listString;
SimpleAdapter simpleAdapter;
ListView listview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//R.layout.activity_main中含有ListView.
setContentView(R.layout.activity_main);
listString=new ArrayList<HashMap<String,Object>>();
for(int i=1;i<=5;i++){
HashMap<String, Object> map=new HashMap<String, Object>();
map.put("img", R.drawable.png_01);
map.put("text1", "广告"+i);
map.put("text2", "广告"+i+"广告"+i);
listString.add(map);
}
listview=(ListView) findViewById(R.id.listView1);
//R.layout.listview_item.这个指的是ListView中每一项的map,即一行中你显示的imageView,textview1,textview2
//的layout.xml
simpleAdapter=new SimpleAdapter(this, listString, R.layout.listview_item, new String[]{"img","text1","text2"}, new int[]{R.id.imageView1,R.id.TextView1,R.id.TextView2});
listview.setAdapter(simpleAdapter);
}
}
SimpleAdapter sa = new SimpleAdapter(this, map, R.layout.advert,
new String[] { "img", "text1", "text2" }, new int[] { R.id.ima,
R.id.tv1, R.id.tv2 })
错误应该在SimpleAdapter构造函数中的第3个参数。
public class MainActivity extends Activity {
List<HashMap<String,Object>> listString;
SimpleAdapter simpleAdapter;
ListView listview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//R.layout.activity_main中含有ListView.
setContentView(R.layout.activity_main);
listString=new ArrayList<HashMap<String,Object>>();
for(int i=1;i<=5;i++){
HashMap<String, Object> map=new HashMap<String, Object>();
map.put("img", R.drawable.png_01);
map.put("text1", "广告"+i);
map.put("text2", "广告"+i+"广告"+i);
listString.add(map);
}
listview=(ListView) findViewById(R.id.listView1);
//R.layout.listview_item.这个指的是ListView中每一项的map,即一行中你显示的imageView,textview1,textview2
//的layout.xml
simpleAdapter=new SimpleAdapter(this, listString, R.layout.listview_item, new String[]{"img","text1","text2"}, new int[]{R.id.imageView1,R.id.TextView1,R.id.TextView2});
listview.setAdapter(simpleAdapter);
}
}
追问
你好,确实是的,之后我把布局文件改过来了,但是我添加了四条数据,运行之后显示的却只有一条,是怎么回事啊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
是执行顺序的问题。你把适配器和listview放到最下面就行了!
追问
代码在补充问题里面,希望您帮我看一看
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
看一下ListView中的Item项的背景颜色是不是和文字的字体颜色设置成一样的了。
如果是一样的那肯定是显示不出来了。
你先把文字颜色设置成为黑色试试。
如果是一样的那肯定是显示不出来了。
你先把文字颜色设置成为黑色试试。
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |