当我点击listitems时,如何避免调用适配器类的getView()方法?

时间:2013-10-19 06:51:14

标签: java android

  • 我已经实现了列表视图,其中的按钮具有背景图像更改效果。但是,当我点击任何列表项时,该效果不再存在并刷新。
  • 我检查过,当我点击任何列表项时,getView()正在调用...
  • 如何避免这种情况???
  • 如果有人知道,请提出任何解决方案......
  • 谢谢..
  • 代码是:

公共类EventListAdapter扩展BaseAdapter {     private static final int VISIBLE = 0;

private static final int GONE = 8;




private List<Events> dateForEventList;


private String eventsRetrived;


private String[] events;
boolean clickStatus = false;

private int status = 0;



public EventListAdapter(Context context)
{
    mContext = context;
}


/**
 * The number of items in the list is determined by the number of announcements
 * in our array.
 * 
 * @see android.widget.ListAdapter#getCount()
 */
public int getCount() {

    DatabaseManager db = new DatabaseManager(mContext);
    dateForEventList = db.getAllData1(CalendarAdapter.dateOfEventSingle);
    for (Events l : dateForEventList) {
        eventsRetrived = l.getEventData();
    }

    events = eventsRetrived.split(",");
    return events.length;
}

/**
 * Since the data comes from an array, just returning
 * the index is sufficent to get at the data. If we
 * were using a more complex data structure, we
 * would return whatever object represents one 
 * row in the list.
 * 
 * @see android.widget.ListAdapter#getItem(int)
 */
public Object getItem(int position) {
    return position;
}

/**
 * Use the array index as a unique id.
 * @see android.widget.ListAdapter#getItemId(int)
 */
public long getItemId(int position) {
    return position;
}

public View getView(final int position, View myView, ViewGroup parent) {
    if(myView == null) {
        LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);           
        myView = inflater.inflate(R.layout.activity_event_list, null);
    }
    TextView textViewTitle;
    TextView textViewDialogue;

    final ImageButton buttonForCheckMark;


    final ImageButton buttonForDelete;
    final ImageButton buttonForRemainder;

    //Events event = new Events("11/2/2013","today, there is function in Shivajinagar");

    //          event.setDate("11 Jan,2013");
    //          String event1 = event.getDate();
    //
    //          textViewForDateHeader = (TextView)myView.findViewById(R.id.textViewHeadingDate);
    //          textViewForDateHeader.setText(event1);

    DatabaseManager db = new DatabaseManager(mContext);
    dateForEventList = db.getAllData1(CalendarAdapter.dateOfEventSingle);
    for (Events l : dateForEventList) {
        eventsRetrived = l.getEventData();
    }

    events = eventsRetrived.split(",");
    //      TextView tv = (TextView)myView.findViewById(R.id.grid_item_text);
    //      tv.setText(events[position]);


    textViewTitle = (TextView) myView.findViewById(R.id.textViewTitle);
    textViewTitle.setText(events[position]);

    textViewDialogue = (TextView) myView.findViewById(R.id.textViewDialog);
    textViewDialogue.setText(events[position]);
    textViewDialogue.setVisibility(mExpanded[position] ? VISIBLE : GONE);


    //          textViewHeader = (TextView)myView.findViewById(R.id.textViewHeader);
    //          textViewHeader.setText(mHeader[position]);
    //          textViewHeader.setVisibility(mExpanded[position] ? VISIBLE : GONE);


    buttonForCheckMark = (ImageButton) myView.findViewById(R.id.buttonForCheckMark);
    buttonForCheckMark.setVisibility(mExpanded[position] ? VISIBLE : GONE);

    buttonForDelete = (ImageButton) myView.findViewById(R.id.buttonForDelete);
    buttonForDelete.setVisibility(mExpanded[position] ? VISIBLE : GONE);

    buttonForRemainder = (ImageButton) myView.findViewById(R.id.buttonForRemainder);
    buttonForRemainder.setVisibility(mExpanded[position] ? VISIBLE : GONE);

    buttonForRemainder.setOnClickListener(new OnClickListener() {

        @SuppressWarnings("static-access")
        @SuppressLint("SimpleDateFormat")
        public void onClick(View v) {

            try {
                Toast.makeText(mContext, "remainder saved..", Toast.LENGTH_SHORT).show();


            } catch (Exception ex) {
                Toast.makeText(mContext, "Exception in Remainder " + ex.toString(), Toast.LENGTH_SHORT).show();

            }
            //              
        }
    });

    buttonForCheckMark.setOnClickListener(new OnClickListener() {


        public void onClick(View v){
            Toast.makeText(mContext, "tapped on checkMark", Toast.LENGTH_SHORT).show();

            Toast.makeText(mContext, " current Position tapped : " + position, Toast.LENGTH_SHORT).show();

            if(position == 0) {
                buttonForCheckMark.setBackgroundResource(R.drawable.ic_launcher);
                buttonForDelete.setBackgroundResource(R.drawable.ic_navigation_cancel);
                buttonForCheckMark.setClickable(false);
                buttonForDelete.setClickable(true);
            }
            // change the status to 1 so the at the second clic , the else will be executed


            //              else {  
            //                  button.setBackgroundResource(R.drawable.ic_navigation_accept);
            //                  status =0;//change the status to 0 so the at the second clic , the if will be executed
            //              }
            //              buttonForCheckMark.setBackgroundResource(R.drawable.ic_drawer);

        }
    });
    buttonForDelete.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            Toast.makeText(mContext, "tapped on delete", Toast.LENGTH_SHORT).show(); 
            if(position == 0) {
                buttonForCheckMark.setBackgroundResource(R.drawable.ic_navigation_accept);
                buttonForCheckMark.setClickable(true);
                buttonForDelete.setBackgroundResource(R.drawable.ic_drawer);
                buttonForDelete.setClickable(false);
                // change the status to 1 so the at the second clic , the else will be executed
            }

        }
    });


    return myView;
}



