无法将我的项目添加到recyclerView

时间:2016-08-19 12:48:13

标签: android android-studio

我试图将一些项目放在我的RecyclerView中,这些项目来自一个带来另一项活动数据的意图的信息

主要活动:

package com.example.android.contacts;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class Main2Activity extends AppCompatActivity {

    RecyclerView contactListView;
    private ViewAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        Intent intent2 = getIntent();
        String nameTxt = intent2.getStringExtra("ContactName");
        String phoneTxt = intent2.getStringExtra("PhoneNumber");
        String emailTxt = intent2.getStringExtra("Email");
        String addressTxt = intent2.getStringExtra("Address");

        contactListView = (RecyclerView) findViewById(R.id.listView);

        //getData(nameTxt,phoneTxt,emailTxt,addressTxt);

        adapter = new ViewAdapter(getApplication(), getData(nameTxt,phoneTxt,emailTxt,addressTxt));
        contactListView.setAdapter(adapter);
    }



    public  List<Contact> getData(String name, String phone, String email, String address){
        List<Contact> data = new ArrayList<>();
        //data.add(new Contact(name,phone,email,address));

        return data;
    }

    public void addNewContact (View view){

        Intent intent = new Intent (Main2Activity.this, MainActivity.class);
        startActivity(intent);

    }
}

适配器:

package com.example.android.contacts;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.Collections;
import java.util.List;

public class ViewAdapter extends RecyclerView.Adapter<ViewAdapter.MyViewHolder>{

    private LayoutInflater inflator;
    List<Contact> data = Collections.emptyList();


    public ViewAdapter (Context context, List<Contact> data){
        inflator=LayoutInflater.from(context);

        this.data=data;
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {


        View view = inflator.inflate(R.layout.custom_row, parent, false);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;

    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {

        Contact current = data.get(position);

        holder.name.setText(current.getName());
        holder.phone.setText(current.getPhone());
        holder.email.setText(current.getEmail());
        holder.address.setText(current.getAddress());

    }

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

    class MyViewHolder extends RecyclerView.ViewHolder{
        TextView name;
        TextView phone;
        TextView email;
        TextView address;

        public MyViewHolder(View itemView) {
            super(itemView);
            name=(TextView) itemView.findViewById(R.id.contactName);
            phone=(TextView) itemView.findViewById(R.id.phoneNumber);
            email=(TextView) itemView.findViewById(R.id.emailAddress);
            address=(TextView) itemView.findViewById(R.id.cAddress);
        }
    }
}

应用程序从其他活动获取数据,并且该数据以意图

进入主活动

3 个答案:

答案 0 :(得分:0)

这很简单,但我想告诉你,问题是什么。我猜你不理解我们想在评论中解释什么。没问题,每个人都从小开始(就像我们在德国说的那样):)。所以这是你的问题:

public  List<Contact> getData(String name, String phone, String email, String address){
        List<Contact> data = new ArrayList<>();
        //data.add(new Contact(name,phone,email,address));

        return data;
    }

两个sla //表示您已对代码进行了注释。所以它不再活跃了。要将这些值添加到列表中,您必须删除该斜杠。它看起来像:

public  List<Contact> getData(String name, String phone, String email, String address){
        List<Contact> data = new ArrayList<>();
        data.add(new Contact(name,phone,email,address));

        return data;
    }

你看到了区别吗?同样在这篇帖子中,在你的例子中它变灰了......

答案 1 :(得分:0)

以下是我获取数据的活动

package com.example.android.contacts;

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

public class MainActivity extends AppCompatActivity {

    EditText nameTxt, phoneTxt, emailTxt, addressTxt;

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

        nameTxt = (EditText) findViewById(R.id.txtName);
        phoneTxt = (EditText) findViewById(R.id.txtPhone);
        emailTxt = (EditText) findViewById(R.id.txtEmail);
        addressTxt = (EditText) findViewById(R.id.txtAddress);

    }
    public void addContact (View view){

        if (nameTxt.getText().length()==0){

            nameTxt.setError("Please enter the Name");

        }

        else if (phoneTxt.getText().length()==0){

            phoneTxt.setError("Please enter the Phone Number");

        }

        else if (emailTxt.getText().length()==0){

            emailTxt.setError("Please enter the Email");

        }

        else if (addressTxt.getText().length()==0){

            addressTxt.setError("Please enter the Address");

        }

        else{

            Intent intent = new Intent (this, Main2Activity.class);
            intent.putExtra("ContactName",nameTxt.getText().toString());
            intent.putExtra("PhoneNumber",phoneTxt.getText().toString());
            intent.putExtra("Email",emailTxt.getText().toString());
            intent.putExtra("Address",addressTxt.getText().toString());

            startActivity(intent);

        }
    }
}

答案 2 :(得分:0)

我喜欢这个问题但是还是谢谢你。 RecyclerView需要布局管理器。 然后我调用适配器并发送不需要的方法