Android从中创建自定义RecyclerView.Adapter并创建其他类

时间:2015-05-01 01:13:57

标签: java android android-recyclerview

好的人我有一个我从WebService填充的RecyclerView,我有一个DataProvider类来管理异步请求。因此,当获取数据时,我需要此DataProvider通知RecyclerView.Adapter有新数据。为此,我需要向RecyclerView.Adapter添加一个允许此通信的方法。但是当我使用这个新方法创建一个基类(扩展了RecyclerView.Adapter),然后创建自定义适配器时,它不会让我覆盖RecyclerView.Adapter方法。我做错了什么?

这是基类

package com.example.liam.arduinocontroller;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.os.StrictMode;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;


public class ControlClass extends ActionBarActivity implements OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        StrictMode.ThreadPolicy policy = new StrictMode.
                ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_control_class);
        View led1on = findViewById(R.id.led_1on);
        View led1off = findViewById(R.id.led_1off);
        View led2on = findViewById(R.id.led_2on);
        View led2off = findViewById(R.id.led_2off);

        led1on.setOnClickListener(this);
        led1off.setOnClickListener(this);
        led2on.setOnClickListener(this);
        led2off.setOnClickListener(this);

    }

    public void commandArduino(String url) {
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpParams httpParameters = httpclient.getParams();
            HttpConnectionParams.setTcpNoDelay(httpParameters, true);
            httpclient.execute(new HttpGet(url));

        } catch (Exception e) {
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_control_class, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View thisView) {



            switch(thisView.getId())


            {
                case R.id.led_1on:
                    commandArduino("http://192.168.0.101/?2");
                    Toast.makeText(getApplicationContext(), "led_1on", Toast.LENGTH_LONG).show();
                    break;
                case R.id.led_1off:
                    commandArduino("http://192.168.0.101/?3");
                    Toast.makeText(getApplicationContext(), "led_1off", Toast.LENGTH_LONG).show();
                    break;
                case R.id.led_2on:
                    commandArduino("http://192.168.0.101/?4");
                    Intent intent = new Intent(this, TempDisplay.class);
                    startActivity(intent);
                    break;
                case R.id.led_2off:
                    commandArduino("http://192.168.0.101/?5");
                    Intent j = new Intent(this, LightDisplay.class);
                    startActivity(j);
                    break;
            }




    }
}

这是扩展BaseRecyclerAdapter的自定义适配器

public abstract class BaseRecyclerAdapter extends RecyclerView.Adapter<BaseRecyclerAdapter.ViewHolder>{

public static class ViewHolder extends RecyclerView.ViewHolder{

    public ViewHolder (View v){
        super(v);
    }
}

public BaseRecyclerAdapter(RecyclerView rv){

}

public void setDataSet( String data) {
   //This is the method i need to add
}

}

我想这与类型参数有关,但我无法弄清楚发生了什么

0 个答案:

没有答案
相关问题