列表视图背景颜色未在选定视图中设置

时间:2015-04-13 07:30:22

标签: android

listView.setOnItemLongClickListener(new OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View view,
                int arg2, long arg3) {

            view.setBackgroundColor(Color.parseColor("#222222"));
            Log.v("Long Click", "Working");
            return false;
        }
    });

2 个答案:

答案 0 :(得分:0)

您必须设置适配器并添加它才能将属性添加到列表视图。 Try visiting here

String [] values = {"color1","color2","color3"};
 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
              android.R.layout.simple_list_item_1, android.R.id.text1, values);


            // Assign adapter to ListView
            listView.setAdapter(adapter); 

//rest of your code here

答案 1 :(得分:0)

您可以跟踪当前所选元素的位置:

int defult_vaule=-1;
listView.setOnItemLongClickListener(new OnItemLongClickListener() {

        public boolean onItemLongClick(AdapterView<?> arg0,
                View arg1, int position, long id) {
            // TODO Auto-generated method stub
            defult_vaule = position;
            listadapter.notifyDataSetChanged();
  }
}

覆盖适配器的getView方法

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final View view = View.inflate(context, R.layout.item_list, null);
    ViewHolder viewHolder = new ViewHolder();
    viewHolder.lout_background = (LinearLayout) rowView
                .findViewById(R.id.lout_background);
    if (position == defult_vaule ) {
        // set your color     
            holder.lout_background.setBackgroundColor(Color.parseColor("#222222"));
    }

    return view;
}