在我的应用程序中,如果我们点击邀请,我必须将活动显示为邀请,否则可能会显示按钮,如果我们点击任何按钮,按钮的颜色应该改变,它应该永远保持颜色直到我们改变回应(是的,否则)。在这里我发布了我的代码
public class InvitationFragment extends Fragment {
LayoutInflater inflater;
ViewGroup container;
Bundle savedInstanceState;
private List<String> eventName = new ArrayList<>();
private List<Long> eventId = new ArrayList<>();
private List<String> eventPlace = new ArrayList<>();
private List<EventMO> eventMOs = new ArrayList<>();
private List<Long> eventUserId = new ArrayList<>();
private Context context;
private UserOccasions userOccasions;
private UserDelegate userDelegate = new UserDelegate();
private EventDelegates eventDelegates = new EventDelegates();
private Gson gson = new Gson();
private ProgressDialog prgDialog;
private UserMO userMO;
private CheckBox invitationCheckBox;
private ListView occasionView;
private DatabaseHelper dbHelper;
private View view;
private SharedPreferences sharedpreferences;
public boolean onOptionsItemSelected(MenuItem item) {
Log.d("onOptionsItemSelected", "yes");
switch (item.getItemId()) {
case R.id.menu_cancel_event:
displayInvitationList();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void displayInvitationList() {
int itemCount = occasionView.getCheckedItemCount();
for (int i = 1; i <= itemCount; i++) {
if (userOccasions.getCheckedInvitationCount() != null) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
// eventUserId = userOccasions.getCheckedInvitationCount();
// db.delete("user_relation", "EVENT_USER_ID" + " = ?", new String[]{Long.toString(eventUserId)});
db.close();
}
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
this.inflater = inflater;
this.container = container;
this.savedInstanceState = savedInstanceState;
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
final View view = inflater.inflate(R.layout.invitation_tab, container, false);
context = getActivity().getApplicationContext();
prgDialog = new ProgressDialog(getActivity());
eventId.clear();
eventName.clear();
eventPlace.clear();
// Set Progress Dialog Text
prgDialog.setMessage("Please wait...");
// Set Cancelable as False
prgDialog.setCancelable(false);
prgDialog.show();
DatabaseHelper dbHelper = new DatabaseHelper(context);
userMO = dbHelper.getRingeeUserData(1);
new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... arg0) {
return eventDelegates.getAllEventForUser(userMO, context);
}
@Override
protected void onPostExecute(String eventLists) {
if (eventLists != "null") {
eventMOs = gson.fromJson(eventLists, new TypeToken<List<EventMO>>() {
}.getType());
Toast.makeText(context, "total items of eventMo" + eventMOs.size(), Toast.LENGTH_LONG).show();
for (EventMO eventMO : eventMOs) {
eventName.add(eventMO.getText());
eventId.add(eventMO.getEventId());
eventPlace.add(eventMO.getPlace());
}
DatabaseHelper dbHelper = new DatabaseHelper(context);
//long totalInsertion = dbHelper.insertUserRelationTable(userMOs);
prgDialog.dismiss();
//Toast.makeText(context, "total userMos size " + userMOs.size() + "total db insertion size " + totalInsertion, Toast.LENGTH_LONG).show();
occasionView = (ListView) view.findViewById(R.id.invitation_list_view);
userOccasions = new UserOccasions();
occasionView.setAdapter(userOccasions);
occasionView.setItemsCanFocus(false);
occasionView.setTextFilterEnabled(true);
occasionView.setOnItemClickListener(occasionView.getOnItemClickListener());
}
}
}.execute(null, null, null);
return view;
}
private class UserOccasions extends BaseAdapter {
LayoutInflater mInflater;
TextView eventNameTxtV, eventPlaceTxtV;
private ArrayList<EventMO> checkedInvitationItems;
private Object invitationTag;
UserOccasions() {
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.checkedInvitationItems = new ArrayList<EventMO>();
}
@Override
public int getCount() {
return eventMOs.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return 0;
}
// show list values name and mobile number in contact page
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (null == convertView) {
convertView = mInflater.inflate(R.layout.invitation, null);
}
eventNameTxtV = (TextView) convertView.findViewById(R.id.invitationTitle);
eventPlaceTxtV = (TextView) convertView.findViewById(R.id.invitationPlace);
eventNameTxtV.setText(eventMOs.get(position).getText());
eventPlaceTxtV.setText(eventMOs.get(position).getPlace());
// intializing variables
final EventMO eventMO = eventMOs.get(position);
convertView.setTag(position);
View v = convertView.findViewById(R.id.invitation_single);
final LinearLayout eventLayout = (LinearLayout) convertView.findViewById(R.id.invitation_single);
invitationCheckBox = (CheckBox) convertView.findViewById(R.id.invitationCheckBox);
Button yesBtn = (Button) convertView.findViewById(R.id.yesButton);
Button noBtn = (Button) convertView.findViewById(R.id.noButton);
Button maybeBtn = (Button) convertView.findViewById(R.id.buttonMayBe);
final LinearLayout responseLayout = (LinearLayout) convertView.findViewById(R.id.hidden);
invitationCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
checkedInvitationItems.add((EventMO) eventMO);
} else {
checkedInvitationItems.remove((EventMO) eventMO);
}
}
});
//Based on the user click, response will be stored
yesBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
responseLayout.setVisibility(View.GONE);
//If user clicks yes button in invitation response layout,response would be stored as 1 for event user
final int response = 1;
final long eventId = eventMOs.get(position).getEventId();
userMO.setIsAttending(response);
new AsyncTask<Void, Void, String>() {
protected String doInBackground(Void... arg0) {
return userDelegate.updateEventUserRelationShipMapping(userMO, eventId);
}
}.execute(null, null, null);
}
});
noBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
responseLayout.setVisibility(View.GONE);
//If user clicks no button in invitation response layout,response would be stored as 0 for event user
final int response = 0;
final long eventId = eventMOs.get(position).getEventId();
userMO.setIsAttending(response);
new AsyncTask<Void, Void, String>() {
protected String doInBackground(Void... arg0) {
return userDelegate.updateEventUserRelationShipMapping(userMO, eventId);
}
}.execute(null, null, null);
}
});
maybeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
responseLayout.setVisibility(View.GONE);
//If user clicks maybe button in invitation response layout,response would be stored as for event user
final int response = 2;
userMO.setIsAttending(response);
final long eventId = eventMOs.get(position).getEventId();
new AsyncTask<Void, Void, String>() {
protected String doInBackground(Void... arg0) {
return userDelegate.updateEventUserRelationShipMapping(userMO, eventId);
}
}.execute(null, null, null);
}
});
eventLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.invitation_single:
responseLayout.setVisibility(View.VISIBLE);
break;
}
}
});
return convertView;
}
public ArrayList<EventMO> getCheckedInvitationCount() {
return this.checkedInvitationItems;
}
}
}
这里我尝试了以下代码,当按钮点击时更改按钮的颜色但是当我们返回并返回按钮时它将不会保持颜色,它将适用于所有事件
yesBtn.setbackgroundColor(color.Yellow);
noBtn.setbackgroundColor(color.blue);
maybeBtn.setbackgroundColor(color.blue);
上面提到的代码在yesBtn.oncliclListener()方法下
此处如何使用共享首选项来解决该问题
答案 0 :(得分:0)
您可以将颜色存储在共享首选项中作为Int。 但作为一个建议,你不需要存储颜色,但一些标志值1,2,3等,并根据该值你可以在按钮上设置颜色