public void toggle(int position) {


    mExpanded[position] = !mExpanded[position];
    notifyDataSetChanged();


}

/**
 * Remember our context so we can use it when constructing views.
 */
private Context mContext;


/**
 * Our data, part 1.
 */
private String[] mTitles = 
    {
        "Event 1",   
        "Event 2",
        "Event 3",       
        "Event 4",
        "Event 5"

    };

/**
 * Our data, part 2.
 */
private String[] mDialogue = 
    {
        "wuszuogwfuieffufuhuysugdueljwihadghgxdhgyhghsdgyigwuweyuqaGDHGYHGHGAdhgyhigxgxgeuyehu.",
        "dgusduugyujguegytgujgdugwjhiuyg7wtqUYGYYgyijyiufufjguhgdugfhgfhgfgfhgfhghfghifgyi,dgwsdtgyfytfiuwt,",
        "rtygygghtudggyjhgujtugdhhguyuaUUUUDJYUIDHUJHDIIDUJDHDUJHDIDIOUYhujtdugyhdgg",
        "gjhuwjsgudggdudgjqhasdgdhgjdhushjaguhguwegagsdgygydgfgdcgycg",
        "fhdgyhdfhfgdyhhwsddgyuduuufguugwugdfgugdgooduiuduiuduuduiuiuidudiiwdiou"


    };

/**
 * Our data, part 3.
 */
private boolean[] mExpanded = 
    {
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false   
    };

//      private Integer[] mThumbIds = {
//              R.drawable.remainder, R.drawable.remainder,
//              R.drawable.remainder, R.drawable.remainder,
//              R.drawable.remainder
//
//      };

}

1 个答案:

答案 0 :(得分:0)

你不能停止自动调用getView()方法,但你可以用另一种方式解决你的问题。

您正在将此布局扩展到适配器

R.layout.activity_event_list

我假设你的父母为这个布局是相对布局,id为@ + id / parent

现在在你的getView方法中设置像这样的行的背景颜色。

            RelativeLayout parentLayout=(RelativeLayout)findViewById(R.id.parent);
        parentLayout.setBackgroundColor(Color.WHITE);