如何获取可扩展列表适配器中单击元素的位置。
我在共享偏好中存储时间和时间值,我希望将其动态存储在共享偏好中,这样当用户点击适配器中的特定元素时,我可以从时间值中提取这些值和来自共享偏好的时间值。
TimeEditExpandableListAdapter
public class TimeEditExpandableListAdapter extends
BaseExpandableListAdapter{
private Context _context;
private List<String> _listDataHeader; // header titles
ArrayList<String> Mon;
ArrayList<String> Tue;
ArrayList<String> Wed;
ArrayList<String> Thu;
ArrayList<String> Fri;
ArrayList<String> Sat;
ArrayList<String> Sun;
Integer m=0;
Integer t=0;
Integer w=0;
Integer th=0;
Integer f=0;
Integer sa=0;
Integer su=0;
String total = "";
private HashMap<String, List<String>> _listDataChild;
private int mYear, mMonth, mDay, mHour, mMinute;
public TimeEditExpandableListAdapter(Context context, List<String>
listDataHeader, HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition)).get(childPosititon);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition,
childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater)
this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.edittimelist_item,
null);
}
final TextView monday = convertView.findViewById(R.id.mon);
final TextView tuesday = convertView.findViewById(R.id.tue);
final TextView wednesday = convertView.findViewById(R.id.wed);
final TextView thursday = convertView.findViewById(R.id.thu);
final TextView friday = convertView.findViewById(R.id.fri);
final TextView saturday = convertView.findViewById(R.id.sat);
final TextView sunday = convertView.findViewById(R.id.sun);
Mon = new ArrayList<String>();
Tue = new ArrayList<String>();
Wed = new ArrayList<String>();
Thu = new ArrayList<String>();
Fri = new ArrayList<String>();
Sat = new ArrayList<String>();
Sun = new ArrayList<String>();
// CheckBox repeatCHeckBox = convertView.findViewById(R.id.repeatCHeckBox);
final LinearLayout weekNameLayout = convertView.findViewById(R.id.weekNameLayout);
Button from = convertView.findViewById(R.id.from);
final Button to = convertView.findViewById(R.id.to);
// final EditText frommin = convertView.findViewById(R.id.frommin);
from.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager manager = ((AppCompatActivity) _context).getSupportFragmentManager();
Bundle bundle = new Bundle();
bundle.putString("from_or_to", "From");
DialogFragment newFragment = new TimePickerFragment();
newFragment.setArguments(bundle);
newFragment.show(manager, "TimePicker");
}
});
to.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager manager = ((AppCompatActivity) _context).getSupportFragmentManager();
Bundle bundle = new Bundle();
bundle.putString("from_or_to", "To");
DialogFragment newFragment = new TimePickerFragment();
newFragment.setArguments(bundle);
newFragment.show(manager, "TimePicker");
}
});
monday.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(m==0) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(_context);
final String fromtime = sp.getString("fromtime", "");
SharedPreferences sp2 = PreferenceManager.getDefaultSharedPreferences(_context);
final String totime = sp.getString("totime", "");
total = fromtime + '_' + totime;
if(sp.contains("fromtime") && sp2.contains("totime"))
{
Mon.add(total);
for(int i=0;i<Mon.size();i++)
{
Log.i("time",Mon.get(i));
}
monday.setBackgroundResource(R.drawable.circle_bg2);
m=1;
}
else
{
Toast.makeText(_context, "Pick To And From Values", Toast.LENGTH_SHORT).show();
}
}
else
{
while(Mon.remove(total)) {}
monday.setBackgroundResource(0);
m=0;
}
}
});
tuesday.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(t==0) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(_context);
final String fromtime = sp.getString("fromtime", "");
SharedPreferences sp2 = PreferenceManager.getDefaultSharedPreferences(_context);
final String totime = sp.getString("totime", "");
String total = fromtime + '_' + totime;
if(sp.contains("fromtime") && sp2.contains("totime"))
{
Tue.add(total);
Log.i("time",Tue.get(0));
tuesday.setBackgroundResource(R.drawable.circle_bg2);
t=1;
Log.i("ans",total);
}
else
{
Toast.makeText(_context, "Pick To And From Values", Toast.LENGTH_SHORT).show();
}
}
else
{
while(Tue.remove(total)) {}
tuesday.setBackgroundResource(0);
t=0;
}
}
});
wednesday.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(w==0) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(_context);
final String fromtime = sp.getString("fromtime", "");
SharedPreferences sp2 = PreferenceManager.getDefaultSharedPreferences(_context);
final String totime = sp.getString("totime", "");
String total = fromtime + '_' + totime;
if(sp.contains("fromtime") && sp2.contains("totime"))
{
Wed.add(total);
Log.i("time",Wed.get(0));
wednesday.setBackgroundResource(R.drawable.circle_bg2);
w=1;
Log.i("ans",total);
}
else
{
Toast.makeText(_context, "Pick To And From Values", Toast.LENGTH_SHORT).show();
}
}
else
{
while(Wed.remove(total)) {}
wednesday.setBackgroundResource(0);
w=0;
}
}
});
thursday.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(th==0) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(_context);
final String fromtime = sp.getString("fromtime", "");
SharedPreferences sp2 = PreferenceManager.getDefaultSharedPreferences(_context);
final String totime = sp.getString("totime", "");
String total = fromtime + '_' + totime;
if(sp.contains("fromtime") && sp2.contains("totime"))
{
Thu.add(total);
Log.i("time",Thu.get(0));
thursday.setBackgroundResource(R.drawable.circle_bg2);
th=1;
Log.i("ans",total);
}
else
{
Toast.makeText(_context, "Pick To And From Values", Toast.LENGTH_SHORT).show();
}
}
else
{
while(Thu.remove(total)) {}
thursday.setBackgroundResource(0);
th=0;
}
}
});
friday.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(f==0) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(_context);
final String fromtime = sp.getString("fromtime", "");
SharedPreferences sp2 = PreferenceManager.getDefaultSharedPreferences(_context);
final String totime = sp.getString("totime", "");
String total = fromtime + '_' + totime;
if(sp.contains("fromtime") && sp2.contains("totime"))
{
Fri.add(total);
Log.i("time",Fri.get(0));
friday.setBackgroundResource(R.drawable.circle_bg2);
f=1;
Log.i("ans",total);
}
else
{
Toast.makeText(_context, "Pick To And From Values", Toast.LENGTH_SHORT).show();
}
}
else
{
while(Fri.remove(total)) {}
friday.setBackgroundResource(0);
f=0;
}
}
});
saturday.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(sa==0) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(_context);
final String fromtime = sp.getString("fromtime", "");
SharedPreferences sp2 = PreferenceManager.getDefaultSharedPreferences(_context);
final String totime = sp.getString("totime", "");
String total = fromtime + '_' + totime;
if(sp.contains("fromtime") && sp2.contains("totime"))
{
Sat.add(total);
Log.i("time",Sat.get(0));
saturday.setBackgroundResource(R.drawable.circle_bg2);
sa=1;
Log.i("ans",total);
}
else
{
Toast.makeText(_context, "Pick To And From Values", Toast.LENGTH_SHORT).show();
}
}
else
{
while(Sat.remove(total)) {}
saturday.setBackgroundResource(0);
sa=0;
}
}
});
sunday.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(su==0) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(_context);
final String fromtime = sp.getString("fromtime", "");
SharedPreferences sp2 = PreferenceManager.getDefaultSharedPreferences(_context);
final String totime = sp.getString("totime", "");
String total = fromtime + '_' + totime;
if(sp.contains("fromtime") && sp2.contains("totime"))
{
Sun.add(total);
Log.i("time",Sun.get(0));
sunday.setBackgroundResource(R.drawable.circle_bg2);
su=1;
Log.i("ans",total);
}
else
{
Toast.makeText(_context, "Pick To And From Values", Toast.LENGTH_SHORT).show();
}
}
else
{
while(Sun.remove(total)) {}
sunday.setBackgroundResource(0);
su=0;
}
}
});
// repeatCHeckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//
// @Override
// public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// // TODO Auto-generated method stub
// if (buttonView.isChecked()) {
// weekNameLayout.setVisibility(View.VISIBLE);
// } else {
// weekNameLayout.setVisibility(View.GONE);
// }
//
// }
// });
// txtListChild.setText(childText);
return convertView;
}
public ArrayList<String> getMon(){
return Mon;
}
public ArrayList<String> getTue(){
return Tue;
}
public ArrayList<String> getWed(){
return Wed;
}
public ArrayList<String> getThu(){
return Thu;
}
public ArrayList<String> getFri(){
return Fri;
}
public ArrayList<String> getSat(){
return Sat;
}
public ArrayList<String> getSun(){
return Sun;
}
@Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
@Override
public int getGroupCount() {
return this._listDataHeader.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.edittimelist_group, null);
}
TextView lblListHeader = convertView.findViewById(R.id.lblListHeader);
ImageView deleteButton = convertView.findViewById(R.id.deleteButton);
if (isExpanded) {
convertView.findViewById(R.id.expand).setVisibility(View.GONE);
convertView.findViewById(R.id.collapse).setVisibility(View.VISIBLE);
convertView.findViewById(R.id.listExpandLayout).setBackgroundResource(R.drawable.login_border);
lblListHeader.setTextColor(Color.parseColor("#ffffff"));
convertView.findViewById(R.id.timeFillLayout).setVisibility(View.VISIBLE);
deleteButton.setBackgroundResource(R.drawable.ic_deletewhite);
lblListHeader.setVisibility(View.GONE);
} else {
convertView.findViewById(R.id.expand).setVisibility(View.VISIBLE);
convertView.findViewById(R.id.collapse).setVisibility(View.GONE);
convertView.findViewById(R.id.listExpandLayout).setBackgroundColor(Color.parseColor("#C2C2C2"));
lblListHeader.setTextColor(Color.parseColor("#808080"));
convertView.findViewById(R.id.timeFillLayout).setVisibility(View.GONE);
deleteButton.setBackgroundResource(R.drawable.ic_dustbin);
lblListHeader.setVisibility(View.VISIBLE);
}
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}