2012-02-12 7 views
0

코드에 이상한 오류가있어서 문제를 볼 수 없습니다. 나는 다른 클래스가있는 메인 클래스를 가지고 있습니다. 나는 메인 클래스에 선언 된 arraylist를 가지고 있고 클래스의 somepart에서 접근하면 모든 정보를 가질 수 있지만 내부 클래스에서 정보에 액세스하면 데이터를 볼 수 없다. 왜?클래스에 array.lengh가 있지만 데이터가 없습니다.

내부 클래스는 뷰리스트를 만들기위한 BaseAdapter입니다. 나는로 한 OnCreate 번들에서 전화 :

public class Main extends ListActivity implements OnClickListener { 
//new adapter 
private EfficientAdapter adap; 
//The variable which makes the error 
public static ArrayList<String> data2 = new ArrayList<String>();  
protected static Context mContext=null; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.nuevoingr); 

    data2.clear(); 
    data2.add("just adding 1"); 
    adap = new EfficientAdapter(this); 
    setListAdapter(adap); 

와 내부 클래스는 (문제가 그것의 끝에)입니다 :

public static class EfficientAdapter extends BaseAdapter implements Filterable { 
    private LayoutInflater mInflater; 
    private Context context; 

    public EfficientAdapter(Context context) { 
     // Cache the LayoutInflate to avoid asking for a new one each time. 
     mInflater = LayoutInflater.from(context); 
     this.context = context; 
    } 

    /** 
    * Make a view to hold each row. 
    * 
    * @see android.widget.ListAdapter#getView(int, android.view.View, 
    *  android.view.ViewGroup) 
    */ 
    public View getView(final int position, View convertView, ViewGroup parent) { 
     // A ViewHolder keeps references to children views to avoid 
     // unneccessary calls 
     // to findViewById() on each row. 
     ViewHolder holder; 

     // When convertView is not null, we can reuse it directly, there is 
     // no need 
     // to reinflate it. We only inflate a new View when the convertView 
     // supplied 
     // by ListView is null. 
     if (convertView == null) { 
      convertView = mInflater.inflate(R.layout.pruebas, null); 

      // Creates a ViewHolder and store references to the two children 
      // views 
      // we want to bind data to. 
      holder = new ViewHolder(); 
      holder.textLine = (TextView) convertView.findViewById(R.id.pr2); 
      holder.buttonLine = (ImageButton) convertView.findViewById(R.id.pr1); 


      convertView.setOnClickListener(new OnClickListener() { 
       private int pos = position; 

       @Override 
       public void onClick(View v) { 
        Toast.makeText(context, "Click-" + String.valueOf(pos), Toast.LENGTH_SHORT).show(); 
       } 
      }); 

      holder.buttonLine.setOnClickListener(new OnClickListener() { 
       private int pos = position; 

       @Override 
       public void onClick(View v) { 
        Toast.makeText(context, "Delete-" + String.valueOf(pos), Toast.LENGTH_SHORT).show();  
       } 
      }); 
      convertView.setTag(holder); 
     } else { 
      // Get the ViewHolder back to get fast access to the TextView 
      // and the ImageView. 
      holder = (ViewHolder) convertView.getTag(); 
     } 

     // Bind the data efficiently with the holder. 
     holder.textLine.setText(String.valueOf(position)); 

     return convertView; 
    } 

    static class ViewHolder { 
     TextView textLine; 
     ImageButton buttonLine; 
    } 

    @Override 
    public Filter getFilter() { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return 0; 
    } 


//Here it comes the problem. Before editing my program this code was provide to include  only a few values from 
a String array declared at the same moment of data2 called data. What I do now is to  declare an arraylist so 
I can include more values and then I refresh the adaptor and here I pass the arraylist  to an array to be equal as 
it was before. 
    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     String [] change = (String[]) data2.toArray(new String[data2.size()]); 
     return change.length; 
     //return data.length; 
    } 
//Same problem 
    @Override 
    public Object getItem(int position) { 
     // TODO Auto-generated method stub 

     String [] change = (String[]) data2.toArray(new String[data2.size()]); 
     return change[position]; 
     //return data[position]; 
    } 

} 

어떤 이상한 것은이 baseAdapter가 목록보기위한 것임을 및 데이터를 업데이트 할 때 arraylist.length와 같은 행이 있지만 데이터에는 포함되지 않으며 모든 행은 클릭 할 때 동일하게 감지됩니다.

답변

0

이 시도 :

@Override 
public int getCount() { 
    if(Main.data2!=null) 
    { 
     return Main.data2.size(); 
    } 
    else 
    { 
     return 0; 
    } 
} 

@Override 
public String getItem(int position) { 
    if(Main.data2!=null) 
    { 
     return Main.data2.get(position); 
    } 
    else 
    { 
     return null; 
    } 
} 
+0

모두 작동합니다. 이상하지? –

+0

정말 이상합니다. 거기에 뭔가 getCount() 메서드를 호출하는 잘못된 생각합니다. getCount() 메소드에 중단 점을 넣거나 로그 메시지를 삽입하여 디버그하십시오. –

+0

if (Main.data2! = null) (getcount)에 중단 점을 넣고 0을 반환하고 Main.data2.size();를 반환합니다. 앱이 시작될 때 getcount를 8 번 호출하고 이클립스가 두 인수에서 모두 8 번 멈추는 경우 Main.data2.size();를 반환합니다. 0을 반환하지만 data2에 정보가 있음을 알 수 있습니다. 나는 아무것도 이해할 수 없다. 어쩌면 그냥 텍스트 및 단추 클릭을 처리하는 단추로 listview 만들 수있는 다른 코드를 사용해야합니다. 다른 방법을 찾을 수있는 페이지를 아십니까? 감사 –

관련 문제