2010-03-01 4 views
7

쇼핑 목록을 만들려고합니다. 이전에 항목을 선택했으면 CheckedTextView가 선택됩니다. 즉, 항목을 선택하면 부울 값이 true로 설정됩니다.CheckedTextView가 listview에서 확인 된 oncreate로 설정 되었습니까?

setText()로 CheckedTextView 텍스트를 변경하기 위해 getview 메소드를 재정 의하여했지만 CheckedTextView에서 setChecked (true) 메소드를 설정하면 체크하지 않습니다.

나는

어떤 생각) = 바로 내가 녹색 체크 표시를 볼 필요가 알다시피, 나는이 프로젝트에 나중에 프로젝트가 될 것의 getView()에있는 뷰를 재활용하지 오전 알아? 도와주세요.

내 xml 파일 :

test.xml의

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/text1" 
    android:layout_width="fill_parent" 
    android:layout_height="?android:attr/listPreferredItemHeight" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:gravity="center_vertical" 
    android:checkMark="?android:attr/textCheckMark" 
    android:paddingLeft="5dip" 
    android:paddingRight="50dip" 
    android:textColor="#000000" 
    android:background="#ffffff" 
    android:tag="text1" 
    android:text="testtestetstt" 
    android:focusable="false"/> 

listfippel.java

package nu.listfippel.mitt; 

import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.List; 

import android.app.ListActivity; 
import android.content.Context; 
import android.graphics.Color; 
import android.graphics.Typeface; 
import android.os.Bundle; 
import android.text.Spannable; 
import android.text.style.StrikethroughSpan; 
import android.text.style.StyleSpan; 
import android.view.Gravity; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.BaseAdapter; 
import android.widget.CheckBox; 
import android.widget.CheckedTextView; 
import android.widget.ListAdapter; 
import android.widget.TextView; 
import android.widget.Toast; 
import android.widget.AdapterView.OnItemClickListener; 

public class listfippel extends ListActivity { 

    //ArrayList<Ingred> ing; 
    Ingred i1 = new Ingred("banan", 2, 1, true); 
    Ingred i2 = new Ingred("äpple", 3, 1, false); 
    Ingred i3 = new Ingred("päron", 3, 1, true); 
    Ingred i4 = new Ingred("gröt", 3, 1, false); 
    Ingred i5 = new Ingred("bröd", 3, 1, false); 
    Ingred i6 = new Ingred("kyckling", 3, 1, false); 
    Ingred i7 = new Ingred("röd paprika", 3, 1, false); 
    Ingred i8 = new Ingred("tårta", 3, 1, false); 
    Ingred i9 = new Ingred("bappelsin", 3, 1, false); 
    Ingred i10 = new Ingred("gurka", 3, 1, true); 
    CheckedTextView ctv; 
    Ingred[] ingarray = { i1, i2, i3, i4, i5, i6, i7, i8, i9, i10 }; 


    Ingadapter adapter; 
    Ingred temp; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.list); 


     ArrayList<Ingred> ing = new ArrayList<Ingred>(Arrays.asList(ingarray)); 

     adapter = new Ingadapter(this, R.layout.test, ing); 
     //adapter = new IngArrayAdapter(ing); 

     OnItemClickListener il = new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View arg1, int index, 
        long arg3) { 

       temp = (Ingred)parent.getItemAtPosition(index); 


       if (temp.getCheck()){ 
        Toast toast = Toast.makeText(getApplicationContext(), 
          "Du avmarkerade nyss den\nklarmarkerade varan " + parent.getItemAtPosition(index).toString(), 
          Toast.LENGTH_LONG); 
        toast.setGravity(Gravity.CENTER, 0, 0); 
        toast.show(); 
       } 

       if (!temp.getCheck()){ 
        temp.setCheck(true); 

        //ctv.setChecked(true); 
       } 
       else { 
        temp.setCheck(false); 
       } 
       adapter.notifyDataSetChanged(); 
      } 
     }; 


     setListAdapter(adapter); 
     //setTitleColor(Color.RED); 
     getListView().setOnItemClickListener(il); 
     getListView().setChoiceMode(2); 



    } 
    class Ingadapter extends ArrayAdapter<Ingred>{ 

     public Ingadapter(Context context, int textViewResourceId, 
       List<Ingred> objects) { 
      super(context, textViewResourceId, objects); 
      // TODO Auto-generated constructor stub 
     } 


     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      // TODO Auto-generated method stub 
      LayoutInflater inflater=getLayoutInflater(); 
      View row = inflater.inflate(R.layout.test, null); 
      row.setTag(R.id.text1, row.findViewById(R.id.text1)); 

      CheckedTextView ctv =(CheckedTextView)row.getTag(R.id.text1); 

      Ingred temp = (Ingred)((AdapterView<?>) parent).getItemAtPosition(position); 

      if(temp.getCheck()){ 
       ctv.setChecked(true); 
      } 

      ctv.setText(temp.toString()); 

      return (row); 
     } 

    } 

    } 

Ingred.java 당신이를 사용하려는 경우

package nu.listfippel.mitt; 

public class Ingred { 

    private String namn; 
    @SuppressWarnings("unused") 
    private int volym; 
    private double antal; 
    private boolean check = false; 
    private String retstring; 

    public Ingred(String namn, double antal, int volym, boolean check){ 
     this.namn = namn; 
     this.antal = antal; 
     this.volym = volym; 
     this.check = check; 
    } 

    public String toString(){ 

     if (!this.check){ 
     retstring = namn + " \n(" + antal + " behövs)"; 
     } 
     else { 
      retstring = namn; 
     } 
     return retstring; 
    } 

    public boolean getCheck(){ 
     return this.check; 
    } 

    public void setCheck(boolean check){ 
     this.check = check; 
    } 
} 

답변

10

의 경우 ListViewsetSelection() 메서드를 사용해야합니다. 어떤 행을 선택했는지 나타내려면 행 바인딩 중에 직접 처리하지 말아야합니다.

+0

나는 getListView(). setChoiceMode (2)를 주석 처리했으며 현재 작동하지만, 왜 그런지 모르지만 나는 총 noob =이 자신을 exscuse한다. 내 부분에 솔리드 코드가 아닌 것처럼 느껴진다. 장기적으로 함께 할 것입니다, 팁 주셔서 감사합니다 =) – Sotarn

관련 문제