2012-02-02 4 views
0

좋은 하루, 배열 어댑터의 목록 뷰에있는 텍스트보기 항목의 상태를 표시하려고합니다. 나는 listview의 항목을 성공적으로 저장할 수 있지만 listview가 다시 채워질 때 다시 표시 할 수는 없습니다.ArrayAdapter에서 목록보기에 취소 선 텍스트 상태를 저장하십시오. Android

onitemclick :

public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

    String mytext = ((TextView)view).getText().toString(); 

    TextView text = (TextView)view.findViewById(android.R.id.text1); 
    text.setText(mytext); 
    text.setPaintFlags(text.getPaintFlags()| Paint.STRIKE_THRU_TEXT_FLAG);    

    crossed_text.add(text.getText().toString()); // this is an Arraylist to store striked texts 

    } 

및 채우기 목록보기 : 내가 다시 목록보기를 채우는 주를 통해이 타격을받을 수 있나요 어떻게

// i tried this 

dialog = new Dialog(this); 
     LayoutInflater my_inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    View checklistView = my_inflater.inflate(R.layout.check_list,null, false); 
    dialog.setContentView(checklistView); 
    dialog.setCancelable(true); 

     ListView list = (ListView)dialog.findViewById(R.id.listview_id); 
     TextView text = (TextView)list.findViewById(android.R.id.text1); 
     list.setOnItemClickListener(this); 
     list.setOnItemLongClickListener(this); 

    my_CheckList_Adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data); 

       for(int i = 0; i < my_CheckList_Adapter.getCount(); i++){ 


    TextView childtext = (TextView)list.getChildAt(i); // getting null value here 

         //childtext = (TextView)my_CheckList_Adapter.getItem(i).toString(); 

     if(crossed_text.contains(childtext.getText().toString())){ 
     childtext.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG); 

       } 
      } 

     list.setAdapter(my_CheckList_Adapter); 

.. 감사

답변

0

시도
if(crossed_text.get(i).contains(childtext.getText().toString())){ 

또는

if(crossed_text.get(i).equals(childtext.getText().toString())){ 

편집 :

그것은 작업 예제이지만 나쁜 연습 샘플의 더 많은 것이다. 나중에 가져올 항목의 텍스트를 수집하지 마십시오. 색인을 사용하여 배열에 넣습니다. 텍스트를 사용하려는 목록에서 항목을 검색하지 않는 한. 목록에서 항목을 수집하여 해당 작업을 수행하려면 색인을 사용하고 작업을 수행하십시오.


public class ListActivityArrayAdapter extends Activity { 

    ListView lv; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     LinearLayout ll = new LinearLayout(this); 
     LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(
       LinearLayout.LayoutParams.WRAP_CONTENT, 
       LinearLayout.LayoutParams.WRAP_CONTENT); 

     ll.setLayoutParams(lp1); 
     ll.setOrientation(1); 
     lv = new ListView(this); 

     final String [] data = new String[]{"test","test1","test2","test3","test4","test5","test6"}; 
     final ArrayAdapter ad = new ArrayAdapter(this,android.R.layout.simple_list_item_single_choice,data){ 
      public View getView(int position, View convertView, ViewGroup parent){ 
        TextView tv =new TextView(getContext()); 
          tv.setText(data[position]); 
       tv.setTextSize(24); 
       return tv; 
      } 
     }; 
     lv.setAdapter(ad); 
     final HashSet<String> arr = new HashSet<String>(); 
     lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 
       String text = ((TextView) view).getText().toString(); 
       arr.add(text); 
       System.out.println("added: "+text); 
      } 
     }); 

     Button b = new Button(this); 
     b.setText("strike items"); 
     b.setOnClickListener(new View.OnClickListener(){ 
      public void onClick(View view) { 
       for(String txt:arr) 
       { 
        ((TextView)(lv.getChildAt(ad.getPosition(txt)))). 
        setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG); 
       } 
      } 
     }); 

     ll.addView(lv); 
     ll.addView(b); 
     setContentView(ll); 
    } 
} 

환호 응답에 대한

+0

감사합니다. 이 줄에서 자식 텍스트의 null 값을 얻는 중입니다. TextView childtext = (TextView) list.getChildAt (i); 어떤 아이디어? – irobotxxx

+0

list.setAdapter (my_CheckList_Adapter); for 루프 전에 설정하십시오. –

+0

여기 ArrayAdapter http://sirinsevinc.wordpress.com/2010/05/11/109/를 사용하는 방법에 대한 좋은 기사가 있거나이 사이트의 어댑터에 대한 정보도 많이 있습니다 :) –

관련 문제