2016-08-01 2 views
0

사용자 정의 어댑터, 레이아웃 및 모델 클래스를 사용하여 목록보기 (텍스트 및 체크 상자 사용)했습니다. 선택한 확인란을 sqlite db에 저장하여 다른 활동으로 이동 한 다음 다시 돌아와서 선택한 확인란을 선택한 상태로 유지하려고합니다.목록보기에서 선택된 체크 상자를 기억합니다.

public class MainActivity extends AppCompatActivity { 
 

 
    MyCustomAdapter dataAdapter = null; 
 

 

 
    @Override 
 
    protected void onCreate(Bundle savedInstanceState) { 
 
     super.onCreate(savedInstanceState); 
 
     setContentView(R.layout.activity_main); 
 

 
     displayListView(); 
 

 
     checkButtonClick(); 
 

 

 
    } 
 

 
    private void displayListView() 
 
    { 
 

 
     //Array list of countries 
 
     ArrayList<States> stateList = new ArrayList<States>(); 
 

 
     States _states = new States("AP","Andhra Pradesh",false); 
 
     stateList.add(_states); 
 
     _states = new States("DL","Delhi",true); 
 
     stateList.add(_states); 
 
     _states = new States("GA","Goa",false); 
 
     stateList.add(_states); 
 
     _states = new States("JK","Jammu & Kashmir",true); 
 
     stateList.add(_states); 
 
     _states = new States("KA","Karnataka",true); 
 
     stateList.add(_states); 
 
     _states = new States("KL","Kerala",false); 
 
     stateList.add(_states); 
 
     _states = new States("RJ","Rajasthan",false); 
 
     stateList.add(_states); 
 
     _states = new States("WB","West Bengal",false); 
 
     stateList.add(_states); 
 

 
     //create an ArrayAdaptar from the String Array 
 
     dataAdapter = new MyCustomAdapter(this,R.layout.state_info, stateList); 
 
     ListView listView = (ListView) findViewById(R.id.listView1); 
 
     // Assign adapter to ListView 
 
     listView.setAdapter(dataAdapter); 
 

 
     listView.setOnItemClickListener(new AdapterView.OnItemClickListener() 
 
     { 
 

 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
 
      { 
 
       // When clicked, show a toast with the TextView text 
 
       States state = (States) parent.getItemAtPosition(position); 
 
       Toast.makeText(getApplicationContext(),"Clicked on Row: " + state.getName(), 
 
         Toast.LENGTH_LONG).show(); 
 
      } 
 
     }); 
 
    } 
 

 
    private void checkButtonClick() 
 
    { 
 

 
     Button myButton = (Button) findViewById(R.id.findSelected); 
 

 
     myButton.setOnClickListener(new View.OnClickListener() 
 
     { 
 

 
      @Override 
 
      public void onClick(View v) 
 
      { 
 

 
       StringBuffer responseText = new StringBuffer(); 
 
       responseText.append("The following were selected...\n"); 
 

 
       ArrayList<States> stateList = dataAdapter.stateList; 
 

 
       for(int i=0;i<stateList.size();i++) 
 
       { 
 
        States state = stateList.get(i); 
 

 
        if(state.isSelected()) 
 
        { 
 
         responseText.append("\n" + state.getName()); 
 
        } 
 
       } 
 

 
       Toast.makeText(getApplicationContext(), 
 
         responseText, Toast.LENGTH_LONG).show(); 
 
      } 
 
     }); 
 
    } 
 

 

 
}
public class MyCustomAdapter extends ArrayAdapter<States> { 
 

 
    public ArrayList<States> stateList; 
 

 
    public MyCustomAdapter(Context context, int textViewResourceId, ArrayList<States> stateList) 
 
    { 
 
     super(context, textViewResourceId, stateList); 
 
     this.stateList = new ArrayList<States>(); 
 
     this.stateList.addAll(stateList); 
 
    } 
 

 
    private class ViewHolder 
 
    { 
 
     TextView code; 
 
