public class SMSEdit extends ListActivity{ private SMSDbAdapter mDbHelper; //private button editsmsbtn,deletesmsbtn; public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.editsmsList); mDbHelper = new SMSDbAdapter(this); mDbHelper.open(); fillData();} public voID fillData() { Cursor smsCursor = mDbHelper.fetchAllNotes(); startManagingCursor(smsCursor); // Create an array to specify the fIElds we want to display in the List (only Title) String[] from = new String[]{SMSDbAdapter.KEY_Title,SMSDbAdapter.KEY_BODY,SMSDbAdapter.KEY_DATE,SMSDbAdapter.KEY_TIME}; // and an array of the fIElds we want to bind those fIElds to (in this case just text1) int[] to = new int[]{R.ID.smseditListTO,R.ID.smseditListMessage,R.ID.smseditListDay,R.ID.smseditListTime}; // Now create a simple cursor adapter and set it to display SimpleCursorAdapter smsadap = new SimpleCursorAdapter(this,R.layout.smsrow,smsCursor,from,to); setlistadapter(smsadap); } public voID myEditClickHandler(VIEw v) { //get the row the clicked button is in linearLayout vwParentRow = (linearLayout)v.getParent(); button btnChild = (button)vwParentRow.getChildAt(4); btnChild.setText("I've been clicked!"); listadapter pos = getlistadapter(); Intent intent = new Intent(); intent.setClass(SMSEdit.this,SMS.class); // intent.putExtra(SMSDbAdapter.KEY_ROWID,ID); startActivity(intent); finish(); } public voID myDeleteClickHandler(VIEw v) { //get the row the clicked button is in linearLayout vwParentRow = (linearLayout)v.getParent(); //TextVIEw child = (TextVIEw)vwParentRow.getChildAt(0); button btnChild = (button)vwParentRow.getChildAt(5); //btnChild.setText(child.getText()); btnChild.setText("I've been clicked!"); int c = color.CYAN; vwParentRow.setBackgroundcolor(c); vwParentRow.refreshDrawableState(); } }
editsmsList.xml – >>
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content"><ListVIEw androID:ID="@+ID/androID:List" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content"/> <TextVIEw androID:ID="@+ID/androID:empty" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="No Future SMS Yet" androID:textSize="35px"/></linearLayout>
smsrow.xml – >>
<?xml version="1.0" enCoding="utf-8"?> <ScrollVIEw androID:ID="@+ID/ScrollVIEw01" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" xmlns:androID="http://schemas.androID.com/apk/res/androID"><linearLayout androID:ID="@+ID/linearLayout02" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:orIEntation="vertical"> <linearLayout androID:ID="@+ID/linearLayout03" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content"> <TextVIEw androID:layout_height="wrap_content" androID:layout_wIDth="fill_parent" androID:text="@string/smsdetailseditList" androID:ID="@+ID/smseditListTO" androID:paddingleft="5px"> </TextVIEw> <TextVIEw androID:layout_height="wrap_content" androID:text="@string/smsdetailseditList" androID:ID="@+ID/smseditListMessage" androID:paddingleft="20px" androID:maxlines="1" androID:layout_wIDth="wrap_content"> </TextVIEw></linearLayout> <linearLayout androID:ID="@+ID/linearLayout01" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:orIEntation="horizontal" androID:paddingleft="5px"> <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="Date: "></TextVIEw><TextVIEw androID:text="@string/smsdetailseditList" androID:ID="@+ID/smseditListDay" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content"></TextVIEw> <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="Time: " androID:paddingleft="5px"> </TextVIEw><TextVIEw androID:text="@string/smsdetailseditList" androID:ID="@+ID/smseditListTime" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:paddingRight="10px"></TextVIEw> <button androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:ID="@+ID/EditSMSbutton" androID:text="@string/editsms" androID:onClick="@string/myEditClickHandler"></button> <button androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:ID="@+ID/DeleteSMSbutton" androID:text="@string/deletesms" androID:onClick="@string/myDeleteClickHandler"></button> </linearLayout> </linearLayout> </ScrollVIEw>
另外请建议是否有更好的方法来实现这个或使布局看起来更好……
解决方法 你可以像这样扩展一个cursorAdapter ……public class exampleAdapter extends CursorAdapter { LayoutInflater inflater; public exampleAdapter (Context context,Cursor c) { super(context,c); inflater = LayoutInflater.from(context); } @OverrIDe public voID bindVIEw(VIEw vIEw,Context context,Cursor cursor) { // Todo auto-generated method stub button btn_del = (button)vIEw.findVIEwByID(R.ID.button1);btn_del.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { // Todo auto-generated method stub SMSDbAdapter vb = new SMSDbAdapter(context); vb.open(); String xyz = cursor.getString(0); //Persumably your ID vb.Delete(Integer.parseInt(cursor.getString(0))); Toast.makeText(context,xyz + " Deleted!",Toast.LENGTH_SHORT).show(); vb.close(); } }); } @OverrIDe public VIEw newVIEw(Context context,Cursor cursor,VIEwGroup parent) { return inflater.inflate(R.layout.smsrow,parent,false); } }总结
以上是内存溢出为你收集整理的无法在android的列表视图中获取单击的行号全部内容,希望文章能够帮你解决无法在android的列表视图中获取单击的行号所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)