使用活动中的全局变量在适配器中实现

时间:2016-08-22 14:50:53

标签: java android android-intent adapter

我一直在寻找答案,而我只是四处转转。 我试图让我的应用程序与谷歌驱动器一起工作。 我的问题是我在一个类中调用的适配器中使用了一个intent,我想传递我从另一个活动中获得的intent变量。

我的内心课程:

public class QueryFilesActivity extends BaseDemoActivity {

    private ListView mResultsListView;
    private ResultsAdapter mResultsAdapter;
    Date date = new Date();
    String stringDate = DateFormat.getDateInstance().format(date);
    String ecrire;
    String ville;
    String container;
    @Override
    protected void onCreate(Bundle b) {
        super.onCreate(b);
        setContentView(R.layout.activity_listfiles);
        mResultsListView = (ListView) findViewById(R.id.listViewResults);
        mResultsAdapter = new ResultsAdapter(this);
        mResultsListView.setAdapter(mResultsAdapter);
        Intent i = getIntent();
        ecrire = i.getStringExtra("test");
        ville = i.getStringExtra("ville");
        container = i.getStringExtra("cont");
    }

    /**
     * Clears the result buffer to avoid memory leaks as soon
     * as the activity is no longer visible by the user.
     */
    @Override
    protected void onStop() {
        super.onStop();
        mResultsAdapter.clear();
    }

    @Override
    public void onConnected(Bundle connectionHint) {
        super.onConnected(connectionHint);
        Query query = new Query.Builder()
                .addFilter(Filters.eq(SearchableField.TITLE, stringDate+"_"+ville+2))
                .build();
        Drive.DriveApi.query(getGoogleApiClient(), query)
                .setResultCallback(metadataCallback);
    }

    final private ResultCallback<MetadataBufferResult> metadataCallback = new
            ResultCallback<MetadataBufferResult>() {
                @Override
                public void onResult(MetadataBufferResult result) {
                    if (!result.getStatus().isSuccess()) {
                        showMessage("Problem while retrieving results");
                        return;
                    }
                    mResultsAdapter.clear();
                    mResultsAdapter.append(result.getMetadataBuffer());
                }
            };
}

我的适配器:

public class ResultsAdapter extends DataBufferAdapter<Metadata> {


    public ResultsAdapter(Context context) {
        super(context, android.R.layout.simple_list_item_1);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(getContext(),
                    android.R.layout.simple_list_item_1, null);
        }
        Metadata metadata = getItem(position);

        String resultat = metadata.getAlternateLink();
        String S1 = resultat.substring(32,60);
        TextView titleTextView =
                (TextView) convertView.findViewById(android.R.id.text1);
        titleTextView.setText(S1);
        Intent intent = new Intent(getContext(), EditContentsActivity.class);
        intent.putExtra("file", S1);



        getContext().startActivity(intent);


        return convertView;
    }

}

我想在我的意图中传递我的适配器意图。 有什么建议吗?

1 个答案:

答案 0 :(得分:0)

我只是忘了把我的第三堂课记录在我的车里。

public class EditContentsActivity extends BaseDemoActivity {
String saver;
String numero;
String file;
String ville;


private static final String TAG = "EditContentsActivity";

@Override
public void onConnected(Bundle connectionHint) {
    super.onConnected(connectionHint);

    final ResultCallback<DriveApi.DriveIdResult> idCallback = new ResultCallback<DriveApi.DriveIdResult>() {
        @Override
        public void onResult(DriveApi.DriveIdResult result) {
            if (!result.getStatus().isSuccess()) {
                showMessage("Cannot find DriveId. Are you authorized to view this file?");
                return;
            }
            DriveId driveId = result.getDriveId();
            DriveFile file = driveId.asDriveFile();
            new EditContentsAsyncTask(EditContentsActivity.this).execute(file);
        }
    };
    Intent i = getIntent();
    file = i.getStringExtra("file");
    ville = i.getStringExtra("localite");

    Drive.DriveApi.fetchDriveId(getGoogleApiClient(), file)
            .setResultCallback(idCallback);
}

public class EditContentsAsyncTask extends ApiClientAsyncTask<DriveFile, Void, Boolean> {

    public EditContentsAsyncTask(Context context) {
        super(context);
    }

    @Override
    protected Boolean doInBackgroundConnected(DriveFile... args) {
        DriveFile file = args[0];
        try {
            DriveApi.DriveContentsResult driveContentsResult = file.open(
                    getGoogleApiClient(), DriveFile.MODE_READ_WRITE, null).await();
            if (!driveContentsResult.getStatus().isSuccess()) {
                return false;
            }
            Intent i = getIntent();
            saver = i.getStringExtra("test");
            numero = i.getStringExtra("cont");
            double e1 = i.getDoubleExtra("lat", 0);
            double e2 = i.getDoubleExtra("longi", 0);

            DriveContents driveContents = driveContentsResult.getDriveContents();
            ParcelFileDescriptor parcelFileDescriptor = driveContents.getParcelFileDescriptor();
            FileInputStream fileInputStream = new FileInputStream(parcelFileDescriptor.getFileDescriptor());
            fileInputStream.read(new byte[fileInputStream.available()]);
            FileOutputStream fileOutputStream = new FileOutputStream(parcelFileDescriptor.getFileDescriptor());
            Writer writer = new OutputStreamWriter(fileOutputStream);
            writer.write("\n container "+numero+" : "+saver+"\nlatitude : "+e1+" longitude : "+e2+ville);
            writer.close();
            fileOutputStream.close();
            //OutputStream outputStream = driveContents.getOutputStream();
            //outputStream.write("Hello world".getBytes());
            com.google.android.gms.common.api.Status status =
                    driveContents.commit(getGoogleApiClient(), null).await();
            return status.getStatus().isSuccess();
        } catch (IOException e) {
            Log.e(TAG, "IOException while appending to the output stream", e);
        }
        return false;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        if (!result) {
            showMessage("Error while editing contents");
            return;
        }
        showMessage("les données ont bien été enregistrées");
    }
}

} 我的变量是空的