我正在Android中开发多片段布局,并且我使用了List Fragment。我不想使用默认的阵列适配器,当我使用自定义阵列适配器时,列表视图没有响应。它无法打开另一个活动或片段(如果是横向)。
这是我的代码,它也出现在Android官方网站上。
TitlesFragment:
import android.app.FragmentTransaction;
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.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;
public class TitlesFragment extends ListFragment {
boolean mDuelPane;
int mCurCheckPosition = 0;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
new SuperHeroInfo();
Fragment_Adapter connectArrayToListView=new Fragment_Adapter(getActivity(),SuperHeroInfo.NAMES);
setListAdapter(connectArrayToListView);
View detailsFrame = getActivity().findViewById(R.id.details);
mDuelPane = detailsFrame != null && detailsFrame.getVisibility() == View.VISIBLE;
// If the screen is rotated onSaveInstanceState() below will store the // hero most recently selected. Get the value attached to curChoice and // store it in mCurCheckPosition
if (savedInstanceState != null) {
// Restore last state for checked position.
mCurCheckPosition = savedInstanceState.getInt("curChoice", 0);
}
if (mDuelPane) {
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
showDetails(mCurCheckPosition);
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("curChoice", mCurCheckPosition);
}
// When a list item is clicked we want to change the hero info
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
showDetails(position);
}
void showDetails(int index) {
// The most recently selected hero in the ListView is sent
mCurCheckPosition = index;
if (mDuelPane) {
//try
getListView().setItemChecked(index, true);
DetailsFragment details = (DetailsFragment)
getFragmentManager().findFragmentById(R.id.details);
if (details == null || details.getShownIndex() != index) {
// Make the details fragment and give it the currently selected hero index
details = DetailsFragment.newInstance(index);
// Start Fragment transactions
FragmentTransaction ft = getFragmentManager().beginTransaction();
// Replace any other Fragment with our new Details Fragment with the right data
ft.replace(R.id.details, details);
// TRANSIT_FRAGMENT_FADE calls for the Fragment to fade away
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
}
} else {
Intent intent = new Intent();
intent.setClass(getActivity(), DetailsActivity.class);
// Pass along the currently selected index assigned to the keyword index
intent.putExtra("index", index);
// Call for the Activity to open
startActivity(intent);
}
}
}
Fragment_Adapter:
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class Fragment_Adapter extends ArrayAdapter<String> {
Context context;
ArrayList<String> SmallArray;
public Fragment_Adapter(Context context, ArrayList<String> objects) {
super(context, R.layout.list_view, objects);
this.context=context;
SmallArray=objects;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
convertView = inflater.inflate(R.layout.list_view, parent, false);
TextView text = (TextView) convertView.findViewById(R.id.small_text);
text.setText(SmallArray.get(position));
return convertView;
}
}
答案 0 :(得分:0)
我认为你应该改变getView方法的第一行。
试试这个:
LayoutInflater inflater = getAplicationContext.getLayoutInflater();