2017-09-26 4 views
0

ここではlistitemを選択し、その特定のアイテムの色が変更されています。 2回目にアイテムを選択すると、この前のアイテムがキャンセルされ、新しいアイテムの色を変更する必要があります。リスト項目の値を1つだけ選択する必要があります

delivery.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      LayoutInflater inflater1 = (LayoutInflater) ct.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      View v1 = inflater1.inflate(R.layout.activity_employees_list_for_pop_up, null); 
      final Button ok = (Button) v1.findViewById(R.id.do_ok); 
      Button cancle = (Button) v1.findViewById(R.id.do_cancle); 
      final TextView empId=(TextView)v1.findViewById(R.id.employeeId); 
      ok.setEnabled(false); 
      listView = (ListView) v1.findViewById(R.id.employeePopUpList); 
      employeePopUpAdapter = new EmployeePopUpAdapter(ct, employeeIdNameBeans); 

      //enable ok button if listitem is checked 
      listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
       @Override 
       public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
        ok.setEnabled(true); 
        //Toast.makeText(ct, ""+employeeIdNameBeans.get(position).getEmpId(), Toast.LENGTH_SHORT).show(); 
        view.setBackgroundColor(Color.GREEN); 
        selectedemployeeid=employeeIdNameBeans.get(position).getEmpId(); 


       } 
      }); 

そして、色を変更した特定のemployeeidを取得する必要があります。

とアダプタクラスで私はちょうど値にEMPIDとのempName

public class EmployeePopUpAdapter extends BaseAdapter { 
Context ct; 
private List<EmployeeIdNameBean> employeeIdNameBeans; 
private int lastPosition = -1; 

public EmployeePopUpAdapter(Context ct, List<EmployeeIdNameBean> employeeIdNameBeans) { 
    this.ct = ct; 
    this.employeeIdNameBeans = employeeIdNameBeans; 
} 


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

@Override 
public Object getItem(int position) { 
    return null; 
} 

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

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    LayoutInflater inflater = (LayoutInflater) ct.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View v = inflater.inflate(R.layout.list_view_for_employee_popup_window, null); 
    TextView empId = (TextView) v.findViewById(R.id.employeeId); 
    TextView empName = (TextView) v.findViewById(R.id.empFullName); 

    //For animation 
    Animation animation = AnimationUtils.loadAnimation(ct, (position > lastPosition) ? R.anim.top_from_bottom : R.anim.down_from_top); 
    v.startAnimation(animation); 
    lastPosition = position; 

    final EmployeeIdNameBean empbean = employeeIdNameBeans.get(position); 
    empId.setText(empbean.getEmpId()); 
    empName.setText(empbean.getEmpName()); 

    return v; 
} 

}

+0

アドパターコードも投稿してください。この目的のために、選択した明細ポジションを更新する必要があります。 –

+0

@ R.R.Mアダプタクラスも追加しました。コードを確認して返信してください。前もってありがとうございます – aaron

答えて

0

このようにしてください。

public class EmployeePopUpAdapter extends BaseAdapter { 

Context ct; 
private List<EmployeeIdNameBean> employeeIdNameBeans; 
private int lastPosition = -1; 
private int chooseItem = -1; 

public EmployeePopUpAdapter(Context ct, List<EmployeeIdNameBean> employeeIdNameBeans) { 
    this.ct = ct; 
    this.employeeIdNameBeans = employeeIdNameBeans; 
} 


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

@Override 
public Object getItem(int position) { 
    return null; 
} 

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

public void setChooseItem(int i) { 
    chooseItem = i; 
    notifyDataSetChanged(); 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    LayoutInflater inflater = (LayoutInflater) ct.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View v = inflater.inflate(R.layout.list_view_for_employee_popup_window, null); 
    TextView empId = (TextView) v.findViewById(R.id.employeeId); 
    TextView empName = (TextView) v.findViewById(R.id.empFullName); 

    //For animation 
    Animation animation = AnimationUtils.loadAnimation(ct, (position > lastPosition) ? R.anim.top_from_bottom : R.anim.down_from_top); 
    v.startAnimation(animation); 
    lastPosition = position; 

    final EmployeeIdNameBean empbean = employeeIdNameBeans.get(position); 
    empId.setText(empbean.getEmpId()); 
    empName.setText(empbean.getEmpName()); 
    // edited here ,change color 
    try { 
     if (chooseItem == -1) { 
     } else { 
      if (chooseItem == position) { 
       empId.setBackgroundColor(ct.getResources().getColor(android.R.color.holo_blue_bright)); 
      } else { 
       empId.setBackgroundColor(ct.getResources().getColor(android.R.color.holo_red_dark)); 
      } 
     } 
    } catch (Exception E) { 
    } 
    return v; 
} 
} 

あなたのアクティビティコード。

mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 
      adapter.setChooseItem(i); 
     } 
}); 
+0

私のコード – aaron

+0

で回答を明記してください。 @ aaron – KeLiuyue

+0

あなたのコードは正常に動作しています。私には欠けていることがもう1つあります。私がその項目をクリックすると、empIDを取得する必要があります。だから私はURLでそれを使用することができます – aaron

0

あなたはその後、別の項目をクリックしますと、選択した項目の位置を取得し、そのための背景色を変更を取っていますリストをリフレッシュします。つまり、選択したすべての色を削除し、選択した項目の色を設定します。

モデルクラスにブール型パラメータを追加して、アダプタのtrue値をチェックして、選択した項目の値をtrueに変更し、フラグをクリアしてリフレッシュするたびに同じ値の背景色を変更することもできますリスト。

+0

リストビューをリフレッシュすると、リストビュー全体が再びリフレッシュされます。 – aaron

+0

リストをリフレッシュしても表示されず、選択した項目のパス値が変更され、色が変更されてリストが表示されます。 –

関連する問題