ListView 自定义 ArrayAdapter - 从 Button OnClickListener 更改文本按钮
ListView Custom ArrayAdapter - Changes textbutton from Button OnClickListener
我想更改按钮 onclicklistener 上的文本按钮,但每 5 行文本按钮显示相同的结果。我只想为单击的 1 个按钮更改文本按钮
xml 布局子视图
<Button
android:id="@+id/btn_seatmap"
style="@style/buttonSeatMap"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/txt_kereta_tiba"
android:layout_toRightOf="@+id/txt_info_seat"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="@string/btn_seatmap" />
xml 布局列表视图
<ListView
android:id="@+id/list_kereta"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/btn_pilih_jadwal"
android:layout_below="@+id/txt_title_jadwal_kai"
android:layout_marginBottom="10dp"
android:background="@android:color/white"
android:choiceMode="singleChoice"
android:dividerHeight="1dp"
android:focusable="true"
android:scrollbars="none" />
这里是来自自定义适配器的代码
@Override
public View getView(final int arg0, View arg1, ViewGroup arg2) {
View view = arg1;
ListHolder listHolder = null;
if (view == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
view = inflater.inflate(layoutResourceId, arg2, false);
listHolder = new ListHolder();
listHolder.layout = (RelativeLayout) view
.findViewById(R.id.layout_jadwal_kai);
listHolder.txtNama = (TextView) view
.findViewById(R.id.txt_nama_kereta);
listHolder.txtBerangkat = (TextView) view
.findViewById(R.id.txt_kereta_brkt);
listHolder.txtHarga = (TextView) view
.findViewById(R.id.txt_harga_kereta);
listHolder.txtTiba = (TextView) view
.findViewById(R.id.txt_kereta_tiba);
listHolder.txtClass = (TextView) view
.findViewById(R.id.txt_kelas_kereta);
listHolder.txtSeat = (TextView) view
.findViewById(R.id.txt_info_seat);
listHolder.btnSeatMap = (Button) view
.findViewById(R.id.btn_seatmap);
CheckSeatOnProgress = false;
view.setTag(listHolder);
} else {
listHolder = (ListHolder) view.getTag();
}
listHolder.btnSeatMap.setTag(listHolder);
listHolder.btnSeatMap.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ListHolder holder = (ListHolder) v.getTag();
holder.btnSeatMap.setText("TEST");
// listHolder.btnSeatMap.setText("TEST");
});
适配器代码
public void refreshListJadwal() {
adapter = new AdapterListKereta(getActivity(),
R.layout.custom_list_jadwalkai, setGetJadwals, dataJadwalKereta);
listKereta.setAdapter(adapter);
}
"TEST" 每 5 行显示一次。
请帮忙。谢谢
如果你想在点击后改变按钮的文本,那么你可以使用 ArrayList。
1) 将位置设置为按钮的标签
2)当点击按钮时,然后找到位置表格标签
3)按位置从创建的ArrayList中获取相应的文本,并将此文本设置为点击视图。
我找到了一个变通办法,虽然不是最佳解决方案 :D 试试这个,
private HashMap<Integer, String> selected = new HashMap<Integer, String>();
@Override
public View getView(final int position, @Nullable View arg1, @NonNull ViewGroup parent) {
View view = arg1;
ListHolder listHolder = null;
if (view == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
view = inflater.inflate(R.layout.list_item, parent, false);
listHolder = new ListHolder();
listHolder.nameTextView = (TextView) view.findViewById(R.id.listitem1);
listHolder.btnSeatMap = (Button) view.findViewById(R.id.listbtn1);
view.setTag(listHolder);
} else {
listHolder = (ListHolder) view.getTag();
}
if(selected != null && !selected.isEmpty() && selected.get(position) != null )
listHolder.btnSeatMap.setText("TEST");
else {
listHolder.btnSeatMap.setText("");
}
listHolder.btnSeatMap.setTag(position);
listHolder.nameTextView.setText(itemsArrayList[position]);
listHolder.btnSeatMap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int which = -1;
Object obj = v.getTag();
if (obj instanceof Integer)
{
which = (Integer) obj;
}
Log.e("POSISITON", which+"");
selected.put(which, "TEST");
notifyDataSetChanged();
}});
return view;
}
我想更改按钮 onclicklistener 上的文本按钮,但每 5 行文本按钮显示相同的结果。我只想为单击的 1 个按钮更改文本按钮
xml 布局子视图
<Button
android:id="@+id/btn_seatmap"
style="@style/buttonSeatMap"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/txt_kereta_tiba"
android:layout_toRightOf="@+id/txt_info_seat"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="@string/btn_seatmap" />
xml 布局列表视图
<ListView
android:id="@+id/list_kereta"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/btn_pilih_jadwal"
android:layout_below="@+id/txt_title_jadwal_kai"
android:layout_marginBottom="10dp"
android:background="@android:color/white"
android:choiceMode="singleChoice"
android:dividerHeight="1dp"
android:focusable="true"
android:scrollbars="none" />
这里是来自自定义适配器的代码
@Override
public View getView(final int arg0, View arg1, ViewGroup arg2) {
View view = arg1;
ListHolder listHolder = null;
if (view == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
view = inflater.inflate(layoutResourceId, arg2, false);
listHolder = new ListHolder();
listHolder.layout = (RelativeLayout) view
.findViewById(R.id.layout_jadwal_kai);
listHolder.txtNama = (TextView) view
.findViewById(R.id.txt_nama_kereta);
listHolder.txtBerangkat = (TextView) view
.findViewById(R.id.txt_kereta_brkt);
listHolder.txtHarga = (TextView) view
.findViewById(R.id.txt_harga_kereta);
listHolder.txtTiba = (TextView) view
.findViewById(R.id.txt_kereta_tiba);
listHolder.txtClass = (TextView) view
.findViewById(R.id.txt_kelas_kereta);
listHolder.txtSeat = (TextView) view
.findViewById(R.id.txt_info_seat);
listHolder.btnSeatMap = (Button) view
.findViewById(R.id.btn_seatmap);
CheckSeatOnProgress = false;
view.setTag(listHolder);
} else {
listHolder = (ListHolder) view.getTag();
}
listHolder.btnSeatMap.setTag(listHolder);
listHolder.btnSeatMap.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ListHolder holder = (ListHolder) v.getTag();
holder.btnSeatMap.setText("TEST");
// listHolder.btnSeatMap.setText("TEST");
});
适配器代码
public void refreshListJadwal() {
adapter = new AdapterListKereta(getActivity(),
R.layout.custom_list_jadwalkai, setGetJadwals, dataJadwalKereta);
listKereta.setAdapter(adapter);
}
"TEST" 每 5 行显示一次。 请帮忙。谢谢
如果你想在点击后改变按钮的文本,那么你可以使用 ArrayList。
1) 将位置设置为按钮的标签
2)当点击按钮时,然后找到位置表格标签
3)按位置从创建的ArrayList中获取相应的文本,并将此文本设置为点击视图。
我找到了一个变通办法,虽然不是最佳解决方案 :D 试试这个,
private HashMap<Integer, String> selected = new HashMap<Integer, String>();
@Override
public View getView(final int position, @Nullable View arg1, @NonNull ViewGroup parent) {
View view = arg1;
ListHolder listHolder = null;
if (view == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
view = inflater.inflate(R.layout.list_item, parent, false);
listHolder = new ListHolder();
listHolder.nameTextView = (TextView) view.findViewById(R.id.listitem1);
listHolder.btnSeatMap = (Button) view.findViewById(R.id.listbtn1);
view.setTag(listHolder);
} else {
listHolder = (ListHolder) view.getTag();
}
if(selected != null && !selected.isEmpty() && selected.get(position) != null )
listHolder.btnSeatMap.setText("TEST");
else {
listHolder.btnSeatMap.setText("");
}
listHolder.btnSeatMap.setTag(position);
listHolder.nameTextView.setText(itemsArrayList[position]);
listHolder.btnSeatMap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int which = -1;
Object obj = v.getTag();
if (obj instanceof Integer)
{
which = (Integer) obj;
}
Log.e("POSISITON", which+"");
selected.put(which, "TEST");
notifyDataSetChanged();
}});
return view;
}