Bol*_*rme 18 android listview simplecursoradapter
实际上我已经读过一些关于此的问题......
这是我使用的代码
auto = (ListView)findViewById(R.id.auto);
String[] projection = new String[] {Browser.BookmarkColumns._ID,Browser.BookmarkColumns.TITLE,Browser.BookmarkColumns.URL};
String[] displayFields = new String[] {Browser.BookmarkColumns.TITLE, Browser.BookmarkColumns.URL};
int[] displayViews = new int[] { R.id.text1,R.id.text2 };
Cursor cur = managedQuery(android.provider.Browser.BOOKMARKS_URI,projection, null, null, null);
//auto.setAdapter(new SimpleCursorAdapter(this, R.layout.mylist, cur,displayFields, displayViews));
myAdapter apt = new myAdapter(this, R.layout.mylist, cur,displayFields, displayViews);
auto.setAdapter(apt);
Run Code Online (Sandbox Code Playgroud)
和类myAdapter
class myAdapter extends SimpleCursorAdapter{
private Cursor c;
private Context context;
public myAdapter(Context context, int layout, Cursor c, String[] from,
int[] to) {
super(context, layout, c, from, to);
// TODO Auto-generated constructor stub
this.c = c;
this.context = context;
AutoList att = new AutoList();
mListView = att.auto;
}
@Override
public View getView(int pos, View inView, ViewGroup parent) {
View vix = inView;
if (vix == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
vix = inflater.inflate(R.layout.mylist, null);
}
this.c.moveToPosition(pos);
String title = this.c.getString(this.c.getColumnIndex(Browser.BookmarkColumns.TITLE));
String cont = this.c.getString(this.c.getColumnIndex(Browser.BookmarkColumns.URL));
TextView text1 = (TextView) vix.findViewById(R.id.text1);
text1.setText(title);
TextView text2 = (TextView) vix.findViewById(R.id.text2);
text2.setText(cont);
Button butt = (Button) vix.findViewById(R.id.button);
butt.setOnClickListener(mButt);
return vix;
}
private OnClickListener mButt = new OnClickListener() {
@Override
public void onClick(View v) {
final int position = mListView.getPositionForView((View) v.getParent());
Log.v("BUTT", "Title clicked, row "+position);
}
};
Run Code Online (Sandbox Code Playgroud)
但是,当我点击按钮时,我仍然会收到很多这样的错误:
04-10 22:30:55.152: ERROR/AndroidRuntime(695): FATAL EXCEPTION: main
04-10 22:30:55.152: ERROR/AndroidRuntime(695): java.lang.NullPointerException
04-10 22:30:55.152: ERROR/AndroidRuntime(695): at com.auto2.AutoList$myAdapter$1.onClick(AutoList.java:113)
04-10 22:30:55.152: ERROR/AndroidRuntime(695): at android.view.View.performClick(View.java:2408)
04-10 22:30:55.152: ERROR/AndroidRuntime(695): at android.view.View$PerformClick.run(View.java:8816)
04-10 22:30:55.152: ERROR/AndroidRuntime(695): at android.os.Handler.handleCallback(Handler.java:587)
04-10 22:30:55.152: ERROR/AndroidRuntime(695): at android.os.Handler.dispatchMessage(Handler.java:92)
04-10 22:30:55.152: ERROR/AndroidRuntime(695): at android.os.Looper.loop(Looper.java:123)
04-10 22:30:55.152: ERROR/AndroidRuntime(695): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-10 22:30:55.152: ERROR/AndroidRuntime(695): at java.lang.reflect.Method.invokeNative(Native Method)
04-10 22:30:55.152: ERROR/AndroidRuntime(695): at java.lang.reflect.Method.invoke(Method.java:521)
04-10 22:30:55.152: ERROR/AndroidRuntime(695): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-10 22:30:55.152: ERROR/AndroidRuntime(695): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-10 22:30:55.152: ERROR/AndroidRuntime(695): at dalvik.system.NativeStart.main(Native Method)
Run Code Online (Sandbox Code Playgroud)
而已!我希望不会被弄清楚这么难!
谢谢!
mor*_*_il 19
实际上,我使用了相同的方法 - 只是添加了转换父布局,我得到了没有任何异常的位置
public void deleteButtonClick(View v) {
//TODO Remove favorite - DB + file system
Toast.makeText(this, "Deleting bookmark", Toast.LENGTH_SHORT).show();
final int position = getListView().getPositionForView((LinearLayout)v.getParent());
if (position >= 0) {
Favorite o = (Favorite) this.getListAdapter().getItem(position);
}
}
Run Code Online (Sandbox Code Playgroud)
我对listview行的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.bbox.application"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.markupartist.android.widget.ActionBar
android:id="@+id/actionbar" app:title="@string/ac_title" style="@style/ActionBar" />
<ListView android:id="@+id/android:list" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="5"
android:background="@drawable/categrory_bckgr" />
<TextView android:id="@+id/android:empty"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="@string/main_no_items" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我希望它会有所帮助.
带有可点击按钮的ListView!!!
\n\n嗯......,这是到目前为止解决我的问题的粗略方法......
\n\n项目.xml:
\n\n<?xml version="1.0" encoding="utf-8"?>\n<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"\n android:layout_width="fill_parent"\n android:orientation="horizontal"\n android:layout_height="wrap_content">\n<TextView android:id="@+id/showTv"\n android:layout_alignParentLeft="true"\n android:gravity="center_vertical"\n android:textSize="24dip"\n android:layout_height="wrap_content"\n android:layout_width="wrap_content" /> \n <RelativeLayout android:id="@+id/jjjj"\n android:layout_alignParentRight="true"\n android:layout_height="wrap_content"\n android:layout_width="wrap_content"> \n <Button android:id="@+id/gointoBt"\n android:focusable="false"\n android:layout_alignParentRight="true"\n android:text="abc"\n android:layout_height="wrap_content"\n android:layout_width="wrap_content"/> \n <Button android:id="@+id/chooseBt"\n android:layout_toLeftOf="@id/gointoBt"\n android:layout_marginRight="10dip"\n android:text="text"\n android:focusable="false"\n android:layout_height="wrap_content"\n android:layout_width="wrap_content"/>\n </RelativeLayout> \nRun Code Online (Sandbox Code Playgroud)\n\n\n\nMySimpleAdapter\xef\xbc\x9a
\n\nimport ........;\n\npublic class MySimpleAdapter extends SimpleAdapter {\n\nprivate final Context context;\nprivate List<Map<String, Object>> data;\nprivate int resource;\nprivate String[] from;\nprivate int[] to;\n\npublic MySimpleAdapter(Context context,List<? extends Map<String, ?>> data, int resource, String[] from,\nint[] to) {\n super(context, data, resource, from, to);\n this.context=context;\n this.data=(List<Map<String, Object>>) data;\n this.resource=resource;\n this.from=from;\n this.to=to;\n}\n\n@Override\npublic View getView(int position, View convertView, ViewGroup parent) {\n\n LayoutInflater inflater = ((Activity)context).getLayoutInflater();\n View rowView = inflater.inflate(resource, null, true); \n Map<String, Object> medMap=data.get(position);\n final TextView[] showTv=new TextView[from.length]; \n\n for (int i = 0; i < from.length; i++) { \n showTv[i]=(TextView)rowView.findViewById(to[i]);\n showTv[i].setText(""+medMap.get(from[i]));\n }\nButton btn=(Button)rowView.findViewById(R.id.gointoBt);\n Button.OnClickListener mOkOnClickListener = new Button.OnClickListener()\n {\n public void onClick(View v) {\n Log.v("ttttttt", ""+showTv[0].getText());\n Toast.makeText(context,""+showTv[0].getText(), Toast.LENGTH_LONG).show();\n }\n };\n btn.setOnClickListener(mOkOnClickListener); \n\n Button btn2=(Button)rowView.findViewById(R.id.chooseBt);\n Button.OnClickListener mOkOnClickListener2 = new Button.OnClickListener()\n {\n public void onClick(View v) {\n Log.v("hhhhhhh", ""+showTv[0].getText());\n Toast.makeText(context,"abc"+showTv[0].getText(), Toast.LENGTH_LONG).show();\n }\n };\n btn2.setOnClickListener(mOkOnClickListener2); \n return rowView; \n}\n}\nRun Code Online (Sandbox Code Playgroud)\n\n活动\xef\xbc\x9a
\n\nimport .......;\n\npublic class ActivityMain extends Activity {\n\nListView listview;\nList<Map<String,Object>> data;\n@Override\nprotected void onCreate(Bundle savedInstanceState) {\n super.onCreate(savedInstanceState);\n setTitle("My work");\n prepareData(); \n listview =new ListView(this);\n MySimpleAdapter adapter=new MySimpleAdapter(this,data,R.layout.item,new String[] {"uu"},new int[]{R.id.showTv});\n\n listview.setAdapter(adapter); \n setContentView(listview);\n\n}\nprivate void prepareData(){\n data=new ArrayList<Map<String,Object>>();\n Map<String,Object> item;\n item=new HashMap<String,Object>();\n item.put("uu", "hello");\n data.add(item);\n item=new HashMap<String,Object>();\n item.put("uu", "myyou");\n data.add(item);\n item=new HashMap<String,Object>();\n item.put("uu", "piero");\n data.add(item);\n}\n\n}\nRun Code Online (Sandbox Code Playgroud)\n\n感谢那个人如此友善地提供了这个很棒但最强大的教程......\n这里的任何人都无法将其提供给菜鸟......
\n| 归档时间: |
|
| 查看次数: |
30153 次 |
| 最近记录: |