2012-03-26 5 views
2

simplecursoradapterの特定のファイル名を呼び出し元のアクティビティに戻したいと思います。 私はこのリンクを試しましたHow communicate between Adapter and ActivityとこれもData between activity and adapter しかしどちらかといえば私はどちらもしませんでした。私はhere.Kindly役立つアクティビティコードとアダプターのコードの両方を掲載していますSimpleCursorAdapterからActivityにファイル名を戻す

Activity 
videolist = (ListView) findViewById(R.id.VideoMusicList); 
     videolist.setAdapter(adapter); 

//アダプタ

public class SdCardAdapter extends SimpleCursorAdapter { 

static final String TAG = "[SongListAdapter]"; 
int position; 
CheckBox media_selected; 
final String SETTING_TODOLIST = "todolist"; 
private String chk; 
private Context context; 
private Object itemText; 
private ArrayList<string> selectedItems = new ArrayList<string>(); 
// String file; 
private Object convertView; 
// private final List<Model> list; 
int count; 
ListView listview; 
private List<Model> list; 
private Cursor videocursor; 
private LayoutInflater mInflater; 
private OnClickListener mClick; 
private OnCheckedChangeListener mChecked; 
String file_rel_path; 
String file_abs_path; 
String last_file; 

/** 
* The Class ViewHolder. 
*/ 
static class ViewHolder { 

    /** The sdcard_item. Layout of each item in the list view */ 
    RelativeLayout sdcard_item; 
    LinearLayout sdcard; 

    /** The title. Textview to display the song title */ 
    TextView media_name; 

    CheckBox media_selected; 
    ListView listview; 
    int position; 

} 


public SdCardAdapter(Context context, int layout, Cursor c, String[] from, 
     int[] to) { 
    super(context, layout, c, from, to); 

} 

public View newView(Context context, Cursor cursor, ViewGroup parent) { 
    final View v = super.newView(context, cursor, parent); 

    final Cursor filecursor = cursor; 
    final ViewHolder vh = new ViewHolder(); 
    vh.media_name = (TextView) v.findViewById(R.id.sdcard_title); 
    position = cursor.getPosition(); 
    count = cursor.getCount(); 
    vh.media_selected = (CheckBox) v.findViewById(R.id.sdcard_checkbox); 
    vh.media_selected 
      .setOnCheckedChangeListener(new OnCheckedChangeListener() { 

       public void onCheckedChanged(CompoundButton buttonView, 
         boolean isChecked) { 
        // TODO Auto-generated method stub 

        System.out.println("checkbox ckicked......"); 
        if (vh.media_selected.isChecked()) { 
         vh.media_selected.setId(1); 
         vh.media_selected.getId(); 
         last_file = file_rel_path; 
         file_rel_path = vh.media_name.getText().toString();       
         Log.d("filename_........", file_rel_path); 
         v.setBackgroundColor(Color.GRAY); 
        } else if (!vh.media_selected.isChecked()) { 
         file_rel_path=""; 
         vh.media_selected.setId(0); 
         vh.media_selected.getId(); 
         v.setBackgroundColor(Color.BLACK); 
        } 

        Log.d("Position", "" + position); 
        for (int i = 0; i < count; i++) { 

         filecursor.moveToPosition(i); 
         file_abs_path = filecursor.getString(filecursor 
           .getColumnIndex(MediaStore.Video.Media.TITLE)); 

         if (file_abs_path.equals(file_rel_path)) { 

          String file_path = filecursor.getString(filecursor 
            .getColumnIndex(MediaStore.Video.Media.DATA)); 
          Log.d("filename.......", file_path); 
         } 

        } 

       } 
      }); 

    vh.sdcard_item = (RelativeLayout) v.findViewById(R.id.sdcard_item); 
    v.setTag(vh); 

    return v; 

} 
} 

//これは私が私の以前の活動に渡したい文字列です

String file_path = filecursor.getString(filecursor.getColumnIndex(MediaStore.Video.Media.DATA)); 
+0

あなたのSdCardAdapterの静的変数にそれを保存し、それをアクティビティで取得できませんか? –

答えて

1

インターフェイスコールバックを使用:

In SdCardAdapterクラスcreate interface:

public interface SdCardAdapterListener 
{ 
    public void sendFilePath(String path); 
} 

SdCardAdapterコンストラクタでリスナーのparam追加:あなたにsendFilePath方法に送信するために単にSdCardAdapterにdelegate.sendFilePath(パス)を呼び出し、その後

SdCardAdapter adapter = new SdCardAdapter(this, layout, c, from, to, new SdCardAdapterListener() 
{ 
    @Override 
    public void sendFilePath(String path) 
    { 
     // do something with path 

    } 
}); 
    videolist = (ListView) findViewById(R.id.VideoMusicList); 
     videolist.setAdapter(adapter); 

:活動の

private SdCardAdapterListener delegate; 

public SdCardAdapter(Context context, int layout, Cursor c, String[] from, 
     int[] to, SdCardAdapterListener delegate) 
{ 
    super(context, layout, c, from, to); 
    this.delegate = delegate; 
} 

をアクティビティ。

+0

コンストラクタ内のSdCardAdapterのメンバに 'delegate'を格納すると、これは素晴らしい&きれいなアプローチです。 – zapl

+0

ありがとう、ちょっと私が編集した監督 –

+0

ちょっと感謝アリックス、それは働いた:) –

関連する問題