我是Android的新手,或者一般的编程,我正在开发一个Android列表应用程序。在应用程序中,我希望能够在listView中保存食谱,并将其名称作为您单击以打开配方的布局。我希望它有点像" Clock"在ios 9上的应用程序,您可以在其中保存无限的食谱。无论如何,这是我的问题,我可以保存并添加一个配方到listView(内部保存每个成分),但我不能保存多个。我尝试过:Dynamically add elements to a listView Android但它不能处理内部保存的字符串。
这是我的代码"创建新食谱" :
package pixleworksgames.mylist;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Button;
import android.widget.Toast;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class new_recipe extends Select_saved_recipes {
EditText name_of_recipe, in1, in2, in3, in4, in5, in6, in7, in8,
in9, in10, in11, in12, in13, in14, in15, in16, in17, steps;
Button createrecipe;
TextView first_recipe, ingr1, ingr2, ingr3, ingr4, ingr5, ingr6, ingr7, ingr8, ingr9,
ingr10, ingr11, ingr12, ingr13, ingr14, ingr15, ingr16, ingr17, stepsView, numbers, stepTitle, recipeTitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_recipe);
createrecipe = (Button) findViewById(R.id.button6);// Declaring editTexts.
name_of_recipe = (EditText) findViewById(R.id.editText);
in1 = (EditText) findViewById(R.id.editText2);
in2 = (EditText) findViewById(R.id.editText3);
in3 = (EditText) findViewById(R.id.editText4);
in4 = (EditText) findViewById(R.id.editText5);
in5 = (EditText) findViewById(R.id.editText6);
in6 = (EditText) findViewById(R.id.editText7);
in7 = (EditText) findViewById(R.id.editText8);
in8 = (EditText) findViewById(R.id.editText9);
in9 = (EditText) findViewById(R.id.editText10);
in10 = (EditText) findViewById(R.id.editText11);
in11 = (EditText) findViewById(R.id.editText12);
in12 = (EditText) findViewById(R.id.editText13);
in13 = (EditText) findViewById(R.id.editText14);
in14 = (EditText) findViewById(R.id.editText15);
in15 = (EditText) findViewById(R.id.editText16);
in16 = (EditText) findViewById(R.id.editText17);
in17 = (EditText) findViewById(R.id.editText18);
steps = (EditText) findViewById(R.id.editText19);
}
public void onCreateClick(View view) { //onClick of "Create recipe" button
createrecipe = (Button) findViewById(R.id.button6);
String filename = "First Recipe"; //Making filenames
String filename1 = "1ingredient";
String filename2 = "2ingredient";
String filename3 = "3ingredient";
String filename4 = "4ingredient";
String filename5 = "5ingredient";
String filename6 = "6ingredient";
String filename7 = "7ingredient";
String filename8 = "8ingredient";
String filename9 = "9ingredient";
String filename10 = "10ingredient";
String filename11 = "11ingredient";
String filename12 = "12ingredient";
String filename13 = "13ingredient";
String filename14 = "14ingredient";
String filename15 = "15ingredient";
String filename16 = "16ingredient";
String filename17 = "17ingredient";
String filename18 = "Steps";
if (createrecipe.isPressed()) {
{
String nrecipe = name_of_recipe.getText().toString(); //Setting strings.
String ing1 = in1.getText().toString();
String ing2 = in2.getText().toString();
String ing3 = in3.getText().toString();
String ing4 = in4.getText().toString();
String ing5 = in5.getText().toString();
String ing6 = in6.getText().toString();
String ing7 = in7.getText().toString();
String ing8 = in8.getText().toString();
String ing9 = in9.getText().toString();
String ing10 = in10.getText().toString();
String ing11 = in11.getText().toString();
String ing12 = in12.getText().toString();
String ing13 = in13.getText().toString();
String ing14 = in14.getText().toString();
String ing15 = in15.getText().toString();
String ing16 = in16.getText().toString();
String ing17 = in17.getText().toString();
String ssteps = steps.getText().toString();
try {
//Creating FileOutputStreams, making the files private
FileOutputStream fileOutputStream = openFileOutput(filename, MODE_PRIVATE);
FileOutputStream fileOutputStream1 = openFileOutput(filename1, MODE_PRIVATE);
FileOutputStream fileOutputStream2 = openFileOutput(filename2, MODE_PRIVATE);
FileOutputStream fileOutputStream3 = openFileOutput(filename3, MODE_PRIVATE);
FileOutputStream fileOutputStream4 = openFileOutput(filename4, MODE_PRIVATE);
FileOutputStream fileOutputStream5 = openFileOutput(filename5, MODE_PRIVATE);
FileOutputStream fileOutputStream6 = openFileOutput(filename6, MODE_PRIVATE);
FileOutputStream fileOutputStream7 = openFileOutput(filename7, MODE_PRIVATE);
FileOutputStream fileOutputStream8 = openFileOutput(filename8, MODE_PRIVATE);
FileOutputStream fileOutputStream9 = openFileOutput(filename9, MODE_PRIVATE);
FileOutputStream fileOutputStream10 = openFileOutput(filename10, MODE_PRIVATE);
FileOutputStream fileOutputStream11 = openFileOutput(filename11, MODE_PRIVATE);
FileOutputStream fileOutputStream12 = openFileOutput(filename12, MODE_PRIVATE);
FileOutputStream fileOutputStream13 = openFileOutput(filename13, MODE_PRIVATE);
FileOutputStream fileOutputStream14 = openFileOutput(filename14, MODE_PRIVATE);
FileOutputStream fileOutputStream15 = openFileOutput(filename15, MODE_PRIVATE);
FileOutputStream fileOutputStream16 = openFileOutput(filename16, MODE_PRIVATE);
FileOutputStream fileOutputStream17 = openFileOutput(filename17, MODE_PRIVATE);
FileOutputStream fileOutputStream18 = openFileOutput(filename18, MODE_PRIVATE);
//Getting bytes.
fileOutputStream.write(nrecipe.getBytes());
fileOutputStream1.write(ing1.getBytes());
fileOutputStream2.write(ing2.getBytes());
fileOutputStream3.write(ing3.getBytes());
fileOutputStream4.write(ing4.getBytes());
fileOutputStream5.write(ing5.getBytes());
fileOutputStream6.write(ing6.getBytes());
fileOutputStream7.write(ing7.getBytes());
fileOutputStream8.write(ing8.getBytes());
fileOutputStream9.write(ing9.getBytes());
fileOutputStream10.write(ing10.getBytes());
fileOutputStream11.write(ing11.getBytes());
fileOutputStream12.write(ing12.getBytes());
fileOutputStream13.write(ing13.getBytes());
fileOutputStream14.write(ing14.getBytes());
fileOutputStream15.write(ing15.getBytes());
fileOutputStream16.write(ing16.getBytes());
fileOutputStream17.write(ing17.getBytes());
fileOutputStream18.write(ssteps.getBytes());
//Closing FileOutPutStreams
fileOutputStream.close();
fileOutputStream1.close();
fileOutputStream2.close();
fileOutputStream3.close();
fileOutputStream4.close();
fileOutputStream5.close();
fileOutputStream6.close();
fileOutputStream7.close();
fileOutputStream8.close();
fileOutputStream9.close();
fileOutputStream10.close();
fileOutputStream11.close();
fileOutputStream12.close();
fileOutputStream13.close();
fileOutputStream14.close();
fileOutputStream15.close();
fileOutputStream16.close();
fileOutputStream17.close();
fileOutputStream18.close();
//"Recipe successfully saved!"
Toast.makeText(getApplicationContext(), "Recipe successfully saved!", Toast.LENGTH_LONG).show();
//Refreshing EditTexts
name_of_recipe.setText("");
in1.setText("");
in2.setText("");
in3.setText("");
in4.setText("");
in5.setText("");
in6.setText("");
in7.setText("");
in8.setText("");
in9.setText("");
in10.setText("");
in11.setText("");
in12.setText("");
in13.setText("");
in14.setText("");
in15.setText("");
in16.setText("");
in17.setText("");
steps.setText("");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//Adding the name of the recipe to the listView
arrayList.add(nrecipe);
adapter.notifyDataSetChanged();
}
//Opening the Select_saved_recipes menu
Intent intent = new Intent(new_recipe.this,
Select_saved_recipes.class);
new_recipe.this.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, 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);
}
以下是食谱菜单的代码:
package pixleworksgames.mylist;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.HeaderViewListAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class Select_saved_recipes extends AppCompatActivity {
Button addarecipe;
ArrayAdapter<String> adapter;
String[] recipes = new String[0];
ArrayList<String> arrayList = new ArrayList<String>();
private ListView listView;
public ArrayList<String> getArrayList() {
return arrayList;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.select_saved_recipe);
String nrecipe;
if (listView == null) {
listView = (ListView) findViewById(R.id.listView);
}
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, arrayList);
setListAdapter(adapter);
try {
//Finding the name of recipe
FileInputStream fileInputStream = openFileInput("First Recipe");
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuffer stringBuffer = new StringBuffer();
while ((nrecipe = bufferedReader.readLine()) != nrecipe) {
stringBuffer.append(nrecipe);
}
arrayList.add(nrecipe);
adapter.notifyDataSetChanged();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Opens the layout which displays the recipe
Intent intent = new Intent(Select_saved_recipes.this,
Saved_recipes.class);
Select_saved_recipes.this.startActivity(intent);
}
});
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void setListAdapter(ArrayAdapter<String> adapter) {
getListView().setAdapter(adapter);
}
private ListView getListView() {
if (listView == null) {
listView = (ListView) findViewById(R.id.listView);
}
return listView;
}
protected ListAdapter getListAdapter() {
ListAdapter adapter = getListView().getAdapter();
if (adapter instanceof HeaderViewListAdapter) {
return ((HeaderViewListAdapter)adapter).getWrappedAdapter();
} else {
return adapter;
}
}
//When someone click the "Plus" symbol to add a new recipe
public void onAddClick (View view) {
addarecipe = (Button) findViewById(R.id.button13);
if (addarecipe.isPressed()) {
//Opens new_recipe
Intent intentApp = new Intent(Select_saved_recipes.this,
new_recipe.class);
Select_saved_recipes.this.startActivity(intentApp);
}
}
@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, 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);
}
}
再次提醒您,我对编程非常陌生,所以我的代码可能非常混乱。我希望有人可以帮忙!
答案 0 :(得分:0)
您的目标:
ListView
上显示食谱标题。我的建议:
SQLiteDatabase
和Cursor
。Adapters
,所以很酷。