2013-07-05 17 views
3

我有一個getView類,它處理的onclick監聽器將刪除Arraylist中的數據,該數據位於另一個在活動中擴展的類中,問題是它返回一個null指針。如何從另一個活動刪除數組列表中的數據

這裏是我的活動它處理的ArrayList

public ArrayList<String> imagesFileName = new ArrayList<String>(); 
public ArrayList<HashMap<String, String>> mylist; 


//method for deleting data in array list and will going to use in another class 
public void deleteFile(int i){ 
    imagesFileName.remove(i); 
    mylist.remove(i); 
    adapter.notifyDataSetInvalidated(); 

} 

這裏是我的類,它擁有getView()和將要使用我在活動中聲明的方法

Activity activity = new Activity(); 
@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    LayoutInflater inflater = LayoutInflater.from(context); 
    View rowView = null; 
    try{ 
    rowView = inflater.inflate(resource, null, true); 
    TextView textViewTitle = (TextView) rowView.findViewById(to[0]); 
    EditText textViewSubtitle = (EditText) rowView.findViewById(to[1]); 
    TextView textViewSubtitle1 = (TextView) rowView.findViewById(to[2]); 
    TextView textViewSubtitle2 = (TextView) rowView.findViewById(to[3]); 
    final TextView textViewSubtitle3 = (TextView) rowView.findViewById(to[4]); 
    TextView textViewId = (TextView) rowView.findViewById(to[5]); 

    final String id = textViewId.getText().toString(); 
     textViewSubtitle3.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View view) { 
       // TODO Auto-generated method stub 

       activity.deleteFile(id);//here is my method in my Activiy 
      } 
     }); 

當我在相同的活動中使用該方法時,它就起作用。但是當我將它用於另一個類時,它會返回一個indexoutofbounds。

回答

3

我終於搞清楚了,我需要的是將我的Activity和Class加入到一個頁面中,以便能夠訪問我的數組列表。

0

deleteFile()的參數類型是整數,但是您通過在getView()函數中傳遞字符串來調用它。你可以先檢查這個問題。

+0

我已經解析到int但不起作用。 – NewDroidDev

0

就我個人而言,我會把數組放在共享首選項中。這意味着您將以標準且易於理解的方式訪問數據。

相關問題