android - 无法在android的 ListView 中获取点击的行号

标签 android listview android-layout android-listview listactivity

我正在尝试构建一个简单的短信应用程序,其编辑页面列出了所有已保存的短信。我在我的布局中为编辑和删除提供了 2 个按钮,但是当我单击它们时,我无法获取 rowid(顶部的行号)来检索与该特定行对应的短信。有人请帮助我。我的短信适配器没有扩展任何东西..这是我的类和 xml 文件..

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的 ListView 中获取点击的行号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4560228/

相关文章:

Android Studio 2.3 AVD 管理器变灰了吗?

java - 如何知道ListView项是否使用两行?

Android:ListView 进行搜索过滤

android - 为什么我的 BottomBar 位于屏幕中间?

android - 为什么我的 float 操作按钮不能正确显示其图像?

android - 应用程序立即打开和关闭,不会崩溃

android - 使用自定义操作 View 时未调用 onOptionsItemSelected

java - 无法使用TabLayout在RelativeLayout中的ImageButton上获得链式 react

java - 如何请求成为 Android 中的默认视频播放器的权限?

android - ListView 项中的 MapView