     CheckBox name; 
 
    } 
 

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

 
     ViewHolder holder = null; 
 

 
     Log.v("ConvertView", String.valueOf(position)); 
 

 
     if (convertView == null) 
 
     { 
 

 
      LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
 

 
      convertView = vi.inflate(R.layout.state_info, null); 
 

 
      holder = new ViewHolder(); 
 
      holder.code = (TextView) convertView.findViewById(R.id.code); 
 
      holder.name = (CheckBox) convertView.findViewById(R.id.checkBox1); 
 

 
      convertView.setTag(holder); 
 

 
      holder.name.setOnClickListener(new View.OnClickListener() 
 
      { 
 
       public void onClick(View v) 
 
       { 
 
        CheckBox cb = (CheckBox) v; 
 
        States _state = (States) cb.getTag(); 
 

 
       /* Toast.makeText(getApplicationContext(), "Clicked on Checkbox: " + cb.getText() + " is " + cb.isChecked(), 
 
          Toast.LENGTH_LONG).show();*/ 
 

 
        _state.setSelected(cb.isChecked()); 
 
       } 
 
      }); 
 

 
     } 
 
     else 
 
     { 
 
      holder = (ViewHolder) convertView.getTag(); 
 
     } 
 

 
     States state = stateList.get(position); 
 

 
     holder.code.setText(" (" + state.getCode() + ")"); 
 
     holder.name.setText(state.getName()); 
 
     holder.name.setChecked(state.isSelected()); 
 

 
     holder.name.setTag(state); 
 

 
     return convertView; 
 
    } 
 

 
}
public class States { 
 

 
    String code = null; 
 
    String name = null; 
 
    boolean selected = false; 
 

 
    public States(String code, String name, boolean selected) { 
 
     this.code = code; 
 
     this.name = name; 
 
     this.selected = selected; 
 
    } 
 

 
    public String getCode() { 
 
     return code; 
 
    } 
 

 
    public void setCode(String code) { 
 
     this.code = code; 
 
    } 
 

 
    public String getName() { 
 
     return name; 
 
    } 
 

 
    public void setName(String name) { 
 
     this.name = name; 
 
    } 
 

 
    public boolean isSelected() { 
 
     return selected; 
 
    } 
 

 
    public void setSelected(boolean selected) { 
 
     this.selected = selected; 
 
    } 
 
}

+0

당신은 안드로이드 스튜디오 또는 일식을 사용하고 있습니까 ?? – Andolasoft

+0

Android Studio. –

답변

0
Use Realm instead of Sqlite. 

// 내부 모델 클래스
미국이 RealmObject {

String code = null; 
String name = null; 
boolean selected = false; 

public States(String code, String name, boolean selected) { 
    this.code = code; 
    this.name = name; 
    this.selected = selected; 
} 

public String getCode() { 
    return code; 
} 

public void setCode(String code) { 
    this.code = code; 
} 

public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

public boolean isSelected() { 
    return selected; 
} 

public void setSelected(boolean selected) { 
    this.selected = selected; 
} 

}를 확장 공용 클래스`

// 내부 어댑터 클래스();

  // Get the book title to show it in toast message 
      States state = results.get(position); 

      holder.code.setText(" (" + state.getCode() + ")"); 
      holder.name.setText(state.getName()); 
      holder.name.setChecked(state.isSelected()); 

      holder.name.setTag(state); 

당신은 안드로이드 스튜디오에서 영역을 사용하는 방법을 알고 아래의 링크를 따라 할 수

use realm database example 1

use realm database example 2

+0

'State state = results.get (position);'줄에 오류가 있습니다. 렐름은 꽤 새롭고 조금은 나를 보완 해주었습니다. 다른 방법이 있습니까? @Andolasoft –

0
There are two more ways like : 

use android sqlite

use Shared Preference

+0

나는 SharedPreference를 선호합니다. SharedPreference를 사용하여 체크 박스의 체크 상태를 저장하고 복원하는 방법을 보여줄 수 있습니까? @Andolasoft –

관련 문제