简体   繁体   English

将Click侦听器添加到ListView中的Buttons

[英]Add click listener to Buttons in ListView

Beginner here. 初学者在这里。 I am using CursorAdapter and ListView . 我正在使用CursorAdapterListView I have two buttons per row(item?). 我每行有两个按钮(项目?)。 I would like to add click listener to them such that I can hide/show the TextViews it the same row. 我想向他们添加点击侦听器,这样我就可以在同一行中隐藏/显示TextViews

Here is my Adapter: 这是我的适配器:

public class ToDoCursorAdapter extends CursorAdapter {
    public ToDoCursorAdapter(Context context, Cursor cursor) {
        super(context, cursor, 0);
    }

    // The newView method is used to inflate a new view and return it,
    // you don't bind any data to the view at this point.
    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        return LayoutInflater.from(context).inflate(R.layout.listview, parent, false);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        TextView engText = (TextView) view.findViewById(R.id.engText);
        TextView arabText = (TextView) view.findViewById(R.id.arabText);
        TextView refText = (TextView) view.findViewById(R.id.refText);

        String arabic = cursor.getString(cursor.getColumnIndexOrThrow("PlainArab_Text"));
        String english = cursor.getString(cursor.getColumnIndexOrThrow("PlainEng_Text"));
        String ref = cursor.getString(cursor.getColumnIndexOrThrow("REF"));

        arabText.setText(arabic);
        engText.setText(english);
        refText.setText(ref);
    }
}

Adapter xml: 适配器xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/button"
            android:layout_width="0sp"
            android:layout_weight="0.5"
            android:layout_height="wrap_content"
            android:text="Left Button"
            />

        <Button
            android:id="@+id/button2"
            android:layout_width="0sp"
            android:layout_weight="0.5"
            android:layout_height="wrap_content"
            android:text="Right Button"
            />
    </LinearLayout>
    <TextView
        android:id="@+id/engText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="10sp"
        android:textSize="15sp"
        android:typeface="serif"/>

    <TextView
        android:id="@+id/arabText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="10sp"
        android:textSize="15sp"
        android:typeface="sans"
        />
    <TextView
        android:id="@+id/refText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="10sp"
        android:textSize="15sp" />
</LinearLayout>

Activity code: 活动代码:

public class ListViewActivity extends Activity {
    ListView listView ;
    private SQLiteDatabase hadlistDB;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listview);

        DatabaseHelper1 hadlistDBHelper;
        hadlistDBHelper = new DatabaseHelper1(this);

        try {
            hadlistDBHelper.createDataBase();
        }
        catch (IOException ioe)
        {
            throw new Error("Unable to create database");
        }
        hadlistDB =  hadlistDBHelper.openDataBase();

        Cursor todoCursor = hadlistDB.rawQuery("SELECT  ID as _id, * FROM HAD_TABLE WHERE ID < 5 ", null);

        // Find ListView to populate
        ListView lvItems = (ListView) findViewById(R.id.list);
// Setup cursor adapter using cursor from last step
        ToDoCursorAdapter todoAdapter = new ToDoCursorAdapter(this, todoCursor);
// Attach cursor adapter to the ListView
        lvItems.setAdapter(todoAdapter);

        todoAdapter.changeCursor(todoCursor);
    }

}

Activity xml: 活动xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Top1" />


        <Button
            android:id="@+id/button4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Top2" />
    </LinearLayout>


    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </ListView>
</LinearLayout>

How do I add the listeners to the buttons in adapter xml? 如何将侦听器添加到适配器xml中的按钮?

Create a button Object on the bindView method you have overridden on the ToDoCursorAdapter class then use the findViewById like you use for the the rest of the text view to initialize the object, After which you need to add an onClickListner to do what you have intended on the code (ie. like hiding the textView) 在您已在ToDoCursorAdapter类上覆盖的bindView方法上创建一个按钮对象,然后像在其余文本视图中一样使用findViewById来初始化该对象,此后,您需要添加onClickListner来执行预期的操作代码(例如,隐藏textView)

public class ToDoCursorAdapter extends CursorAdapter {
public ToDoCursorAdapter(Context context, Cursor cursor) {
    super(context, cursor, 0);
}

// The newView method is used to inflate a new view and return it,
// you don't bind any data to the view at this point.
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    return LayoutInflater.from(context).inflate(R.layout.listview, parent, false);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    TextView engText = (TextView) view.findViewById(R.id.engText);
    TextView arabText = (TextView) view.findViewById(R.id.arabText);
    TextView refText = (TextView) view.findViewById(R.id.refText);
    TextView refText = (TextView) view.findViewById(R.id.refText);
    Button button = (Button) view.findViewById(R.id.button);
    Button button2 = (Button) view.findViewById(R.id.button2);

    String arabic = cursor.getString(cursor.getColumnIndexOrThrow("PlainArab_Text"));
    String english = cursor.getString(cursor.getColumnIndexOrThrow("PlainEng_Text"));
    String ref = cursor.getString(cursor.getColumnIndexOrThrow("REF"));

    arabText.setText(arabic);
    engText.setText(english);
    refText.setText(ref);
    //updated
    arabText.setVisibility(View.VISIBLE);
    engText.setVisibility(View.VISIBLE);

    button.setOnClickListener(new View.OnClickListener() 
         { 
           @Override 
           public void onClick(View v){
           arabText.setVisibility(View.GONE);
         }

      });
    button2.setOnClickListener(new View.OnClickListener() 
        { 
           @Override 
           public void onClick(View v){
           engText.setVisibility(View.GONE);
         }
      });
 }
}

In your bindView() method create Button objects and add click listeners to them 在您的bindView()方法中,创建Button对象并向其添加点击侦听器

 @Override public void bindView(View view, Context context, Cursor cursor) {   
     Button button= (Button) findViewById(R.id.buttonId); button.setOnClickListener(new View.OnClickListener() {   
            @Override public void onClick(View v) {   
              //your code here   
        }   
    });  
}

Create a Callback interface 创建一个回调界面

public interface ButtonCallback{
     void onClickButton(View v);
     void onClickButton2(View v);
}

Your MainActivity implements the Callback interface 您的MainActivity实现了Callback接口

 public class ListViewActivity extends Activity implements ButtonCallback{
   ....
   public void onClickButton(View v){
      // do something
   }

   public void onClickButton2(View v){
      // do something
   }
 }

In your Adapter, set the onclicklistener 在您的适配器中,设置onclicklistener

@Override
public void bindView(View view, Context context, Cursor cursor) {
    TextView engText = (TextView) view.findViewById(R.id.engText);
    TextView arabText = (TextView) view.findViewById(R.id.arabText);
    TextView refText = (TextView) view.findViewById(R.id.refText);
    Button button = (TextView) view.findViewById(R.id.button);
    Button button2 = (TextView) view.findViewById(R.id.button2);

    String arabic = cursor.getString(cursor.getColumnIndexOrThrow("PlainArab_Text"));
    String english = cursor.getString(cursor.getColumnIndexOrThrow("PlainEng_Text"));
    String ref = cursor.getString(cursor.getColumnIndexOrThrow("REF"));

    arabText.setText(arabic);
    engText.setText(english);
    refText.setText(ref);

    button.setOnClickListenter(new View.OnClickListener({
        public void onCLick(View v){
             context.OnClickButton(v);
        }
    });

    button2.setOnClickListenter(new View.OnClickListener({
        public void onCLick(View v){
             context.OnClickButton2(v);
        }
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM