显示从SQLite到其他RecyclerView活动的详细数据

时间:2017-01-04 19:34:17

标签: android android-intent android-recyclerview android-sqlite

我已经阅读了几天的教程和案例研究。但我的代码实现有问题。

我无法使用MainMuriddActivity中的键将数据从数据库中检索到DetailsMuriddActivity。这是我的代码。

MainMuriddActivity.java

    public class MainMuriddActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private LinearLayoutManager layoutManager;
    private MuriddAdapter adapter;
    private DBHandler dbHandler;
    private TextView txt_resultadapter;
    public  static MainMuriddActivity mma;

    private List<Muridd> muriddList = new ArrayList<>();
    FloatingActionButton fab;

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

        //fab aksi
        fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainMuriddActivity.this, TambahMuriddActivity.class);
                startActivity(i);
            }
        });
        //end fab
       initComponents();
       initRecyclerView();
       cekDataRecyclerView();
    }

    // FUNGSI INI UNTUK MENG-INIT RECYLERVIEW BESERTA ADAPTERNYA
    private void initRecyclerView () {
        recyclerView = (RecyclerView) findViewById(R.id.rv_muridd);
        recyclerView.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);

        recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));

        mma = this;
        dbHandler = new DBHandler(MainMuriddActivity.this);
        refresh();
    }

    public void refresh(){
        muriddList = dbHandler.getSemuaMuridd();
        adapter = new MuriddAdapter(muriddList);
        recyclerView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }
    private void initComponents() {
        txt_resultadapter = (TextView) findViewById(R.id.txt_resultadapter);
    }

    // FUNGSI INI UNTUK MENGECEK APAKAH ADA DATA DI DALEM RECYCLERVIEW ATAU TIDAK
   private void cekDataRecyclerView() {
        if (adapter.getItemCount() == 0) {
            txt_resultadapter.setVisibility(View.VISIBLE);
            recyclerView.setVisibility(View.GONE);
        } else {
            txt_resultadapter.setVisibility(View.GONE);
            recyclerView.setVisibility(View.VISIBLE);

            recyclerView.addOnItemTouchListener(
                    new RecyclerItemClickListener(getApplicationContext(), new RecyclerItemClickListener.OnItemClickListener() {
                        @Override
                       public void onItemClick(View view, int position) {
                       // TODO Handle item click
                            Muridd murid = muriddList.get(position);
                            String nama = murid.getNama();
                            Toast.makeText(MainMuriddActivity.this, "Klik di " + nama, Toast.LENGTH_SHORT).show();
                           Intent i = new Intent(MainMuriddActivity.this, DetailsMuriddActivity.class);
                                    String getNama = null;
                                    i.putExtra("nama", getNama);
                                    muriddList.get(position).getNama();
                                    startActivity(i);                            }
                    })
            );
        }
    }
}

DetailsMuriddActivity.java

public class DetailsMuriddActivity extends AppCompatActivity {

    DBHandler dbHandler;
    private DetailAdapter adapter;
    private Muridd muridd;
    protected Cursor cursor;
    TextView txt_resultnomor, txt_resultnama, txt_resulttempatlahir, txt_resulttanggallahir, txt_resultagama,
            txt_resultjeniskelamin, txt_resultalamat, txt_resultnamaayah, txt_resulttlpayah, txt_resultkerjaayah, txt_resultnamaibu,
            txt_resulttlpibu, txt_resultkerjaibu;


    private List<Muridd> muriddList = new ArrayList<>();

    public Muridd getItem(int position) {
        return muriddList.get(position);
    }

    @Override
    public Intent getIntent() {
        return super.getIntent();
    }

    public Muridd getMuridd() {
        return muridd;
    }

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

        dbHandler = new DBHandler(this);

        //
        Intent i=getIntent();
        final String nomor=i.getExtras().getString("nomor");
        final String nama=i.getExtras().getString("nama");

