0

20 행의 ListView 안에 EditText가 있습니다. 버튼을 클릭하면 모든 편집 텍스트 텍스트를 원합니다. 이 응용 프로그램은 버튼을 클릭 할 때 NullPointerException 오류가 발생하는 충돌을 가져옵니다. 첫 번째 11 개의 행에 대한 값을 제공합니다.이 후 뷰가 스크롤 된 것으로 생각되면 충돌이 발생합니다.ListView 내에서 EditText 텍스트를 가져 오는 방법

완전한 어댑터 코드

@SuppressLint("NewApi") public class CustomAdapter extends BaseAdapter implements OnClickListener { 

/*********** Declare Used Variables *********/ 
private Activity activity; 
private ArrayList data; 
private static LayoutInflater inflater=null; 
public Resources res; 
ListModel tempValues=null; 
int i=0; 

int j=0; 

public ArrayList<String> selectedStrings = new ArrayList<String>(); 


/************* CustomAdapter Constructor *****************/ 
public CustomAdapter(Activity a, ArrayList d,Resources resLocal) { 

    /********** Take passed values **********/ 
    activity = a; 
    data=d; 
    res = resLocal; 

    /*********** Layout inflator to call external xml layout() **********************/ 
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

} 

/******** What is the size of Passed Arraylist Size ************/ 
public int getCount() { 

    if(data.size()<=0) 
     return 1; 
    return data.size(); 
} 

public Object getItem(int position) { 
    return data.get(position); 
} 


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

/********* Create a holder to contain inflated xml file elements ***********/ 
public static class ViewHolder{ 

    public TextView text; 
    public TextView text1; 
    public EditText text2; 
    public TextView textWide; 
    public ImageView image; 
    public CheckBox check; 

} 

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



    View vi=convertView; 
    final ViewHolder holder; 

    if(convertView==null){ 

     /********** Inflate tabitem.xml file for each row (Defined below) ************/ 
     vi = inflater.inflate(R.layout.tabitem, null); 

     /******** View Holder Object to contain tabitem.xml file elements ************/ 
     holder=new ViewHolder(); 
     holder.text=(TextView)vi.findViewById(R.id.textView1); 

     holder.check =(CheckBox)vi.findViewById(R.id.checkBox1); 
       holder.text2=(EditText)vi.findViewById(R.id.editText1); 



     holder.check.setOnClickListener(new OnClickListener(){ 

      @Override 
      public void onClick(View v) 
      { 
       if (((CheckBox) v).isChecked()) 
       { 
        holder.text2.setText("Checked"); 


       } 
       else 
       { 
        holder.text2.setText("Not Checked"); 

       } 
      } 
     }); 

     vi.setTag(holder); 



     j++; 

     if(j<8){ 
     holder.text2.setVisibility(View.INVISIBLE); 
     holder.check.setX(-150); 
     holder.check.setVisibility(View.VISIBLE); 

     }else 
     { 
      holder.check.setVisibility(View.INVISIBLE); 
      holder.text2.setVisibility(View.VISIBLE); 

     } 



    } 
    else 
     holder=(ViewHolder)vi.getTag(); 

    holder.text2.getText(); 
     if(data.size()<=0) 
    { 
     holder.text.setText("No Data"); 

     holder.text2.getText(); 

    } 
    else 
    { 
     /***** Get each Model object from Arraylist ********/ 
     tempValues=null; 
     tempValues = (ListModel) data.get(position); 

     /************ Set Model values in Holder elements ***********/ 
     holder.text.setText(tempValues.getCompanyName()); 

     vi.setOnClickListener(this);  } 

      return vi; 
} 

@Override 
public int getViewTypeCount() { 
    return getCount(); 
} 


@Override 
public int getItemViewType(int position) { 
    return position; 
} 


@Override 
public void onClick(View v) { 
     Log.v("CustomAdapter", "=====Row button clicked"); 
TextView tv = (TextView)v.findViewById(R.id.textView1); 
    int pos = (Integer)tv.getTag(); 
    Log.d("position of clicked item is", ""+pos); 

} 

/********* Called when Item click in ListView ************/ 
private class OnItemClickListener implements OnClickListener{   
    private int mPosition; 

    OnItemClickListener(int position){ 
     mPosition = position; 
    } 

    @Override 
    public void onClick(View arg0) { 
     CustomListViewAndroidExample sct = (CustomListViewAndroidExample)activity; 
     sct.onItemClick(mPosition); 
    }    
} 

} 11 행 후 실패

버튼을 클릭 이벤트 ..

Button button =(Button)findViewById(R.id.button1); 
    button.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 

for (int i=0;i< 20;i++){ 
     View vListSortOrder; 
     vListSortOrder=list.getChildAt(i);  

     EditText edit=(EditText)vListSortOrder. findViewById(R.id.editText1); 
     String temp1223=edit.getText().toString();} 

LOG

04-28 03:33:23.542: E/AndroidRuntime(4917): FATAL EXCEPTION: main 
04-28 03:33:23.542: E/AndroidRuntime(4917): java.lang.NullPointerException 
04-28 03:33:23.542: E/AndroidRuntime(4917): at com.androidexample.customlistview.CustomListViewAndroidExample$1.onClick(CustomListViewAndroidExample.java:89) 
04-28 03:33:23.542: E/AndroidRuntime(4917): at android.view.View.performClick(View.java:4240) 
04-28 03:33:23.542: E/AndroidRuntime(4917): at android.view.View$PerformClick.run(View.java:17721) 
04-28 03:33:23.542: E/AndroidRuntime(4917):  at android.os.Handler.handleCallback(Handler.java:730) 
+1

추가하시기 바랍니다 로그 충돌 – SacreDeveloper

+0

'앱이 인 button.'의 클릭에 NullPointerException이 오류를주는 충돌 얻는다? – GrIsHu

+0

@SacreDeveloper를 확인하십시오 – user3226440

답변

0

모델의 listview에 표시 할 editext 및 기타 정보의 내용을 저장하고 해당 모델의 arraylist를 작성해야합니다. 이렇게하면 스크롤 할 때 목록 항목 상태를 유지하고 적절한 내용을 얻을 수 있습니다.

그리고 버튼에 해당 모델의 ArrayList를에서 콘텐츠를 클릭

...

2

사용 vListSortOrder = list.getAdapter()의 getView (I, NULL, NULL).;

대신 vListSortOrder = list.getChildAt (i);

감사

관련 문제