2012-02-24 109 views
1

我正在使用下面的代码ListView。问题是当我点击一个没有突出显示的列表项时。你看,我试图使用arg0.setBackgroundColor(Color.RED);,但是当我点击该项目并且不要让我的手指向上时,没有任何反应,当我点击该项目并让手指向上时,它会突出显示并保持突出显示。 如何让我的手指向上突出显示被删除? 我不知道为什么这不像一个简单的listview。listview与listadapter突出显示项目

private class ListAdapter extends ArrayAdapter { 
     private ArrayList mList; 
     private Context mContext; 

     public ListAdapter(Context context, int textViewResourceId, ArrayList list) { 
      super(context, textViewResourceId, list); 
      this.mList = list; 
      this.mContext = context; 
     } 

     public View getView(int position, View convertView, ViewGroup parent) { 
      View view = convertView; 

      try { 
       if (view == null) { 
        LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
        view = vi.inflate(R.layout.list_item, null); 
       } 

       final Object listItem = mList.get(position); 
       if (listItem != null) { 

        ((TextView) view.findViewById(R.id.tv_name)).setText(((HotOrNot) listItem).getName()); 
        view.setOnClickListener(new OnClickListener() { 
         public void onClick(View arg0) { 
          arg0.setBackgroundColor(Color.RED); 
          Toast.makeText(SQLView.this, "ID: " + ((HotOrNot) listItem).getID(), Toast.LENGTH_SHORT).show(); 
          Toast.makeText(SQLView.this, "Name: " + ((HotOrNot) listItem).getName(), Toast.LENGTH_SHORT).show(); 
          Toast.makeText(SQLView.this, "Descr: " + ((HotOrNot) listItem).getDescription(), Toast.LENGTH_SHORT).show(); 


          // finish(); 
         } 
        }); 
       } 
      } catch (Exception e) { 
       //Log.i(Splash.ListAdapter.class.toString(), e.getMessage()); 
      } 
      return view; 
     } 
    } 
+0

看一看这个问题 - 它描述了解决方案,你需要什么。总之,您需要为列表项目背景使用选择器。 http://stackoverflow.com/questions/9434317/listview-with-listadapter-highlight-item – 2012-02-24 16:58:27

回答

0

这个XML文件放到你的drawable文件夹和设置LIST_ITEM背景mainlayout

<?xml version="1.0" encoding="utf-8"?> 
    <selector xmlns:android="http://schemas.android.com/apk/res/android"> 

     <item android:drawable="#FFFFFF" android:state_pressed="false" android:state_selected="false"/> 
     <item android:drawable="#FF00FF" android:state_pressed="true" android:state_selected="false"/> 
     <item android:drawable="#FF00FF" android:state_pressed="true" android:state_selected="true"/> 

    </selector> 
+0

我不能理解你的评论 – 2012-02-24 17:18:28

+0

你不能理解我的答案....首先复制我的答案<?xml ...然后右键单击可绘制文件夹并创建xml文件并给出名称list_bg并保存在可绘制文件夹中(不要在布局中),然后粘贴,,,然后在你的布局list_item中有任何布局,并给android:background =“@ drawable/list_bg”然后清理并建立您的项目并重新运行 – 2012-02-24 17:27:14