Android-recycleView无法显示,我不知道热,以显示我的TextView的价值

时间:2016-09-30 06:36:45

标签: android

我有两个问题作为标题

首先是我的recycleView无法显示,我返回MyAdapter类中的void onCreateViewHolder和MainActivity Class中的setAdapter视图,但它不起作用

第二个似乎是我的List arrayList为null 我从Json成功获得了四个值,因为我可以看到Log:FinallyJson信息 我尝试将四个值用于两个代码

Transaction transaction = new Transaction(name, address, tel, words);    
arrayList.add(transaction);

它也不起作用。这让我整天困惑。如何展示recycleView并让setAdaper四个值成功。

有人可以教我解决方案吗?

这是我的三个课程:

public class *MyAdapter* extends RecyclerView.Adapter< MyAdapter.MyViewHolder >{
    private Context context;
    private List<Transaction> mDatas;

    public MyAdapter(Context context, List<Transaction> datas) {
        this.context = context;
        this.mDatas = datas;
    }
    public class MyViewHolder extends RecyclerView.ViewHolder{
        private  TextView name,address,tel,words;
        public MyViewHolder(View itemView) {
            super(itemView);
            name=(TextView)itemView.findViewById(R.id.name_textView);
            address=(TextView)itemView.findViewById(R.id.address_textView);
            tel=(TextView)itemView.findViewById(R.id.tel_textView);
            words=(TextView)itemView.findViewById(R.id.words_textView);
        }
    }
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view=View.inflate(parent.getContext(),R.layout.cardview,null);
        MyViewHolder myViewHolder=new MyViewHolder(view);
        return myViewHolder;
    }
    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.name.setText(mDatas.get(position).getName().toString());
        holder.address.setText(mDatas.get(position).getAddress().toString());
        holder.tel.setText(mDatas.get(position).getTel().toString());
        holder.words.setText(mDatas.get(position).getWords().toString());
    }
    @Override
    public int getItemCount() {
        return mDatas.size();
    }
}

public class Transaction {
    String name;
    String address;
    String tel;
    String words;

    public Transaction() {
    }

    public Transaction(String name, String address, String tel, String words) {
        this.name = name;
        this.address = address;
        this.tel = tel;
        this.words = words;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }

    public String getWords() {
        return words;
    }

    public void setWords(String words) {
        this.words = words;
    }
}

public class MainActivity extends AppCompatActivity {

    private final String TAG = "MainActivity";
    private final String url = "http://data.coa.gov.tw/Service/OpenData/EzgoTravelFoodStay.aspx";
    public RecyclerView recyclerView;
    public List<Transaction> arrayList = new ArrayList<>();

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

        new MyTask().execute(url);
        recyclerView = (RecyclerView) findViewById(R.id.recycleView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        recyclerView.setAdapter(new MyAdapter(this,arrayList));

        Log.d("Array:",arrayList.toString());
    }
    class MyTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... strings) {
            String url = strings[0];
            try {
                String routeJson = getRouteJson(url);
                return routeJson;
            } catch (IOException ex) {
                Log.d(TAG, ex.toString());
                return null;
            }
        }
        @Override
        protected void onPostExecute(String s) {
            showRoute(s);    
        }
    }
    private String getRouteJson(String url) throws IOException {
        HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
        int responseCode = connection.getResponseCode();
        StringBuilder jsonIn = new StringBuilder();
        if (responseCode == 200) {
            BufferedReader bf = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            while ((line = bf.readLine()) != null) {
                jsonIn.append(line);
            }
        } else {
            Log.d(TAG, "responseCode:" + responseCode);
        }
        connection.disconnect();
        Log.d(TAG, "jsonIn:" + jsonIn);
        return jsonIn.toString();
    }
    private void showRoute(String route) {
        try {
            JSONArray jsonArray = new JSONArray(route);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                Log.d("jsonObject", jsonObject.toString());
                String name = jsonObject.getString("Name").toString();
                String address = jsonObject.getString("Address").toString();
                String tel = jsonObject.getString("Tel").toString();
                String words = jsonObject.getString("HostWords").toString();
                Transaction transaction = new Transaction(name, address, tel, words);
                arrayList.add(transaction);
                Log.d("FinallyJson:", name + "," + address + "," + tel + "," + words);
            }
        } catch (JSONException ex) {
            ex.printStackTrace();
        }
    }
}

3 个答案:

答案 0 :(得分:1)

youradapter.notifyDataChanged()方法填充arraylist后调用showRoute()。这是你的问题。

答案 1 :(得分:0)

尝试在字段中存储适配器的引用: public RecyclerView recyclerView; private mAdapter adapter; 并在for循环后调用notifyDatasetChanged(): adapter.notifyDatasetChanged();

答案 2 :(得分:0)

showRoute(s)函数中的onPostExecute后调用此行:

recyclerView.setAdapter(new MyAdapter(this,arrayList));

或者您必须使适配器全局并为其设置arraylist。然后,您可以致电notifyDatasetChanged刷新列表。