有关listView与Adapter的使用 本帖最后由 lok_le 于 2011-04-30 17:30:20 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这是java文件private ListView mListView; private SimpleAdapter mAdapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // ArrayList<Map<String,Object>> mList= new ArrayList<Map<String, Object>>(); Map<String, Object> map = new HashMap<String, Object>(); map.put("title", "G1"); map.put("info", "google 1"); map.put("img", R.drawable.icon); mList.add(map); map = new HashMap<String, Object>(); map.put("title", "G2"); map.put("info", "google 2"); map.put("img", R.drawable.icon); mList.add(map); map = new HashMap<String, Object>(); map.put("title", "G3"); map.put("info", "google 3"); map.put("img", R.drawable.icon); mList.add(map);; // mAdapter=new SimpleAdapter(this,mList,R.layout.sss,new String[]{"title","info","img"},new int[]{R.id.textview1,R.id.textview2,R.id.imageview1}); mListView.setAdapter(mAdapter); 这是布局文件,一个主窗口,一个ListViewItem的布局主窗口<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" ><TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /><ListView android:id="@+id/listview1" android:layout_height="wrap_content" android:layout_width="match_parent"/></LinearLayout>ListViewItem布局<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ImageView android:id="@+id/imageview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5px"/> <LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFFFFFFF" android:textSize="22px" android:text="textview1"/> <TextView android:id="@+id/textview2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFFFFFFF" android:textSize="13px" android:text="textview2"/> </LinearLayout> </LinearLayout> 今天又做了个LISTVIEW用到SIMPLECURSORVIEW的,发现还是有这种情况。请问有人知道问什么吗??!! private ListView mListView;private SimpleAdapter mAdapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);//mListView必须在R.layout.main布局中 mListView = (ListView)findViewById(R.id.listview1); // ArrayList<Map<String,Object>> mList= new ArrayList<Map<String, Object>>(); Map<String, Object> map = new HashMap<String, Object>(); map.put("title", "G1"); map.put("info", "google 1"); map.put("img", R.drawable.icon); mList.add(map); map = new HashMap<String, Object>(); map.put("title", "G2"); map.put("info", "google 2"); map.put("img", R.drawable.icon); mList.add(map); map = new HashMap<String, Object>(); map.put("title", "G3"); map.put("info", "google 3"); map.put("img", R.drawable.icon); mList.add(map);; // mAdapter=new SimpleAdapter(this,mList,R.layout.sss(该参数为ListViewItem的布局),new String[]{"title","info","img"},new int[]{R.id.textview1,R.id.textview2,R.id.imageview1}); mListView.setAdapter(mAdapter); 是这样,不知到楼主声明的mListView定义了吗?没看到java中对其定义的地方,也就是楼上说的赋值的地方。 谢谢两位的帮助,的确很粗心。可是我还有一个SimpleCursorAdapter的小程序,这次Widget都有定义,可是同样发生了RUNTimeException的异常,能否帮忙查看。我已经通过检测发现错误出在SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.list, mCursor, new String[]{ myHelper.col2_name }, new int[]{ R.id.listTextView1 });这句。因为前面的输出都有输出。下面是简要代码。 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // mEditText=(EditText)findViewById(R.id.edittext1); mListView=(ListView)findViewById(R.id.listview1); // mHelper=new myHelper(this); mCursor=mHelper.select(); //startManagingCursor(mCursor); for(int i=0;i<mCursor.getColumnCount();i++) { System.out.println("Column "+i+":"+mCursor.getColumnName(i)); } // SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.list, mCursor, new String[]{ myHelper.col2_name }, new int[]{ R.id.listTextView1 }); if(adapter!=null)System.out.println("adapter"); mListView.setAdapter(adapter); mListView.setOnItemClickListener (new AdapterView.OnItemClickListener() { @Override public void onItemClick (AdapterView<?> arg0, View arg1, int arg2, long arg3) { System.out.println("onItemClick arg2:"+arg2+" arg3:"+arg3); mCursor.moveToPosition(arg2); _id = mCursor.getInt(0); System.out.println("In ItemClick:"+_id); mEditText.setText(mCursor.getString(1)); } }); mListView.setOnItemSelectedListener (new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected (AdapterView<?> arg0, View arg1, int arg2, long arg3) { System.out.println("onItemSelect arg2:"+arg2+" arg3:"+arg3); SQLiteCursor sc = (SQLiteCursor) arg0.getSelectedItem(); _id = sc.getInt(0); System.out.println("ItemSelected:"+_id); mEditText.setText(sc.getString(1)); } @Override public void onNothingSelected(AdapterView<?> arg0) { } }); } 紧急求助 官方android的SDK无法连接下载了,eclipse里面的ADT用官方的地址也不能下载了 eclipse安装ADT等待巨长时间,然后报错。 android wifi 是如何实现打开wifi后自动连接的 如何学习Android的应用框架? android 菜单制作 Android应用程序运行出错 Android update 无效 求怎么实现微信的伪装定位 udp组播 新人求助!一个很小的app作业 如何将数据库中的数据显示到文本控件中? 请教个初级问题,环境搭建
private ListView mListView;
private SimpleAdapter mAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//
ArrayList<Map<String,Object>> mList= new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>();
map.put("title", "G1");
map.put("info", "google 1");
map.put("img", R.drawable.icon);
mList.add(map);
map = new HashMap<String, Object>();
map.put("title", "G2");
map.put("info", "google 2");
map.put("img", R.drawable.icon);
mList.add(map);
map = new HashMap<String, Object>();
map.put("title", "G3");
map.put("info", "google 3");
map.put("img", R.drawable.icon);
mList.add(map);;
//
mAdapter=new SimpleAdapter(this,mList,R.layout.sss,new String[]{"title","info","img"},new int[]{R.id.textview1,R.id.textview2,R.id.imageview1});
mListView.setAdapter(mAdapter);
主窗口
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<ListView
android:id="@+id/listview1"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
</LinearLayout>
ListViewItem布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/imageview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5px"/>
<LinearLayout android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFFFF"
android:textSize="22px"
android:text="textview1"/>
<TextView android:id="@+id/textview2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFFFF"
android:textSize="13px"
android:text="textview2"/>
</LinearLayout>
</LinearLayout>
请问有人知道问什么吗??!!
private SimpleAdapter mAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);//mListView必须在R.layout.main布局中
mListView = (ListView)findViewById(R.id.listview1);
//
ArrayList<Map<String,Object>> mList= new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>();
map.put("title", "G1");
map.put("info", "google 1");
map.put("img", R.drawable.icon);
mList.add(map);
map = new HashMap<String, Object>();
map.put("title", "G2");
map.put("info", "google 2");
map.put("img", R.drawable.icon);
mList.add(map);
map = new HashMap<String, Object>();
map.put("title", "G3");
map.put("info", "google 3");
map.put("img", R.drawable.icon);
mList.add(map);;
//
mAdapter=new SimpleAdapter(this,mList,R.layout.sss(该参数为ListViewItem的布局),new String[]{"title","info","img"},new int[]{R.id.textview1,R.id.textview2,R.id.imageview1});
mListView.setAdapter(mAdapter);
可是我还有一个SimpleCursorAdapter的小程序,这次Widget都有定义,可是同样发生了RUNTimeException的异常,能否帮忙查看。我已经通过检测发现错误出在
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.list, mCursor, new String[]{ myHelper.col2_name }, new int[]{ R.id.listTextView1 });这句。因为前面的输出都有输出。下面是简要代码。
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//
mEditText=(EditText)findViewById(R.id.edittext1);
mListView=(ListView)findViewById(R.id.listview1);
//
mHelper=new myHelper(this);
mCursor=mHelper.select();
//startManagingCursor(mCursor);
for(int i=0;i<mCursor.getColumnCount();i++)
{
System.out.println("Column "+i+":"+mCursor.getColumnName(i));
}
//
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.list, mCursor, new String[]{ myHelper.col2_name }, new int[]{ R.id.listTextView1 });
if(adapter!=null)System.out.println("adapter");
mListView.setAdapter(adapter); mListView.setOnItemClickListener
(new AdapterView.OnItemClickListener()
{ @Override
public void onItemClick
(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
System.out.println("onItemClick arg2:"+arg2+" arg3:"+arg3);
mCursor.moveToPosition(arg2);
_id = mCursor.getInt(0);
System.out.println("In ItemClick:"+_id);
mEditText.setText(mCursor.getString(1));
} });
mListView.setOnItemSelectedListener
(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected
(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
System.out.println("onItemSelect arg2:"+arg2+" arg3:"+arg3);
SQLiteCursor sc = (SQLiteCursor) arg0.getSelectedItem();
_id = sc.getInt(0);
System.out.println("ItemSelected:"+_id);
mEditText.setText(sc.getString(1));
}
@Override
public void onNothingSelected(AdapterView<?> arg0)
{
}
});
}