android - 将文件名从 SimpleCursorAdapter 返回到 Activity

标签 android

我想将一个特定的文件名从 simplecursoradapter 返回到调用它的 Activity 。 我试过这个链接 How communicate between Adapter and Activity还有这个Data between activity and adapter 但两者都没有帮助我。我在这里发布了 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;

}
}

//这是我要传递给之前 Activity 的字符串

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

最佳答案

使用接口(interface)回调:

在 SdCardAdapter 类中创建接口(interface):

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

在 SdCardAdapter 构造函数中添加一个监听器参数:

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;
}

在 Activity 中:

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);

然后,只需调用 SdCardAdapter 中的 delegate.sendFilePath(path) 即可发送到 Activity 中的 sendFilePath 方法。

关于android - 将文件名从 SimpleCursorAdapter 返回到 Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9867616/

相关文章:

android - Android 小部件中的 RTL 支持

java - 仅在 androidTest : Invoke-customs are only supported starting with Android O

android - 开发游戏

android - 从 Firebase Cloud Functions 调用 Facebook 图形 API

android - 如何克服此错误 :java. lang.OutOfMemoryError:位图大小超出 VM 预算

java - RecyclerView 内的分组开关

android - 从 Android 应用程序向流行的社交网络分享内容时,如何通过 [我的应用程序] 添加共享?

android - java.lang.NoClassDefFoundError : android. support.v7.internal.view.menu.MenuBuilder

java - Android:是否应该以异步方式访问应用程序中的数据库?

android - 隐藏android底部按钮使用expo build react native