我有个问题。为什么recyclerView不可见。我已经编写了代码,以便ActivityListToSerach类可以从文本数据库中检索数据后检索带有文本的数组。数据下载到RecyclerViewAdapter完成后,我将数据发送到显示。
当我在ActivityListToSerach类中调用.size()时,得到0。
任何人都知道如何解决它。
我是一个新手程序员,请立即感谢您。

ConnectorSerach:

public class ConnectorSerach extends AsyncTask<String, Void, String>
{

Context context;

public ConnectorSerach (Context ctx)
{
    context = ctx;
}

public ArrayList<String> xNames = new ArrayList<>();
public ArrayList<String> xPrices = new ArrayList<>();

@Override
protected String doInBackground(String... params)
{
    String nameOfProduct = params[0];
    String result = "";

    try
    {
        URL url_login = new URL("http://192.168.100.9/szukajProduktu.php");
        HttpURLConnection httpURLConnection = (HttpURLConnection)url_login.openConnection();
        httpURLConnection.setDoInput(true);
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setRequestMethod("POST");

        OutputStream outputStream = httpURLConnection.getOutputStream();
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));

        String data = URLEncoder.encode("name_product", "UTF-8")+"="+URLEncoder.encode(nameOfProduct, "UTF-8");

        bufferedWriter.write(data);
        bufferedWriter.flush();

        bufferedWriter.close();
        outputStream.close();

        InputStream inputStream = httpURLConnection.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));

        String line = "";

        while ((line = bufferedReader.readLine()) != null)
        {
            result += line;
        }

        bufferedReader.close();
        inputStream.close();
        httpURLConnection.disconnect();

        return result;
    }
    catch (MalformedURLException e)
    {
        e.printStackTrace();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }

    return null;
}

@Override
protected void onPostExecute(String result)
{
    super.onPostExecute(result);
    try
    {
        JSONArray jsonArray = new JSONArray(result);
        for (int i = 0;i<jsonArray.length(); i++)
        {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            xNames.add(jsonObject.getString("nazwa"));
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    Intent intent = new Intent(context, ActivityListToSerach.class);
    context.startActivity(intent);
}


ActivityListToSerach:

package bjd.adrian.e_shop;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import java.util.ArrayList;

public class ActivityListToSerach extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list_to_serach);
    this.initRecyclerView();
}

public void initRecyclerView()
{
    ConnectorSerach connectorSerach = new ConnectorSerach(this);

    if(connectorSerach.getStatus().equals(AsyncTask.Status.FINISHED))
    {
        ArrayList<String> sNames = connectorSerach.xNames;
        ArrayList<String> sPrices = connectorSerach.xPrices;

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        RecyclerViewAdapter recyclerViewAdapter = new RecyclerViewAdapter(this, sNames, sPrices);

        recyclerView.setAdapter(recyclerViewAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setHasFixedSize(true);

    }

}


RecyclerViewAdapter:

package bjd.adrian.e_shop;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

public class RecyclerViewAdapter extends
RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>
{
private static final String TAG = "RecyclerViewAdapter";

ArrayList<String> mNames = new ArrayList<>();
ArrayList<String> mPrices = new ArrayList<>();
Context context;

public RecyclerViewAdapter(Context ctx, ArrayList<String> pNames, ArrayList<String> pPrices)
{
    mNames = pNames;
    mPrices = pPrices;
    context = ctx;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
{
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false);
    ViewHolder viewHolder = new ViewHolder(view);
    return viewHolder;
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position)
{
    Log.d(TAG, "onBindViewHolder: called");
    holder.nameTV.setText(mNames.get(position));
    holder.priceTV.setText(mPrices.get(position));
}

@Override
public int getItemCount()
{
    return mNames.size();
}




public class ViewHolder extends RecyclerView.ViewHolder
{
    TextView nameTV;
    TextView priceTV;

    public ViewHolder(View itemView)
    {
        super(itemView);

        nameTV = itemView.findViewById(R.id.title);
        priceTV = itemView.findViewById(R.id.priceTV);
    }
}

最佳答案

有一个执行:

package bjd.adrian.e_shop;

import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import java.util.ArrayList;

public class ListSerachActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list_serach);
}

ConnectorSerach connectorSerach = new ConnectorSerach(this);

public void onSerach(View view) {
    EditText serachET = (EditText) findViewById(R.id.serachET);
    String toSerachString = serachET.getText().toString();

    connectorSerach.execute(toSerachString);

}


}

关于java - RecyclerView不可见,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51180261/

10-10 17:09