我有一個活動,它包含一個ActionBar(有四個選項卡)分別分配給每個選項卡的片段。在這些片段中,我已經分配了一些填充字符串值的ListAdapter,可點擊的,以及我想操作的。當我點擊一個項目時,我希望該應用程序從該片段發送到另一個片段。我知道我必須使用FragmentManager()和FragmentTransaction(),但由於我是Android新手,我需要任何形式的幫助,非常感謝。如何在單擊listitem時進行片段事務?
這裏有一個選項卡(UserFragment.java)的代碼片段:
import android.app.ListFragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
/** This is a listfragment class */
public class UserFragment extends ListFragment
{
/** An array of items to display in ArrayList */
String user_items[] = new String[]
{
"Account",
"Addresses",
"Payment Providers",
"Profile",
"Transactions",
"Wallet"
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
/** Creating array adapter to set data in listview */
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getBaseContext(), android.R.layout.simple_expandable_list_item_1, user_items);
/** Setting the array adapter to the listview */
setListAdapter(adapter);
getListView().setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int pos,
long id)
{
Intent myIntent = new Intent(getActivity().getBaseContext(), Profile.class);
startActivity(myIntent);
}
});
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onStart()
{
super.onStart();
/** Setting the multiselect choice mode for the listview */
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
}
的Profile.java活動代碼:
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class Profile extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.profile_layout);
//Button test = (Button) findViewById(R.id.btnTest);
}
}
我想你建議和我的應用程序崩潰,當我切換到我發起OnItemClickListener的標籤。是這樣的錯誤: 「java.lang.IllegalStateException:尚未創建內容視圖」: getListView()setOnItemClickListener(新OnItemClickListener(){ \t \t \t公共無效onItemClick(適配器視圖> 最近插入代碼父母,觀景,INT POS, \t \t \t \t \t長ID) \t \t \t { \t \t \t \t我的意圖Intent = new Intent(getActivity()。getBaseContext(),Profile.class); \t \t \t \t startActivity(myIntent); \t \t \t \t \t \t \t} \t }); –
您需要粘貼您的完整代碼 – waqaslam
請將此格式添加到您的問題中 – waqaslam