java—将restclient类中的外部函数的返回值传递给单独活动中的列表适配器

5lwkijsr  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(195)

我正在开发一个android应用程序,它使用reformation2作为框架来进行rest调用。应用程序在列表视图中显示这些rest调用中的项目。我在一个活动中进行了其余的调用,并实现了一个运行良好的方法;由于我需要进行更多的rest调用,我创建了一个单独的rest客户机类,以便在那里实现所有必要的方法;正在进行rest调用,但listview仍为空。在尝试通过外部函数中的改装将api调用的响应传递到listview时,出现以下错误:
必需类型:提供的列表:void
这就是androidstudio中的错误。现在我可以想象´无法将函数“附加”到listview。
但是,接下来如何使用reformation2调用restapi,并从我创建的rest客户机类调用外部方法呢´你在这个和其他活动中创造了什么?
我们的想法是重构代码´在整个项目中没有大量的rest调用,而是有一个rest客户机类,在那里实现所有必要的方法,然后调用rest客户机和必要的方法´这是必要的。
我想保留这个结构,所以我希望得到任何关于如何将返回值(包含来自api的响应)从被调用的外部函数传递到列表适配器的提示或帮助,提前谢谢!
rest客户端:

package com.example.xxx;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import okhttp3.Credentials;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static com.example.xxx.MainListActivity.TAG;

public class RestClient {

    public static SimpleItemRecyclerViewAdapter adapter;
    private static android.content.Context context;
    public boolean mTwoPane;

    static Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(PalletteApi.BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    public RestClient(Context context) {
       this.context = context;
    }

    public static void getPalettes() {

        String basic = Credentials.basic("logisticApp", "-haisz8z23halsis");

        //creating the paletteApi interface
        PalletteApi palletteApi = retrofit.create(PalletteApi.class);

        //making the call object
        Call<List<Pallette>> call = palletteApi.getPallettes(basic);

        //call.execute();

        call.enqueue(new Callback<List<Pallette>>() {
            @Override
            public void onResponse(@NonNull Call<List<Pallette>> call, @NonNull Response<List<Pallette>> response) {

                if (response.isSuccessful()) {
                    Log.d(TAG, String.valueOf(response));
                    MainListActivity.setPostsToAdapter();

                } else {
                    int statusCode = response.code();
                    Toast.makeText(context, statusCode, Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<List<Pallette>> call, Throwable t) {
                Toast.makeText(context, t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
        return;
    }

    private void setupRecyclerView(@NonNull RecyclerView recyclerView) {
        adapter = new SimpleItemRecyclerViewAdapter((MainListActivity) context, mTwoPane);
        recyclerView.setAdapter(adapter);
    }

}

主列表活动:

package com.example.xxx;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.mancj.materialsearchbar.MaterialSearchBar;

import java.util.List;

/**
 * An activity representing a list of Items. This activity
 * has different presentations for handset and tablet-size devices. On
 * handsets, the activity presents a list of items, which when touched,
 * lead to a {@link MainDetailActivity} representing
 * item details. On tablets, the activity presents the list of items and
 * item details side-by-side using two vertical panes.
 */
public class MainListActivity extends AppCompatActivity {

    /**
     * Whether or not the activity is in two-pane mode, i.e. running on a tablet
     * device.
     */
    private boolean mTwoPane;
    public static final String TAG = "Response:" ;
    public static String type = "";
    public static final String TAGGG = "Scan Value" ;
    public static SimpleItemRecyclerViewAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_list);
        MaterialSearchBar searchBar = findViewById(R.id.searchBar);
        EditText editBarcode = (EditText) findViewById(R.id.editText);
        TextView editTextNumber = findViewById(R.id.editTextNumber);
        TextView content = (TextView) findViewById(R.id.content);
        LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        CustomSuggestionsAdapter customSuggestionsAdapter = new CustomSuggestionsAdapter(inflater);

        if (findViewById(R.id.main_detail_container) != null) {
            // The detail container view will be present only in the
            // large-screen layouts (res/values-w900dp).
            // If this view is present, then the
            // activity should be in two-pane mode.
            mTwoPane = true;
        }

       editBarcode.setOnKeyListener(new View.OnKeyListener() {

            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {

                // TODO Auto-generated method stub
                String code = editBarcode.getText().toString();
                if (code.matches("")) //{ if(code.trim().isEmpty())
                //|| editBarcode.getText().toString() > 100 )
                {
                Log.d(TAG, "Code ist leer");
                    Intent intent = new Intent(getApplicationContext(),MainListActivity.class);
                    //intent.putExtra("name",fruitNames[i]);
                    //intent.putExtra("image",fruitImages[i]);
                    startActivity(intent);
                }
                if (keyCode == KeyEvent.KEYCODE_ENTER && code.length() > 0) {
                    editBarcode.setText("");
                        return true;
                }
                Intent intent = new Intent(getApplicationContext(),MainDetailActivity.class);
                intent.putExtra("scannedCode", code);
                startActivity(intent);
                finish();
                return false;
            }

        });

        View recyclerView = findViewById(R.id.main_list);
        assert recyclerView != null;
        setupRecyclerView((RecyclerView) recyclerView);
        RestClient.getPalettes();
    }

    public static void setPostsToAdapter() {
        List<Pallette> responseBody = RestClient.getPalettes();
        List<Pallette> postList = responseBody;
        adapter.setPostList(postList);
        adapter.notifyDataSetChanged();

    }

    private void setupRecyclerView(@NonNull RecyclerView recyclerView) {
        adapter = new SimpleItemRecyclerViewAdapter(this, mTwoPane);
        recyclerView.setAdapter(adapter);
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Log.i(TAGGG, "Scanergebnis"+ keyCode);
        //I think you'll have to manually check for the digits and do what you want with them.
        //Perhaps store them in a String until an Enter event comes in (barcode scanners i've used can be configured to send an enter keystroke after the code)
        return true;
    }

}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题