ラジオボタンの項目を持つリストビューがあります。Androidのlistviewadapterラジオボタンの最初の項目をチェックし、項目をクリックすると、最初の項目のチェックが外されます
ここに私の断片のコードがあります。
ListViewAdapterShipping.AdapterInterface listener = new ListViewAdapterShipping.AdapterInterface() {
@Override
public void onClick(String value, String price, String symbol, String name, String desc, String shipicon, String weight, String module){
textShippingRateId.setText(value);
textShippingRate.setText(price);
textSymbol.setText(symbol);
textNameName.setText(name);
textDescDesc.setText(desc);
textShipIcon.setText(shipicon);
textShipWeight.setText(weight);
textShipModule.setText(module);
}
};
shippingrateadapter = new ListViewAdapterShipping(getActivity(), shippingratearraylist, listener);
shippingratelistview.setAdapter(shippingrateadapter);
shippingratelistview.setExpanded(true);
最初の読み込み時に最初の項目を選択します。ユーザーが他の項目をクリックすると、最初と他の項目はチェックされなくなります。
ここは自分のアダプターです。
package com.example.administrator.mosbeau;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.StringTokenizer;
public class ListViewAdapterShipping extends BaseAdapter {
boolean expanded = false;
Context context;
LayoutInflater inflater;
ArrayList<HashMap<String, String>> data;
HashMap<String, String> resultp = new HashMap<String, String>();
AdapterInterface listener;
public interface AdapterInterface
{
void onClick(String value, String price, String symbol, String name, String desc, String shipicon, String weight, String module);
}
public ListViewAdapterShipping(Context context,
ArrayList<HashMap<String, String>> arraylist, AdapterInterface listener) {
this.context = context;
this.listener = listener;
data = arraylist;
}
String mconfiguration_id, mshipping_price, mshipping_symbol, mshipping_title, mshipping_desc, mshipping_icon, mmshipping_icon, mshipping_weight, mmshipping_weight, mshipping_module;
private RadioButton mSelectedRB;
private int mSelectedPosition = 0;
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
public View getView(final int position, View convertView, ViewGroup parent) {
TextView configuration_id;
RadioButton shipping_title;
TextView shipping_weight;
ImageView shipping_icon;
TextView shipping_price;
TextView shipping_symbol;
TextView shipping_desc;
TextView shippingicon;
TextView shippingmodule;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.shippingrate_item, parent, false);
resultp = data.get(position);
configuration_id = (TextView) itemView.findViewById(R.id.textconfigurationid);
shipping_title = (RadioButton) itemView.findViewById(R.id.radioShippingtitle);
shipping_weight = (TextView) itemView.findViewById(R.id.textWeight);
shipping_icon = (ImageView) itemView.findViewById(R.id.shipping_icon);
shipping_price = (TextView) itemView.findViewById(R.id.textPrice);
shipping_symbol = (TextView) itemView.findViewById(R.id.textSymbol);
shipping_desc = (TextView) itemView.findViewById(R.id.textDesc);
shippingicon = (TextView) itemView.findViewById(R.id.textshippingicon);
shippingmodule = (TextView) itemView.findViewById(R.id.textshippingmodule);
configuration_id.setText(resultp.get(CheckoutFragment1.configuration_id));
shipping_title.setText(resultp.get(CheckoutFragment1.shipping_title));
shipping_weight.setText(resultp.get(CheckoutFragment1.shipping_weight));
shipping_price.setText(resultp.get(CheckoutFragment1.shipping_price));
shipping_symbol.setText(resultp.get(CheckoutFragment1.shipping_symbol));
shipping_desc.setText(resultp.get(CheckoutFragment1.shipping_desc));
shippingicon.setText(resultp.get(CheckoutFragment1.shipping_icon));
shippingmodule.setText(resultp.get(CheckoutFragment1.shipping_module));
Glide.with(context).load(resultp.get(CheckoutFragment1.shipping_icon)).diskCacheStrategy(DiskCacheStrategy.ALL).into(shipping_icon);
int color = 0xffffffff;
itemView.setBackgroundColor(color);
mconfiguration_id = configuration_id.getText().toString();
mshipping_price = shipping_price.getText().toString();
mshipping_symbol = shipping_symbol.getText().toString();
mshipping_title = shipping_title.getText().toString();
mshipping_desc = shipping_desc.getText().toString();
mshipping_icon = shippingicon.getText().toString();
mshipping_weight = shipping_weight.getText().toString();
mshipping_module = shippingmodule.getText().toString();
if(mshipping_icon.matches("")){
mmshipping_icon = "null";
}else{
mmshipping_icon = mshipping_icon;
}
if(mshipping_weight.matches("")){
mmshipping_weight = "null";
}else{
mmshipping_weight = mshipping_weight;
}
shipping_title.setTag(mconfiguration_id + "#" + mshipping_price + "#" + mshipping_symbol + "#" + mshipping_title + "#" + mshipping_desc + "#" + mmshipping_icon + "#" + mmshipping_weight + "#" +mshipping_module);
shipping_title.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (position != mSelectedPosition && mSelectedRB != null) {
mSelectedRB.setChecked(false);
}
mSelectedPosition = position;
mSelectedRB = (RadioButton) v;
Log.e("mSelectedRB",Integer.toString(mSelectedPosition));
String CurrentString;
CurrentString = v.getTag().toString();
StringTokenizer tokens = new StringTokenizer(CurrentString, "#");
String mconfiguration_id = tokens.nextToken();
String mshipping_price = tokens.nextToken();
String mshipping_symbol = tokens.nextToken();
String mshipping_title = tokens.nextToken();
String mshipping_desc = tokens.nextToken();
String mmshipping_icon = tokens.nextToken();
String mmshipping_weight = tokens.nextToken();
String mshipping_module = tokens.nextToken();
if(listener != null)
listener.onClick(mconfiguration_id, mshipping_price, mshipping_symbol, mshipping_title, mshipping_desc, mmshipping_icon, mmshipping_weight, mshipping_module);
}
});
if(mSelectedPosition != position){
shipping_title.setChecked(false);
}else{
shipping_title.setChecked(true);
if(mSelectedRB != null && shipping_title != mSelectedRB){
mSelectedRB = shipping_title;
}
}
return itemView;
}
}
あなたはprivate int mSelectedPosition = 0;
がゼロに設定されている私のアダプタで見ることができるように、これは最初の項目がチェックされることを意味します。しかし、私は他のアイテムをクリックします。最初の項目はまだチェックされています。
ないので、効率的な 'ListView.invalidateViews'より効率的な:あなたがポジションを選択した設定で行われた後にOnclickで – Selvin
「リストビューで単一の行を再描画」のためのgoogle notifydatasetchangedを試してください。 –
@ shreyashmashruありがとう.. – Joehamir