1

I have created a listview with simplecursoradapter and made it to Highlight when any of the item is clicked on it with the following code.

<selector xmlns:android="http://schemas.android.com/apk/res/android">  

<item
android:state_selected="true"
android:drawable="@color/blue" /> 

<item 
android:drawable="@color/white" />

</selector>

and on Item selected i have done as below.

list = (ListView) view.findViewById(android.R.id.list);
adapter = new SimpleCursorAdapter(getActivity(), R.layout.title_intro_list, articleCur, FROM, TO,1);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, final View view,
int position, long id) 
{
view.setSelected(true);
}

It's working fine ,when i select the item in listview it get selected , But the problem is when i scroll the listview the item selected doesn't remain highlighted.

4

2 回答 2

0

正如其他人所提到的,由于列表视图回收了您的视图,当您将所选视图从页面上滚动出来时,它会被回收,这是您问题的根本原因。

您需要使用 xml 属性或方法将 ListView 上的选择模式设置为CHOICE_MODE_SINGLE。已经有一段时间了,但我认为这会自动使单击位置成为选定项目,但如果没有,您可以调用ListView (这是参数)choiceModesetChoiceMode()setSelection(position)parentonItemClick

于 2013-08-08T07:34:51.617 回答
0

您应该使用扩展 BaseAdapter 的内部类,例如:

    private class TicketLVAdapter extends BaseAdapter{
    private LayoutInflater inflater;
    private List<Ticket> list;
    private HashMap<String, Integer> alphaIndexer;
    private String[] sections;
    List<Ticket> ts;

    public TicketLVAdapter(Context context) {
        this.inflater = LayoutInflater.from(context);
    }

    public void setList(List<Ticket> ts) {
        this.ts = ts;
    }

    @Override
    public int getCount() {
        return ts.size();
    }

    @Override
    public Object getItem(int position) {
        return ts.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
//the very important method:
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;//use view holder;
        holder = new ViewHolder();
        convertView = inflater.inflate(R.layout.pulldown_item, null);
        holder.tv_id_num = (TextView) convertView.findViewById(R.id.num_id);
        holder.tvConsumeTime = (TextView) convertView
                .findViewById(R.id.time);
        holder.tvOperator = (TextView) convertView
                .findViewById(R.id.operator);
        holder.tvValue = (TextView) convertView.findViewById(R.id.value);
        convertView.setTag(holder);
        Ticket ticket = ts.get(position);
        if (rtApp.isOnUnload) {// onCreate()方法中清空掉failureTickets
            ArrayList<Integer> positionList = new ArrayList<Integer>();
            for (int i = 0; i < ts.size(); i++) {
                if (rtApp.failureTickets.contains(ts.get(i).getTicketID())) {
                    positionList.add(i);
                }
            }
        }
        String ticketCT = ticket.getTicketconsumeTime();
        holder.tvConsumeTime.setText(ticketCT);
        holder.numberString = "位置:" + position;
        String ticketOp = ticket.getTicketOperator();
        holder.tvOperator.setText(ticketOp);
        holder.tv_id_num.setText(position + 1 + "");
        String ticketValue = ticket.getTicketPrice();
        holder.tvValue.setText(ticketValue);
        if (rtApp.failureTickets.contains(ticket.getTicketID())
                && rtApp.isOnUnload) {
            holder.tvConsumeTime.setTextColor(Color.parseColor("#ff0000"));
            holder.tvOperator.setTextColor(Color.parseColor("#ff0000"));
            holder.tv_id_num.setTextColor(Color.parseColor("#ff0000"));
            holder.tvValue.setTextColor(Color.parseColor("#ff0000"));
        }
        return convertView;
    }

    private class ViewHolder {
        TextView tvID;
        TextView tvConsumeTime;
        TextView tvOperator;
        TextView tvValue;
        TextView tv_id_num;
        String numberString;

    }

    @Override
    public int getPositionForSection(int section) {

        String later = sections[section];
        return alphaIndexer.get(later);
    }

    @Override
    public int getSectionForPosition(int position) {
        return 0;
    }

    @Override
    public Object[] getSections() {
        // TODO Auto-generated method stub
        return null;
    }
}
于 2013-08-08T07:56:25.567 回答