2012-02-12 19 views
0

私のコードには奇妙なエラーがあり、私は問題を見ることができません。私は内部に他のクラスがあるメインクラスを持っています。私はメインクラスで宣言されたarraylistを持っています。クラスの途中で私がそれにアクセスすると、私はすべての情報を持つことができますが、私が内部クラスでそれにアクセスすると、データを見ることができません。どうして?クラスは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にその情報があることがわかります。私は何も理解できません。たぶん私は、テキストとボタンのクリックの両方を処理するボタンを使ってリストビューを作成できるようにするために、別のコードを使うべきでしょう。私は別の方法を見つけることができるいくつかのページを知っていますか?ありがとう –

関連する問題