gpt4 book ai didi

android - 为 ListView 中的按钮添加点击监听器

转载 作者:行者123 更新时间:2023-11-29 19:08:51 25 4
gpt4 key购买 nike

这里是初学者。我正在使用 CursorAdapterListView。我每行有两个按钮(项目?)。我想向它们添加点击监听器,以便我可以在同一行中隐藏/显示 TextViews

这是我的适配器:

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);
}
}

适配器 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 代码:

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 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>

如何将监听器添加到适配器 xml 中的按钮?

最佳答案

在您在 ToDoCursorAdapter 类上重写的 bindView 方法上创建一个按钮对象,然后像您在其余 TextView 中使用的那样使用 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);
}
});
}
}

关于android - 为 ListView 中的按钮添加点击监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46140715/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com