我的mylist.xml中有两个按钮,使用arrayadapter将其插入main.xml的listview中。它向我显示了应用程序中的数据,但仅单击功能不起作用 无法单击列表和两个按钮。
activity_main.xml
(在这个文件中,我刚刚使用xml创建了listview,将在其中插入我的自定义列表“ mylist.xml”。)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="@+id/lv"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
mylist.xml
(在此文件中,mainactivity上存在的listview lv的所有内容。这些文件需要插入该listview中。)
<?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">
<TextView
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginTop="10dp"
android:id="@+id/s"
android:gravity="center_horizontal"
android:textSize="20dp"
android:textStyle="bold"/>
<TextView
android:layout_width="match_parent"
android:layout_height="25dp"
android:id="@+id/c"
android:textSize="15dp"
android:textStyle="bold"/>
<TextView
android:layout_width="match_parent"
android:layout_height="55dp"
android:id="@+id/d"
android:textSize="15dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="left">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b1"
android:text="UPDATE"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b2"
android:text="DELETE"/>
</LinearLayout>
</LinearLayout>
MainActivity类
public class MainActivity extends AppCompatActivity
{
String S1[]={"hello","1","2"},C1[]={"ello2","1","2"},D1[]={"hello3","3","4"};
Button b1,b2;
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=findViewById(R.id.lv);
b1=findViewById(R.id.b1);
b2=findViewById(R.id.b2);
// display data
CustomAdapter myAdapter=new CustomAdapter(this,S1,C1,D1);
lv.setAdapter(myAdapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this,"ello"+position,Toast.LENGTH_LONG).show();
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"ello",Toast.LENGTH_LONG).show();
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"ello",Toast.LENGTH_LONG).show();
}
});
}
});
}
}
CustomAdapter类
public class CustomAdapter extends ArrayAdapter<String>
{
private final Activity context;
private final String Subject1[],Chapter1[],Details1[];
public CustomAdapter(Activity context,String Subject1[], String Chapter1[], String Details1[])
{
super(context,R.layout.mylist,Subject1);
this.context=context;
this.Subject1=Subject1;
this.Chapter1=Chapter1;
this.Details1=Details1;
}
public View getView(int position, View view, ViewGroup parent)
{
LayoutInflater inflater=context.getLayoutInflater();
view=inflater.inflate(R.layout.mylist, null);
TextView s =view.findViewById(R.id.s);
TextView c= view.findViewById(R.id.c);
TextView d= view.findViewById(R.id.d);
s.setText(Subject1[position]);
c.setText(Chapter1[position]);
d.setText(Details1[position]);
return view;
};
}
答案 0 :(得分:0)
尝试一下:
在MainActivity
中:
public class MainActivity extends AppCompatActivity{
String S1[]={"hello","1","2"},C1[]={"ello2","1","2"},D1[]={"hello3","3","4"};
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=findViewById(R.id.lv);
// display data
CustomAdapter myAdapter=new CustomAdapter(this,S1,C1,D1);
lv.setAdapter(myAdapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// this click work for only on list items not work on list components
// you have to do this functionality in your **CustomAdapter** for list components.
}
});
}
}
现在,在CustomAdapter
中:
public class CustomAdapter extends ArrayAdapter<String>{
private final Activity context;
private final String Subject1[],Chapter1[],Details1[];
public CustomAdapter(Activity context,String Subject1[], String Chapter1[], String Details1[])
{
super(context,R.layout.mylist,Subject1);
this.context=context;
this.Subject1=Subject1;
this.Chapter1=Chapter1;
this.Details1=Details1;
}
public View getView(int position, View view, ViewGroup parent)
{
LayoutInflater inflater=context.getLayoutInflater();
view=inflater.inflate(R.layout.mylist, null);
TextView s =view.findViewById(R.id.s);
TextView c= view.findViewById(R.id.c);
TextView d= view.findViewById(R.id.d);
Button b1 = view.findViewById(R.id.b1);
Button b2 = view.findViewById(R.id.b2);
s.setText(Subject1[position]);
c.setText(Chapter1[position]);
d.setText(Details1[position]);
// here you have to do click thing :
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"HELLO BUTTON 1",Toast.LENGTH_LONG).show();
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"HELLO BUTTON 2",Toast.LENGTH_LONG).show();
}
});
return view;
};
}
答案 1 :(得分:0)
您可以在两个地方放置butonClick。
1:在适配器内部:-
public class CustomAdapter extends ArrayAdapter<String>{
private final Activity context;
private final String Subject1[],Chapter1[],Details1[];
public CustomAdapter(Activity context,String Subject1[], String Chapter1[], String Details1[])
{
super(context,R.layout.mylist,Subject1);
this.context=context;
this.Subject1=Subject1;
this.Chapter1=Chapter1;
this.Details1=Details1;
}
public View getView(int position, View view, ViewGroup parent)
{
LayoutInflater inflater=context.getLayoutInflater();
view=inflater.inflate(R.layout.mylist, null);
TextView s =view.findViewById(R.id.s);
TextView c= view.findViewById(R.id.c);
TextView d= view.findViewById(R.id.d);
Button b1 = view.findViewById(R.id.b1);
Button b2 = view.findViewById(R.id.b2);
s.setText(Subject1[position]);
c.setText(Chapter1[position]);
d.setText(Details1[position]);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Button1 click here
Toast.makeText(getApplicationContext(),"BUTTON 1 Position : "+ position,Toast.LENGTH_LONG).show();
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Button2 click here
Toast.makeText(getApplicationContext(),"BUTTON 2 Position : "+ position,Toast.LENGTH_LONG).show();
}
});
return view;
};
}
2:在您的活动中:-
要在列表视图中放置一个ButtonClick侦听器,请首先使一个侦听器如下:
public interface OnclickListener {
void onButtonClickHere(int position);
}
然后在您的活动中:
ListAdapter adapter;
OnclickListener onclickListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
onclickListener = this;
filterNames = getResources().getStringArray(R.array.finalfilter_names);
filterMethods = getResources().getStringArray(R.array.filter_method_names);
listView = (ListView)
findViewById(R.id.idListView);
adapter = new ListAdapter(groceryList, getApplicationContext(), onclickListener);
}
您可以在此处访问onclick
@Override
public void onButtonClickHere(int position) {
Filter filter = null;
if(position == 0){
filter = FilterPack.getAweStruckVibeFilter(this);
}else if(position == 1){
filter = FilterPack.getMarsFilter(this);
}else if(position == 2){
filter = FilterPack.getStarLitFilter(this);
}
filtered = bitmap.copy(bitmap.getConfig(), true);
// apply filter
imgImage.setImageBitmap(filter.processFilter(filtered));
// struck mars starlit
}
然后在您的适配器中:
将此添加到您的构造函数中
OnclickListener onclickListener;
public ListAdapter(List<ButtonsBean> horizontalGrocderyList, Context context,OnclickListener onclickListener ){
this.buttonsBeanList = horizontalGrocderyList;
this.context = context;
this.onclickListener = onclickListener; // Add this to your constructor
}
和getView()
内
添加此行:
onclickListener.onButtonClickHere(position);
答案 2 :(得分:0)
使用
更新mylist.xml中的父布局。android:descendantFocusability =“ blocksDescendants”
类似以下内容...
<?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:descendantFocusability="blocksDescendants"
android:orientation="vertical">
如果列表的任何行项目包含可聚焦或可单击的视图,则 OnItemClickListener无法正常工作。
我找到了解决方法here
或者,您可以尝试像这样将列表适配器的getView()方法下的onClickListeners设置(将getView()方法的int position参数设为final并在onclick()方法中使用它)。只需将getView()方法复制并粘贴到适配器中即可。
public View getView(final int position, View view, ViewGroup parent)
{
LayoutInflater inflater=context.getLayoutInflater();
view=inflater.inflate(R.layout.mylist, null);
TextView s =view.findViewById(R.id.s);
TextView c= view.findViewById(R.id.c);
TextView d= view.findViewById(R.id.d);
s.setText(Subject1[position]);
c.setText(Chapter1[position]);
d.setText(Details1[position]);
//Do like this
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Do whatever you want here with the position
}
});
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context,"BUTTON1 clicked: position=" + position,Toast.LENGTH_LONG).show();
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context,"BUTTON2 clicked: position=" + position,Toast.LENGTH_LONG).show();
}
});
return view;
};