我创建的两个片段名称是Frag_A和Frag_B。在Frag_A中,我显示listview中的所有数据。 并且还有更新按钮。在Frag_B中有编辑文本和保存按钮。 首先,当用户从列表视图中选择一个项目,然后单击upadate。当点击更新时,Frag_A被Frag_B替换并在编辑文本中显示所选列表视图中的选定项目,用户进行更改并保存。如何实现此演示? 提前谢谢。
我的DatabaseAdapter文件是DBAdapter.java文件
package com.dlcpl.insert_view;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBAdapter {
public static final String KEY_TEXT="text";
public static final String TAG="DBAdapter";
public static final String DATABASE_NAME="taskmanager.db";
public static final String DATABAS_TABLE="text_table";
public static final int DATABASE_VERSION=1;
public static final String DATABASE_CREATE="create table "+DATABAS_TABLE+"("+KEY_TEXT+" varchar(500) not null);";
public final Context context;
public DatabaseHelper DBHelper;
public SQLiteDatabase db;
public DBAdapter(Context ctx)
{
this.context=ctx;
DBHelper=new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
public DatabaseHelper(Context context)
{
super(context,DATABASE_NAME,null,DATABASE_VERSION );
}
@Override
public void onCreate(SQLiteDatabase db)
{
try
{
db.execSQL(DATABASE_CREATE);
}
catch(SQLException e)
{
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.w(TAG,"Upgrading database from version"+oldVersion+"to"+newVersion+",which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS text_table");
onCreate(db);
}
}
//---opens the database---
public DBAdapter open() throws SQLException
{
db=DBHelper.getWritableDatabase();
return this;
}
//---close the database---
public void close()
{
DBHelper.close();
}
//---insert a text into the database---
public long insertText(String text)
{
ContentValues initialValues=new ContentValues();
initialValues.put(KEY_TEXT, text);
return db.insert(DATABAS_TABLE, null, initialValues);
}
//---delete a particular contact---
public boolean deleteContact(String text)
{
return db.delete(DATABAS_TABLE, KEY_TEXT + "=" + text, null) > 0;
}
//---retrieves all the text
/*public Cursor getAlltext()
{
return db.query(DATABAS_TABLE, new String[] {KEY_TEXT}, null, null, null, null, null);
}*/
public List<String[]> getAlltext()
{
List<String[]> list = new ArrayList<String[]>();
Cursor cursor = db.query(DATABAS_TABLE, new String[] {KEY_TEXT}, null, null, null, null, null);
int x=0;
if (cursor.moveToFirst()) {
do {
String[] b1=new String[]{cursor.getString(0)};
list.add(b1);
x=x+1;
} while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
cursor.close();
return list;
}
//---retrieves a particular contact---
public Cursor gettext(String string) throws SQLException
{
Cursor mCursor= db.query(true, DATABAS_TABLE, new String[] {KEY_TEXT}, KEY_TEXT + "=" + string, null, null, null, null, null);
if(mCursor !=null)
{
mCursor.moveToFirst();
}
return mCursor;
}
//---updates a text_table---
public boolean updateContact(String text)
{
ContentValues args=new ContentValues();
args.put(KEY_TEXT, text);
return db.update(DATABAS_TABLE, args, KEY_TEXT + "=" + text, null) > 0;
}
}
我的Frag_A文件是View_Data.java
package com.dlcpl.insert_view;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
//import android.widget.Toast;
import android.app.ListFragment;
import android.graphics.Color;
public class View_Data extends ListFragment
{
public static final String ROW_ID = "row_id";
ListView listView;
TextView selection;
TextView label1;
public int idToModify;
List<String[]> list = new ArrayList<String[]>();
List<String[]> names2 =null ;
String[] stg1;
EditText A_input;
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.view_data, container, false);
}
@Override
public void onStart()
{
super.onStart();
DBAdapter dbAdapter =new DBAdapter(getActivity());
dbAdapter.open();
names2 = dbAdapter.getAlltext();
stg1=new String[names2.size()];
int x=0;
String stg;
for (String[] name : names2) {
stg = name[0];
stg1[x]=stg;
x++;
}
setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, stg1));
Button btnLoad = (Button) getActivity().findViewById(R.id.updata);
btnLoad.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
android.app.FragmentManager fragmentManager = getFragmentManager();
android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Update_data updatedata = new Update_data();
fragmentTransaction.replace(R.id.fragment,updatedata);
fragmentTransaction.commit();
}
});
dbAdapter.close();
}
public void onListItemClick(ListView parent, View v, int position, long id)
{
for(int a = 0; a < parent.getChildCount(); a++)
{
parent.getChildAt(a).setBackgroundColor(Color.WHITE);
}
TextView label1=(TextView)getActivity().findViewById(R.id.textdisplay);
v.setBackgroundColor(Color.LTGRAY);
label1.setText(stg1[position]);
}
}
和Frag_B文件是Update_data.java
package com.dlcpl.insert_view;
import android.app.Fragment;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
//import android.widget.ListView;
import android.widget.TextView;
//import android.support.v4.app.Fragment;
import android.widget.Toast;
public class Update_data extends Fragment
{
//String Text;
EditText A_input;
TextView label1;
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState)
{
return inflater.inflate(R.layout.update, container, false);
}
@Override
public void onStart()
{
super.onStart();
Button btnLoad = (Button) getActivity().findViewById(R.id.save_btn);
A_input = (EditText)getActivity().findViewById(R.id.udate_text);
btnLoad.setOnClickListener( new View.OnClickListener() {
DBAdapter Adapter =new DBAdapter(getActivity());
public void onClick(View v) {
Adapter.open();
Cursor c = Adapter.gettext("text");
if (c.moveToFirst())
{
DisplayContact(c);
}
else
{
Toast.makeText(getActivity(), "No contact found", Toast.LENGTH_LONG).show();
}
Adapter.close();
}
});
}
public void DisplayContact(Cursor c)
{
A_input.setText(c.getString(0));
}
}
我的主要活动文件是Home.java
package com.dlcpl.insert_view;
import android.app.Activity;
import android.os.Bundle;
//import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class Home extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Button insertData = (Button) findViewById(R.id.button1);
insertData.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Insert_Data insertdata = new Insert_Data();
android.app.FragmentManager fragmentManager = getFragmentManager();
android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment,insertdata);
fragmentTransaction.commit();
}
});
Button viewData = (Button) findViewById(R.id.button2);
viewData.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
View_Data viewdata = new View_Data();
android.app.FragmentManager fragmentManager = getFragmentManager();
android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment,viewdata);
fragmentTransaction.commit();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_home, menu);
return true;
}
}
答案 0 :(得分:6)
尝试使用Bundle,
我现在不记得代码了,但是当我的一个朋友使用片段时他使用了Bundle,我认为他使用的是像某个调用方法中的最后一个参数。
更新:
发送:
Fragment fragment = new Fragment();
Bundle bundle = new Bundle();
bundle.putInt(key, value);
fragment.setArguments(bundle);
提取:
Bundle bundle = this.getArguments();
int myInt = bundle.getInt(key, defaultValue);
我在此链接中找到了上述代码:How to transfer some data to another Fragment?
答案 1 :(得分:1)
由于两个片段共享相同的活动“Home”,您可以使用它来共享两个片段之间的信息。 View_Data可以调用:
((Home) getActivity()).shareData("example string");
这可能对应于Home中的方法shareData():
public void shareData(String string){
android.app.FragmentManager fragmentManager = getFragmentManager();
Update_data fragment = (Update_data) fragmentManager.findFragmentByTag( "your_tag" );
fragment.update(string);
}
您需要设置第二个片段的ID,以便片段管理器能够找到片段,并在第二个片段中创建公共方法“update”。
编辑:要设置片段的标记,请更改片段添加代码以包含标记“your_tag”。
View_Data viewdata = new View_Data();
android.app.FragmentManager fragmentManager = getFragmentManager();
android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment,viewdata,"your_tag");
fragmentTransaction.commit();
答案 2 :(得分:0)
setIntent()
和getIntet()
将您想要的所有数据放在您的意图上,getActivity()
在这两种情况下,您的数据都将存储在活动级别,这很好,因为您的片段存在于同一个活动中,因此这两个选项都可以很好地完成工作。
答案 3 :(得分:0)
您可以使用setArgument和getArgument方法传递数据
ft.replace(R.id.menuContent, Fragment.instantiate(getApplicationContext(), name, bundle));
您可以通过getArguments()方法在FragmentB类中获取此包
其中name是类的名称,例如fragmentB.class.getName()