我对Android有点新,所以请原谅任何缺点。
我在activity_main.xml
中有一个FloatingActionButton,无论何时点击它,我都希望它带我到一个带有两个EditText字段的新活动,还有另一个FAB。
我想要做的是,无论何时输入文本并按下activity_new_goal.xml
中的FAB,它都应该创建一个包含@+id/edit_goalTitle
文本中文本的RecyclerView。
它显示了return data;
中不兼容类型的错误,并显示无法解析getData()
MainActivity.java
这是我的代码:
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" />
答案 0 :(得分:1)
从我看到的,您尝试做的是将 NewGoal 活动中的一些信息返回到 MainActivity 。您无法访问其他活动的布局以恢复其视图的内容。
通常使用 startActivityForResult 完成。
看看开发人员参考: http://developer.android.com/training/basics/intents/result.html
答案 1 :(得分:0)
更改
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;
}
答案 2 :(得分:0)
您的代码存在许多问题。
首先,这种方法没有任何意义:
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"
但是,这个onClick事情也会失败,因为MainActivity
夸大了其他xml文件,而不是activity_new_goal.xml
。只有将mSetRecyclerGoalTitle
方法放在导致activity_new_goal.xml
膨胀的活动中时,此方法才有效。
答案 3 :(得分:-2)
在方法定义中,()
与方括号public List<RVData> getData () {
...
}
之间有空格:
public List<RVData> getData() { // <-- deleted space
...
}
更改为:
library(ggplot2)
library(dplyr)
library(gridExtra)
x <- data.frame(Var = rep(1:3, 10000)) %>%
mutate(Val = rnorm(10000)*Var,
Var = factor(Var)) %>%
arrange(Var, Val) %>%
group_by(Var) %>%
mutate(ecdf = ecdf(Val)(Val))
ggplot(x, aes(x=Val)) +
stat_ecdf(aes(color = Var), size = 1.25, alpha = .9) -> gg1
ggplot(x, aes(x=Val)) +
stat_ecdf(aes(color = Var), size = 1.35, alpha = .9) +
guides(color = guide_legend(override.aes= list(alpha = 1))) -> gg2
grid.arrange(gg1, gg2)