将“标题”项目保存到 ListView 并单击项目以将数据加载到新活动
Posted
技术标签:
【中文标题】将“标题”项目保存到 ListView 并单击项目以将数据加载到新活动【英文标题】:Save "Title" item to ListView and click on item to load data to new activity 【发布时间】:2013-08-10 03:27:00 【问题描述】:我正在创建一个应用程序,其中用户在活动中输入班级名称(例如:Geology 101),在其下方,有编辑文本,用户在其中输入作业(家庭作业)及其相应的成绩权重(15%)。当用户保存它时,我希望将类的名称存储到另一个活动的列表视图中。因此,当用户查看他看到的日程安排时,Geology 101 并且当他单击该项目时,它会显示各种作业类型,例如家庭作业、测试、测验等,然后您可以单击家庭作业,在这里您将存储输入的各种家庭作业通过另一项活动。
到目前为止,这就是我的各种编辑文本的活动。
new_class.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:orientation="horizontal">
<EditText
android:id="@+id/register_assign_edit"
android:layout_
android:layout_
android:gravity="center_horizontal"
android:hint="@string/example_assign"
android:singleLine="true"
android:inputType="textCapWords"
android:layout_weight="1">
</EditText>
<EditText
android:id="@+id/register_weight_edit"
android:layout_
android:layout_weight="1"
android:layout_
android:hint="@string/example_weight"
android:layout_marginLeft="3dp"
android:gravity="center_horizontal"
android:inputType="number"
android:singleLine="true">
</EditText>
</LinearLayout>
new_class_container.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parentView"
android:layout_
android:layout_
android:orientation="vertical"
android:background="@drawable/gradient_gray">
<LinearLayout
android:layout_
android:layout_
android:layout_marginTop="5dp"
android:layout_marginLeft="3dp"
android:orientation="horizontal">
<TextView
android:id="@+id/register_class_textview"
android:layout_
android:layout_
android:text="@string/write_class_name"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF"/>
<EditText
android:id="@+id/register_class_edit"
android:layout_
android:layout_
android:hint="@string/example_name"
android:inputType="textCapWords"
android:singleLine="true"/>
</LinearLayout>
<LinearLayout
android:layout_
android:layout_
android:orientation="horizontal"
android:layout_marginLeft="3dp">
<TextView
android:id="@+id/register_assign_textview"
android:layout_
android:layout_
android:gravity="center_horizontal"
android:text="@string/write_assignment_type"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#4169E1"/>
<TextView
android:id="@+id/register_weight_textview"
android:layout_
android:layout_
android:gravity="center_horizontal"
android:text="@string/write_assign_percent"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#4169E1"/>
</LinearLayout>
<LinearLayout
android:layout_
android:layout_
android:orientation="horizontal">
<EditText
android:id="@+id/register_assign_edit"
android:layout_
android:layout_
android:gravity="center_horizontal"
android:hint="@string/example_assign"
android:singleLine="true"
android:inputType="textCapWords"
android:layout_weight="1">
</EditText>
<EditText
android:id="@+id/register_weight_edit"
android:layout_
android:layout_weight="1"
android:layout_
android:hint="@string/example_weight"
android:layout_marginLeft="3dp"
android:gravity="center_horizontal"
android:inputType="number"
android:singleLine="true">
</EditText>
</LinearLayout>
<LinearLayout
android:layout_
android:layout_
android:animateLayoutChanges="true"
android:orientation="vertical">
<Button
android:id="@+id/btn_add"
android:layout_
android:layout_
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:gravity="center_vertical|left"
android:onClick="onAddNewClicked"
android:paddingLeft="5dp"
android:text="@string/more_assign"/>
</LinearLayout>
</LinearLayout>
new_class.java
public class NewClass extends Activity
private LinearLayout mContainerView;
private Button mAddButton;
private View mExclusiveEmptyView;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.new_class_container);
mContainerView = (LinearLayout) findViewById(R.id.parentView);
mAddButton = (Button) findViewById(R.id.btn_add);
@Override
protected void onSaveInstanceState(Bundle outState)
super.onSaveInstanceState(outState);
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
super.onRestoreInstanceState(savedInstanceState);
public void onAddNewClicked(View v)
inflateEditRow(null, null);
v.setVisibility(View.GONE);
//Inflates the new_class xml each time I press the Add New Button
private void inflateEditRow(String name, String name1)
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.new_class, null);
final EditText register_assign = (EditText) rowView.findViewById(R.id.register_assign_edit);
final EditText et1 = (EditText) rowView.findViewById(R.id.register_weight_edit);
if (name != null && !name.isEmpty() && name1 != null && !name1.isEmpty())
register_assign.setText(name);
et1.setText(name1);
else
mExclusiveEmptyView = rowView;
register_assign.addTextChangedListener(new TextWatcher()
@Override
public void afterTextChanged(Editable s)
if (s.toString().isEmpty())
mAddButton.setVisibility(View.GONE);
if (mExclusiveEmptyView != null
&& mExclusiveEmptyView != rowView)
mContainerView.removeView(mExclusiveEmptyView);
mExclusiveEmptyView = rowView;
else
if (mExclusiveEmptyView == rowView)
mExclusiveEmptyView = null;
mAddButton.setVisibility(View.VISIBLE);
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after)
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count)
);
mContainerView.addView(rowView, mContainerView.getChildCount()-1);
// Add save button to action bar
@Override
public boolean onCreateOptionsMenu(Menu menu)
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
@Override
public boolean onOptionsItemSelected(MenuItem item)
switch(item.getItemId())
case R.id.btn_save:
//make it save to the new activity
return true;
default:
return true;
因此,每次我按下“添加更多”按钮时,new_class 布局都会在下方膨胀。正如您在我的代码中看到的那样,保存按钮位于操作栏中,我想单击该按钮并将数据存储在上述方法中。我想知道如何将特定权重分配给特定任务。谢谢。
【问题讨论】:
【参考方案1】:那么,您是否需要这样做(将新视图膨胀到容器中)?或者你只是简单地使用 ListView,只需添加新项目,刷新视图,它就在那里。你可以看看这个例子:
MainActivity:
public class MainActivity extends Activity
private Button add;
private MyAdapter adapter;
private ArrayList<String> data;
private ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
add = (Button) findViewById(R.id.button1);
lv = (ListView) findViewById(R.id.listView1);
data = new ArrayList<String>();
adapter = new MyAdapter(data);
lv.setAdapter(adapter);
add.setOnClickListener(new OnClickListener()
@Override
public void onClick(View arg0)
data.add("Add new item and its properties, class name, assignment type, grade...");
// Here you can store into your data storage(database,etc) to display later
adapter.notifyDataSetChanged();
);
@Override
public boolean onCreateOptionsMenu(Menu menu)
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
还有适配器:
class MyAdapter extends BaseAdapter
private ArrayList<String> data;
public MyAdapter(ArrayList<String> data)
this.data = data;
@Override
public int getCount()
// TODO Auto-generated method stub
return data.size();
@Override
public Object getItem(int arg0)
// TODO Auto-generated method stub
return data.get(arg0);
@Override
public long getItemId(int arg0)
// TODO Auto-generated method stub
return 0;
@Override
public View getView(int arg0, View arg1, ViewGroup arg2)
View view = arg1;
if(view == null)
view = getLayoutInflater().inflate(R.layout.item, null);
return view;
还有 XML 容器:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
tools:context=".MainActivity" >
<ListView
android:id="@+id/listView1"
android:layout_
android:layout_
android:layout_below="@+id/button1"
>
</ListView>
<Button
android:id="@+id/button1"
android:layout_
android:layout_
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Add more" />
</RelativeLayout>
项目:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_
android:layout_
android:text="Name of class:"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editText1"
android:layout_
android:layout_
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/textView1"
android:ems="10" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView2"
android:layout_
android:layout_
android:layout_alignParentLeft="true"
android:layout_below="@+id/editText1"
android:text="Asignment Type"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView3"
android:layout_
android:layout_
android:layout_alignBottom="@+id/textView2"
android:layout_alignLeft="@+id/editText1"
android:text="Grade Weight(%)"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editText2"
android:layout_
android:layout_
android:layout_alignParentLeft="true"
android:layout_alignRight="@+id/textView2"
android:layout_below="@+id/textView2"
android:ems="10" />
<EditText
android:id="@+id/editText3"
android:layout_
android:layout_
android:layout_alignBaseline="@+id/editText2"
android:layout_alignBottom="@+id/editText2"
android:layout_alignRight="@+id/textView3"
android:layout_toRightOf="@+id/textView1"
android:ems="10" />
</RelativeLayout>
希望这会有所帮助。
【讨论】:
以上是关于将“标题”项目保存到 ListView 并单击项目以将数据加载到新活动的主要内容,如果未能解决你的问题,请参考以下文章
如何更新 ListView 的一行,将视图添加到单击的项目?