繁体   English   中英

单击按钮即可新建RecyclerView项目

[英]New RecyclerView item on click of button

我对Android有点陌生,因此请原谅所有缺点。

我在activity_main.xml有一个FloatingActionButton,每当单击它时,我都希望它带我进入具有两个EditText字段的新活动,并且那里还有另一个FAB。
我想做的是,每当输入文本并按下activity_new_goal.xml的FAB时,都应该使用@+id/edit_goalTitle文本中的文本创建一个RecyclerView。
它向我显示return data;中不兼容类型的错误return data; 并且显示无法解析MainActivity.java方法getData()

这是我的代码:

MainActivity.java

public class MainActivity extends AppCompatActivity {

private Toolbar toolbar;
private RVAdapter adapter;
private RecyclerView recyclerView;
private EditText editGoalTitle;
private String stringGoalTitle;

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

    toolbar = (Toolbar) findViewById(R.id.app_bar);
    setSupportActionBar(toolbar);

    getSupportActionBar().setDisplayShowHomeEnabled(true);
    NavigationDrawerFragment drawerFragment = (NavigationDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
    drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), (Toolbar) findViewById(R.id.app_bar));

    recyclerView = (RecyclerView) findViewById(R.id.goalList);
    recyclerView.setHasFixedSize(true);
    adapter = new RVAdapter(getApplicationContext(), getData());
    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));



}

public RVData mSetRecyclerGoalTitle(View view) {

    editGoalTitle = (EditText) findViewById(R.id.edit_goalTitle);
    editGoalTitle.setText(R.string.dummyTxt);
    stringGoalTitle = editGoalTitle.getText().toString();


    public List<RVData> getData () {
        List<RVData> data = new ArrayList<>();
        String[] titles = {};
        if (stringGoalTitle != null) {
            titles = new String[]{stringGoalTitle};
        } else {
            titles = new String[]{"DummyText1"};
        }
        for (int i = 0; i <= titles.length; i++) {
            RVData current = new RVData();
            current.goalTitle = titles[i];
            data.add(current);
        }
        return data;
    }
}

public void newGoal(View view) {
    Intent intent = new Intent(MainActivity.this, NewGoal.class);
    startActivity(intent);
}

@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_main, 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);
}
}

RVData.java

import android.widget.CheckBox;

public class RVData {
CheckBox checkBox;
String goalTitle;

}

RVAdapter.java

public class RVAdapter extends RecyclerView.Adapter<RVAdapter.myViewHolder> {

List<RVData> data = Collections.emptyList();
private LayoutInflater inflater;

public RVAdapter(Context context, List<RVData> data) {
    inflater = LayoutInflater.from(context);
    this.data = data;
}

@Override
public myViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View view = inflater.inflate(R.layout.goal_row, viewGroup, false);
    myViewHolder holder = new myViewHolder(view);
    return holder;
}

@Override
public void onBindViewHolder(myViewHolder viewHolder, int position) {
    RVData current = data.get(position);
    viewHolder.title.setText(current.goalTitle);

}

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

class myViewHolder extends RecyclerView.ViewHolder {
    //@Bind(R.id.goalRowTitle)
    TextView title;
    //@Bind(R.id.goalRowCB)
    CheckBox checkBox;

    public myViewHolder(View itemView) {
        super(itemView);
        title = (TextView) itemView.findViewById(R.id.goalRowTitle);
        checkBox = (CheckBox) itemView.findViewById(R.id.goalRowCB);
    }
}
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
style="@style/DefaultLayoutStyle"
tools:context=".MainActivity">

<include
    android:id="@+id/app_bar"
    layout="@layout/app_bar" />


<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    style="@style/FABStyle"
    android:onClick="newGoal"
    android:src="@drawable/ic_add"
    app:borderWidth="0dp" />

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/app_bar">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/goalList"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"></android.support.v7.widget.RecyclerView>
    </RelativeLayout>

    <fragment
        android:id="@+id/fragment_navigation_drawer"
        android:name="com.kellarapps.zeal.NavigationDrawerFragment"
        android:layout_width="@dimen/nav_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:layout="@layout/fragment_navigation_drawer"
        tools:layout="@layout/fragment_navigation_drawer" />

</android.support.v4.widget.DrawerLayout>

activity_new_goal.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
style="@style/DefaultLayoutStyle"
tools:context="com.kellarapps.zeal.NewGoal">

<include
    android:id="@+id/app_bar"
    layout="@layout/app_bar" />

<LinearLayout
    android:id="@+id/goalTitle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/app_bar"
    android:layout_marginTop="10dp"
    android:orientation="horizontal"
    android:paddingEnd="5dp"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"

    android:paddingStart="5dp">

    <TextView
        style="@style/DefaultTVStyle"
        android:text="@string/goal" />

    <EditText
        android:id="@+id/edit_goalTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/goalHint" />
