Android:如何在选项卡布局中显示值,同时从中传递数据

Android: How to display values in Tab Layout while passing data from it

我想在单击列表中的一项时将我的值显示到选项卡布局中。我的问题是如何将值传递到我的选项卡 activity(TabActivity1) 中,以便在显示 tablayout.xml 时出现在我的 Tablayout 的第一个选项卡中,因为每当我单击列表中的项目时它转到我的tablayout.xml,这是正确的,但它没有任何价值。任何帮助将不胜感激。

这是我的代码:

Main.java

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.TextView;


public class Main extends Activity {

public final static String TAG_RECIPE="com.example.getlistfromdb.RECIPE_NAME";
public final static String TAG_INGRIDIENTS="com.example.getlistfromdb.INGRIDIENTS";


private recipelistHelper dbrecipelistHelper = null;
private Cursor ourCursor = null;
private recipeAdapter adapter = null;


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

    ListView myListView = (ListView) findViewById(R.id.myListView);

    dbrecipelistHelper = new recipelistHelper(this);

    dbrecipelistHelper.createDatabase();

    dbrecipelistHelper.openDatabase();

    ourCursor=dbrecipelistHelper.getCursor();

    startManagingCursor(ourCursor);

    adapter = new recipeAdapter(ourCursor);

    myListView.setAdapter(adapter);


    myListView.setOnItemClickListener(onListClick);

    }
    catch (Exception e)
    {

        Log.e("ERROR", "ERROR IN CODE: " + e.toString());   

        e.printStackTrace();

    }


}


private AdapterView.OnItemClickListener onListClick = new AdapterView.OnItemClickListener() 

{
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)

    {

        Cursor selectedCursor=(Cursor)adapter.getItem(position);

        selectedCursor.moveToPosition(position);  

        String ingridients = dbrecipelistHelper.getCursor(selectedCursor);
        String name = selectedCursor.getString(
        selectedCursor.getColumnIndexOrThrow("Recipe"));


        Intent i=new Intent(Main.this,Tab.class);
        //Intent in=new Intent(Main.this,TabActivity1.class);
        i.putExtra(TAG_INGRIDIENTS,ingridients);
        i.putExtra(TAG_RECIPE,name);
        startActivity(i);

    }

};



class recipeAdapter extends CursorAdapter {

    recipeAdapter(Cursor c){
        super(Main.this, c);
    }

    @Override
    public  void bindView (View row, Context ctxt, Cursor c)
    {
        recipeHolder holder = (recipeHolder)row.getTag();
        holder.populateFrom(c, dbrecipelistHelper);
    }
    @Override
    public  View newView(Context ctxt, Cursor c, ViewGroup parent)
    {
        LayoutInflater inflater = getLayoutInflater();
        View row=inflater.inflate(R.layout.list, parent, false);
        recipeHolder holder = new recipeHolder(row);
        row.setTag(holder);
        return(row);
    }


}

static class recipeHolder {
    private TextView name=null;

    recipeHolder(View row){
        name=(TextView)row.findViewById(R.id.recipeText);
    }

    void populateFrom(Cursor c, recipelistHelper r){
        name.setText(r.getName(c));
    }
}
}

此主要 class 中的其他方法来自 dbhelper class

Tab.java

import android.os.Bundle;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.view.Menu;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class Tab extends TabActivity {

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

    TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);

    TabSpec tab1 = tabHost.newTabSpec("First Tab");
    TabSpec tab2 = tabHost.newTabSpec("Second Tab");
    TabSpec tab3 = tabHost.newTabSpec("Third tab");

    tab1.setIndicator("Recipe");
    tab1.setContent(new Intent(this,TabActivity1.class));

    tab2.setIndicator("Facts");
    tab2.setContent(new Intent(this,TabActivity2.class));

    tab3.setIndicator("background");
    tab3.setContent(new Intent(this,TabActivity3.class));

    /** Add the tabs  to the TabHost to display. */
    tabHost.addTab(tab1);
    tabHost.addTab(tab2);
    tabHost.addTab(tab3);


}

}

TabActivity1

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;



public class TabActivity1 extends  Activity {

String recipeName = null;
String ingridients = null;
String procedure = null;

private TextView txtIngridients = null;
private TextView txtName = null;




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

    recipeName=this.getIntent().getStringExtra(Main.TAG_RECIPE);
    ingridients=this.getIntent().getStringExtra(Main.TAG_INGRIDIENTS);

    txtName=(TextView)findViewById(R.id.nameText);
    txtIngridients=(TextView)findViewById(R.id.ingridientText);


    txtName.setText(recipeName);
    txtIngridients.setText(ingridients);


}
}

在意图中传递包数据,如:

Intent intent1 = new Intent(this,TabActivity1.class);
intent1.putExtra(Main.TAG_RECIPE, value);
intent1.putExtra(Main.TAG_INGRIDIENTS, value);
tab1.setContent(intent1);