列表视图使用的TextView和Button。点击的按钮中的RowId [英] Listview with TextView and Button. RowId of the clicked button

查看:129
本文介绍了列表视图使用的TextView和Button。点击的按钮中的RowId的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我哈瓦类似下面的列表视图。在TextView中的文本来自数据库。

I hava a listview like the following. The text in the TextView comes from a database.

-------------------------
TextView          Button
-------------------------

当我按一下按钮,我想显示该行举杯TextView的文本。

When I click on the button, I want to show the text in the TextView of this row in a Toast.

我的问题如下::当我按一下按钮,我显示了该行,这是用光标选择的文本。我不显示其中的按钮是行的文本。我知道这个问题是mCursor变量。

My question is the following: When I click on the button, I am showing the text of the row, which is selected by the cursor. I am not showing the text of the row where the button is. I know the problem is the mCursor variable.

我不知道如何解决它。有没有人一个想法?

I don't know how to fix it. Has anybody an idea?

这是我的ModulCursorAdapter:

public class ModuleCursorAdapter extends ResourceCursorAdapter implements OnClickListener {
private Context mContext;
private Cursor mCursor;

    public ModuleCursorAdapter(Context context, Cursor cur) {
        super(context, R.layout.notes_row, cur);
        this.mContext = context;

    }

    @Override
    public View newView(Context context, Cursor cur, ViewGroup parent) {
        LayoutInflater li = LayoutInflater.from(context);           
        return li.inflate(R.layout.notes_row, parent, false);
    }



    @Override
    public void bindView(View view, Context context, Cursor cur) {  
        this.mCursor = cur;

        TextView tvText1 = (TextView)view.findViewById(R.id.text1);            
        Button btnButtonOFF = (Button)view.findViewById(R.id.buttonOFF);

        tvText1.setText(cur.getString(cur.getColumnIndex(NotesDbAdapter.KEY_TITLE)));

        int idRow = cur.getColumnIndex(NotesDbAdapter.KEY_ROWID);
        btnButtonOFF.setTag(cur.getInt(idRow));
        btnButtonOFF.setOnClickListener(btnButtonOFFclicked);           

    }
    @Override
    public void onClick(View view) {

    }       

    private OnClickListener btnButtonOFFclicked = new OnClickListener() {
        @Override
        public void onClick(View view) {                
            Toast.makeText(mContext,mCursor.getString(mCursor.getColumnIndex(NotesDbAdapter.KEY_TITLE)), Toast.LENGTH_LONG).show();                 
        }
    };
}

注释

在XML的文件中的按钮:

The Button in the xml-File:

<Button android:id="@+id/buttonOFF" 
        android:gravity="center"
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content"
        android:layout_alignParentRight="true" 
        android:focusable="false"
        android:text="OFF" />

 <Button android:id="@+id/buttonOFF2" 
        android:gravity="center"
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content"
        android:layout_alignParentRight="true" 
        android:focusable="false"
        android:onClick="btnButtonOFFclicked"
        android:text="ON" />

的OnClickMethod在MainActivity

 public class MainActivity extends ListActivity {
 .....
 public void btnButtonOFFclicked(View view) 
  {          
     Toast.makeText(MainActivity.this,"Test", Toast.LENGTH_LONG).show();
     //Toast.makeText(mContext,mCursor.getString(mCursor.getColumnIndex(NotesDbAdapter.KEY_TITLE)), Toast.LENGTH_LONG).show();              
  }
}

MainActivity

public class MainActivity extends ListActivity {
...
private void fillData() {
    Cursor notesCursor = mDbHelper.fetchAllNotes();
    startManagingCursor(notesCursor);

   adapter = new ModuleCursorAdapter(this, notesCursor);
   setListAdapter(adapter);
}
...
}

的Notes.DbAdapter.fetchAllNotes()方法

public Cursor fetchAllNotes() {
    return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE,
            KEY_DEVICETYPE, KEY_HOMECODE, KEY_DEVICECODE}, null, null, null, null, null);
}

感谢。

菲利克斯

推荐答案

只需创建一个私有类里面你适配器这样的,并在bindView方法按钮设置监听其会像

Just create a private class inside you adapter like this and set listener on button in bindView method its will be like

public void bindView(View view, Context context, Cursor cur) {  
    this.mCursor = cur;

    TextView tvText1 = (TextView)view.findViewById(R.id.text1);            
    Button btnButtonOFF = (Button)view.findViewById(R.id.buttonOFF);

    tvText1.setText(cur.getString(cur.getColumnIndex(NotesDbAdapter.KEY_TITLE)));

    int idRow = cur.getColumnIndex(NotesDbAdapter.KEY_ROWID);
    btnButtonOFF.setOnClickListener(new OnItemClickListener(cur.getInt(idRow)));           

}

private class OnItemClickListener implements OnClickListener {
    private int position;


    public OnItemClickListener(int position) {
        super();
        this.position = position;
    }


    @Override
    public void onClick(View v) {
        // position is an id.
    }
}

这篇关于列表视图使用的TextView和Button。点击的按钮中的RowId的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