2012-02-12 69 views
0

我在我的代码中有一个奇怪的错误,我看不到问题。我有一个主要课程,我在里面有一门课。我在主类中声明了一个数组列表,如果我在该类的某个部分访问它,我可以获得所有信息,但是当我在内部类访问它时,它无法看到数据。为什么?类看到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是一个ListView和当它更新它的数据时,会有像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

它都不起作用。奇怪,不是吗? – 2012-02-12 17:27:37

+0

真的很奇怪。我认为你从哪里调用getCount()方法是有问题的。通过在getCount()方法中放置断点或放置日志消息来进行调试。 – 2012-02-12 17:30:59

+0

我已经把断点放在if(Main.data2!= null)(getcount),返回0并返回Main.data2.size();.当应用程序启动时,就像8次调用getcount和eclipse,在这两个参数中停止了8次,返回Main.data2.size();并返回0,但我可以看到data2有它的信息。我什么都不懂。也许我应该使用另一个代码只是为了能够使用一个处理文本和按钮点击的按钮来创建一个列表视图。你知道一些页面,我可以找到另一种方式吗?谢谢 – 2012-02-12 17:59:37

相关问题