findViewById无法在片段中工作这里Iam尝试将listview添加到特定片段但是我无法使用find viewBy Id映射listview控件 我是android开发的初学者,我不知道实现片段 任何人都可以帮助我吗?
public class FindPeopleFragment extends Fragment {
public FindPeopleFragment(){}
private static final String TAG = "CardListActivity";
private CardArrayAdapter cardArrayAdapter;
private ListView listView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_find_people, container, false);
return rootView;
listView = (ListView) findViewById(R.id.card_listView);
listView.addHeaderView(new View(this));
listView.addFooterView(new View(this));
cardArrayAdapter = new CardArrayAdapter(getApplicationContext(), R.layout.list_item_card);
for (int i = 0; i < 10; i++) {
Card card = new Card("Card " + (i+1) + " Line 1", "Card " + (i+1) + " Line 2");
cardArrayAdapter.add(card);
}
listView.setAdapter(cardArrayAdapter);
}
public class CardArrayAdapter extends ArrayAdapter<Card> {
private static final String TAG = "CardArrayAdapter";
private List<Card> cardList = new ArrayList<Card>();
public class CardViewHolder {
TextView line1;
TextView line2;
}
public CardArrayAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
@Override
public void add(Card object) {
cardList.add(object);
super.add(object);
}
@Override
public int getCount() {
return this.cardList.size();
}
@Override
public Card getItem(int index) {
return this.cardList.get(index);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
CardViewHolder viewHolder;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.list_item_card, parent, false);
viewHolder = new CardViewHolder();
viewHolder.line1 = (TextView) row.findViewById(R.id.line1);
viewHolder.line2 = (TextView) row.findViewById(R.id.line2);
row.setTag(viewHolder);
} else {
viewHolder = (CardViewHolder)row.getTag();
}
Card card = getItem(position);
viewHolder.line1.setText(card.getLine1());
viewHolder.line2.setText(card.getLine2());
return row;
}
public Bitmap decodeToBitmap(byte[] decodedByte) {
return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
}
}
}
答案 0 :(得分:4)
做到这一点
listView = (ListView) rootView.findViewById(R.id.card_listView);
并移动
return rootView;
最后onCreateView(...)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_find_people, container, false);
listView = (ListView)rootView.findViewById(R.id.card_listView);
listView.addHeaderView(new View(getActivity()));
listView.addFooterView(new View(getActivity()));
............
return rootView;
}
你也应该改变
listView.addHeaderView(new View(this));
listView.addFooterView(new View(this));
到
listView.addHeaderView(new View(getActivity()));
listView.addFooterView(new View(getActivity()));
答案 1 :(得分:4)
您的oncreateview应如下所示:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_find_people, container, false);
listView = (ListView)rootView.findViewById(R.id.card_listView);
listView.addHeaderView(new View(this));
listView.addFooterView(new View(this));
cardArrayAdapter = new CardArrayAdapter(getApplicationContext(), R.layout.list_item_card);
for (int i = 0; i < 10; i++) {
Card card = new Card("Card " + (i+1) + " Line 1", "Card " + (i+1) + " Line 2");
cardArrayAdapter.add(card);
}
listView.setAdapter(cardArrayAdapter);
return rootView;
}
答案 2 :(得分:-1)
试试这个
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_find_people, container, false);
listView = (ListView) rootView.findViewById(R.id.card_listView);
listView.addHeaderView(new View(this));
listView.addFooterView(new View(this));
cardArrayAdapter = new CardArrayAdapter(getApplicationContext(), R.layout.list_item_card);
for (int i = 0; i < 10; i++) {
Card card = new Card("Card " + (i+1) + " Line 1", "Card " + (i+1) + " Line 2");
cardArrayAdapter.add(card);
}
listView.setAdapter(cardArrayAdapter);
return rootView;
}
答案 3 :(得分:-1)
片段没有findViewById()。您必须使用View对象访问Fragment中的方法(在您的情况下为“rootView”)。如果你使用Activity而不是直接使用findViewById(),因为Activity有这个方法。
如下所示更新代码中的一行,它将使其正常工作。
listView = (ListView)rootView.findViewById(R.id.card_listView);
在onCreateView()的末尾添加以下行。
return rootView;