        txt_resultnomor = (TextView) findViewById(R.id.txt_resultnomor);
        txt_resultnama = (TextView) findViewById(R.id.txt_resultnama);
        txt_resulttempatlahir = (TextView) findViewById(R.id.txt_resulttempatlahir);
        txt_resulttanggallahir = (TextView) findViewById(R.id.txt_resulttanggallahir);
        txt_resultagama = (TextView) findViewById(R.id.txt_resultagama);
        txt_resultjeniskelamin = (TextView) findViewById(R.id.txt_resultjeniskelamin);
        txt_resultalamat = (TextView) findViewById(R.id.txt_resultalamat);
        txt_resultnamaayah = (TextView) findViewById(R.id.txt_resultnamaayah);
        txt_resulttlpayah = (TextView) findViewById(R.id.txt_resulttlpayah);
        txt_resultkerjaayah = (TextView) findViewById(R.id.txt_resultkerjaayah);
        txt_resultnamaibu = (TextView) findViewById(R.id.txt_resultnamaibu);
        txt_resulttlpibu = (TextView) findViewById(R.id.txt_resulttlpibu);
        txt_resultkerjaibu = (TextView) findViewById(R.id.txt_resultkerjaibu);

        //
        txt_resultnomor.setText(nomor);
        txt_resultnama.setText(nama);

        SQLiteDatabase db = dbHandler.getReadableDatabase();
        cursor = db.rawQuery("SELECT * FROM table_muridd WHERE nama = '" +
                txt_resultnama.getText()+"'",null);

        cursor.moveToFirst();
        if (cursor.getCount()>0)
        {
            cursor.moveToPosition(0);
            txt_resultnomor.setText(cursor.getString(0).toString());
            txt_resultnama.setText(cursor.getString(1).toString());
            txt_resulttempatlahir.setText(cursor.getString(2).toString());
            txt_resulttanggallahir.setText(cursor.getString(3).toString());
            txt_resultagama.setText(cursor.getString(4).toString());
            txt_resultjeniskelamin.setText(cursor.getString(5).toString());
            txt_resultalamat.setText(cursor.getString(6).toString());
            txt_resultnamaayah.setText(cursor.getString(7).toString());
            txt_resulttlpayah.setText(cursor.getString(8).toString());
            txt_resultkerjaayah.setText(cursor.getString(9).toString());
            txt_resultnamaibu.setText(cursor.getString(10).toString());
            txt_resulttlpibu.setText(cursor.getString(11).toString());
            txt_resultkerjaibu.setText(cursor.getString(12).toString());
        }
    }   
}

这里是MuriddAdapter

 public class MuriddAdapter extends RecyclerView.Adapter<MuriddAdapter.MuriddViewHolder> {

    private List<Muridd> muriddList = new ArrayList<>();

    public MuriddAdapter(List<Muridd> muriddList) {
        this.muriddList = muriddList;
    }

    @Override
    public MuriddAdapter.MuriddViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_muridd, parent, false);
        MuriddViewHolder muriddViewHolder = new MuriddViewHolder(view);
        return muriddViewHolder;
    }

    @Override
    public void onBindViewHolder(MuriddAdapter.MuriddViewHolder holder, int position) {
        holder.txt_resultnomor.setText(muriddList.get(position).getNomor());
        holder.txt_resultnama.setText(muriddList.get(position).getNama());
        holder.txt_resulttempatlahir.setText(muriddList.get(position).getTempat_lahir());

 }

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

    public static class MuriddViewHolder extends RecyclerView.ViewHolder {

        TextView txt_resultnomor;
        TextView txt_resultnama;
        TextView txt_resulttempatlahir;


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

            txt_resultnomor = (TextView) itemView.findViewById(R.id.txt_resultnomor);
            txt_resultnama = (TextView) itemView.findViewById(R.id.txt_resultnama);
            txt_resulttempatlahir = (TextView) itemView.findViewById(R.id.txt_resulttempatlahir);
                }
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }
}

我也创建了DetailsMuriddAdapter。我应该改进哪一部分?

1 个答案:

答案 0 :(得分:0)

好像你应该替换这个

String getNama = null;
i.putExtra("nama", getNama);
muriddList.get(position).getNama();

用这个

String getNama = muriddList.get(position).getNama();
i.putExtra("nama", getNama);

另外,你没有为&#34; nomor&#34;设定价值。键,所以 nomor 将在你的情况下 null

final String nomor=i.getExtras().getString("nomor");