</LinearLayout>

<LinearLayout
    android:id="@+id/goalDescription"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/goalTitle"
    android:layout_marginTop="10dp"
    android:orientation="horizontal"
    android:paddingEnd="5dp"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:paddingStart="5dp">

    <TextView
        style="@style/DefaultTVStyle"
        android:text="@string/goalDescription" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/description"
        android:maxLines="3" />

</LinearLayout>

<android.support.design.widget.FloatingActionButton
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/fab2"
    style="@style/FABStyle"
    android:onClick="mSetRecyclerGoalTitle"
    android:src="@drawable/ic_action_done"
    app:borderWidth="0dp"/>

NewGoal.java

public class NewGoal extends AppCompatActivity {

private Toolbar toolbar;

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

    toolbar = (Toolbar) findViewById(R.id.app_bar);
    setSupportActionBar(toolbar);

    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

}

@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_new_goal, 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;
    }

    if(id == android.R.id.home)
    {
        NavUtils.navigateUpFromSameTask(this);
    }
    return super.onOptionsItemSelected(item);
}
}

goal_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingLeft="10dp"
android:paddingStart="10dp">

<CheckBox
    android:id="@+id/goalRowCB"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/goalRowTitle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:gravity="center_horizontal"
    android:text="@string/dummyTxt"
    android:textColor="@color/TextColor"
    android:textSize="17sp" />

从我所看到的,您想要做的是将NewGoal活动中的一些信息返回给MainActivity 您无法访问其他活动的布局以恢复其视图的内容。

通常使用startActivityForResult完成。

看一下开发人员参考: http : //developer.android.com/training/basics/intents/result.html

更改

public RVData mSetRecyclerGoalTitle(View view) {

    editGoalTitle = (EditText) findViewById(R.id.edit_goalTitle);
    editGoalTitle.setText(R.string.dummyTxt);
    stringGoalTitle = editGoalTitle.getText().toString();


    public List<RVData> getData () {
        List<RVData> data = new ArrayList<>();
        String[] titles = {};
        if (stringGoalTitle != null) {
            titles = new String[]{stringGoalTitle};
        } else {
            titles = new String[]{"DummyText1"};
        }
        for (int i = 0; i <= titles.length; i++) {
            RVData current = new RVData();
            current.goalTitle = titles[i];
            data.add(current);
        }
        return data;
    }
}

public void mSetRecyclerGoalTitle(View view) {

    editGoalTitle = (EditText) findViewById(R.id.edit_goalTitle);
    editGoalTitle.setText(R.string.dummyTxt);
    stringGoalTitle = editGoalTitle.getText().toString();

}

public List<RVData> getData () {
    mSetRecyclerGoalTitle(null);
    List<RVData> data = new ArrayList<>();
    String[] titles = {};
    if (stringGoalTitle != null) {
        titles = new String[]{stringGoalTitle};
    } else {
        titles = new String[]{"DummyText1"};
    }

    for (int i = 0; i <= titles.length; i++) {
        RVData current = new RVData();
        current.goalTitle = titles[i];
        data.add(current);
    }
    return data;
}

您的代码有很多问题。

首先,此方法没有任何意义:

public RVData mSetRecyclerGoalTitle(View view) {

editGoalTitle = (EditText) findViewById(R.id.edit_goalTitle);
editGoalTitle.setText(R.string.dummyTxt);
stringGoalTitle = editGoalTitle.getText().toString();


public List<RVData> getData () {
    List<RVData> data = new ArrayList<>();
    String[] titles = {};
    if (stringGoalTitle != null) {
        titles = new String[]{stringGoalTitle};
    } else {
        titles = new String[]{"DummyText1"};
    }
    for (int i = 0; i <= titles.length; i++) {
        RVData current = new RVData();
        current.goalTitle = titles[i];
        data.add(current);
    }
        return data;
    }
}

第一行是错误的。 edit_goalTitle不是来自activity_main因此您不会找到该视图。

接下来,您在方法内部编写了方法定义? 不,那不会发生。 这就是IDE会提示您错误的原因。

然后,您在xml中声明了onClick

android:onClick="mSetRecyclerGoalTitle"

但是,由于MainActivity膨胀了其他xml文件,而不是activity_new_goal.xml ,因此onClick也将失败。 这只会工作,如果你把mSetRecyclerGoalTitle方法膨胀活动中activity_new_goal.xml

在方法的定义中, getData和方括号()之间有一个空格:

public List<RVData> getData () {
    ...
}

改成:

public List<RVData> getData() {  // <-- deleted space
    ...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM