2017-12-03 3 views
0

저는 문제가 있습니다. 즉, 토글 스위치를 사용하면 listview의 마지막 항목에서만 작동합니다. 전환 할 스위치는 중요하지 않습니다. 나는 다른 질문을 연구하지만 나는 그것을 이해할 수 없었다.listview 토글 스위치는 마지막 행에서만 작동합니다.

@Override 
public View getView(int position, View convertView, ViewGroup parent) 
{ 
    LayoutInflater inflater = LayoutInflater.from(getContext()); 
    View customView = inflater.inflate(R.layout.row, parent, false); 
    String allData = getItem(position); 


    final Switch status = (Switch) customView.findViewById(R.id.switchStatus); 


    status.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) { 
      Toast.makeText(getContext(),"selectid",Toast.LENGTH_LONG).show(); 

     } 
    }); 

    return customView; 

} 

여기서 내 어댑터를 볼 수 있습니다.

public class adapter_test extends ArrayAdapter<String> { 



    public adapter_allservicerecords(Context context, String[] data){ 
     super(context, R.layout.row, data); 
    } 

    @Override 
public View getView(int position, View convertView, ViewGroup parent) 
{ 
    LayoutInflater inflater = LayoutInflater.from(getContext()); 
    View customView = inflater.inflate(R.layout.row, parent, false); 
    String allData = getItem(position); 



     try { 
      if(!all_data.isEmpty()) { 
       JSONObject jsonRowData = new JSONObject(all_data); 
       try { 
        text.setText(jsonRowData.getString("TITLE")); 


       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     } catch (Exception e) { 
      Log.e("FAILED", "Json parsing error: " + e.getMessage()); 
     } 

    return customView; 

    } 
} 

답변

2

문제를 해결하려면 데이터를 String으로 만드는 대신 각 행을 나타내는 데이터로 변경해야합니다. 대신 목록으로 목록을 가지고 있어야하고 쉽게에 문자열을 추가 할 수 있도록

class Item { 
    String data; 
    boolean toggled; 

    public Item(String data) { 
     this.data = data; 
    } 

    public void setToggled(boolean toggle) { 
     toggled = toggle; 
    } 
} 

데이터 소스 지금, 문자열이 아니어야합니다 : 당신이 당신의 행을 표현하기 위해이 클래스를 설정할 수 있습니다 예를 들어

새 항목을 전달하여 목록을 만듭니다 ("THE STRING DATA HERE"). 목록에 데이터를 추가 할 때. 그래서 그 대신

String allData = getItem(position); 

의이 같은 보일 것의 getView에 다음

Item item = getItem(position); 

을 사용합니다 :

@Override 
public View getView(int position, View convertView, ViewGroup parent) 
{ 
    LayoutInflater inflater = LayoutInflater.from(getContext()); 
    View customView = inflater.inflate(R.layout.row, parent, false); 
    Item item = getItem(position); 


    final Switch status = (Switch) customView.findViewById(R.id.switchStatus); 
    status.set 

    status.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) { 
      // here we change the value of the item which is referring to the specific index in the array returned by getItems() 
      item.setToggled(isChecked); 
      Toast.makeText(getContext(),"selectid",Toast.LENGTH_LONG).show(); 

     } 
    }); 

    // here we get what the latest value of the switch is 
    status.setChecked(item.toggled); 
    return customView; 

} 

당신은 또한 데이터 구조에 데이터 소스를 변경할 수 있는지 확인을/List 또는 사용자가 사용하기로 결정한 유사한 것을 나타내는 목록.

보너스 팁 : 보기를 유지하고 효율적인 인스턴스 재활용을 위해 ViewHolder를 사용하는 것이 좋습니다.

class ViewHolder { 
    Switch statusSwitch; 
    View customView; 
} 

그런 다음 getView() 부분에서 쉽게 사용할 수 있습니다. convertView 또는 메서드의 두 번째 매개 변수가 null 인 경우 다시 사용하거나 인스턴스화 할 수있는 뷰를 나타내므로 실제로 뷰의 새 인스턴스를 만들 필요가 없습니다.

이기종 목록은 뷰 유형의 수를 지정할 수 있으므로이보기가 항상 올바른 유형이므로이를 사용하는 것이 좋습니다.

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

    LayoutInflater inflater = LayoutInflater.from(getContext()); 
    ViewHolder holder = null; 
    Item item = getItem(position); 
    if (convertView == null) { // instantiate if not yet instantiated 
     convertView = inflater.inflate(R.layout.supplies_list_item, null); 
     holder.statusSwitch = (Switch) convertView.findViewById(R.id.switchStatus); 
    } 
    else { 
     holder = (ViewHolder) convertView.getTag(); 
    } 
    // you could set the values/ listeners here 
    holder.statusSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) { 
      item.setToggled(isChecked); 
      Toast.makeText(getContext(),"selectid",Toast.LENGTH_LONG).show(); 

     } 
    }); 
    holder.statusSwitch.setChecked(item.toggled); 

    return convertView; 
} 
+0

답장을 보내 주셔서 감사합니다. 항목 item = getItem (position); gaves 오류 : (48, 28) 오류 : 호환되지 않는 유형 : 문자열을 오류로 변환 할 수 없습니다. – user7732643

+0

좋아요, 당신이해야 할 일은 Item 생성자의 String 데이터를 전달하는 것입니다. –

+0

여전히 같은 친구. – user7732643

관련 문제