我有一个片段活动,我用来制作标签栏。在这个活动中,我计划将一个表或ListViews与一些随机值放在一起。
这是我的代码:
import com.example.mvaguimaraes.beetrackprov.R;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
public class Orders extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.orders, container, false);
ListView listView1 = (ListView) findViewById(R.id.listView1);
Order[] items = {
new Order(1, "Milk", 21.50),
new Order(2, "Butter", 15.99),
new Order(3, "Yogurt", 14.90),
new Order(4, "Toothpaste", 7.99),
new Order(5, "Ice Cream", 10.00),
};
ArrayAdapter<Order> adapter = new ArrayAdapter<Order>(this,
android.R.layout.simple_list_item_1, items);
listView1.setAdapter(adapter);
listView1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String item = ((TextView) view).getText().toString();
Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
}
});
}
}
问题是我无法将我的Orders类扩展为Fragment和Activity,所以我在代码中出现了这个错误:
无法解析方法findViewByID(int)
ListView listView1 = (ListView) findViewById(R.id.listView1);
无法解析构造函数
ArrayAdapter<Order> adapter = new ArrayAdapter<Order>(this,
android.R.layout.simple_list_item_1, items);
无法解析方法getBaseContext()
Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
可能是因为他们从Activity继承了一些东西。有没有办法让它们在Fragment中工作?对不起我的新手,我是Android开发的新手。
要在点击列表视图中的一个项目后打开另一个屏幕,我在onItemClick public void方法之后立即执行此操作:
String item = ((TextView) view).getText().toString();
Intent i = new Intent(getApplicationContext(), OrderDetails.class);
i.putExtra("new_variable_name",item);
Intent browserIntent =
new Intent(Orders.this, OrderDetails.class);
startActivity(browserIntent);
startActivity(i);
我试图替换&#34; getApplicationContext()&#34;和&#34; Orders.this&#34;使用&#34; getActivity()&#34;但它没有用。
答案 0 :(得分:2)
片段没有像活动这样的findViewById()
方法。相反,您可以致电findViewById()
来查看View
中通过对onCreateView()
中的布局进行充气而返回的视图。此外,在您需要Context
的情况下,您通常会通过调用Activity
来使用附加片段的getActivity()
。
public class Orders extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.orders, container, false);
ListView listView1 = (ListView) v.findViewById(R.id.listView1);
Order[] items = {
new Order(1, "Milk", 21.50),
new Order(2, "Butter", 15.99),
new Order(3, "Yogurt", 14.90),
new Order(4, "Toothpaste", 7.99),
new Order(5, "Ice Cream", 10.00),
};
ArrayAdapter<Order> adapter = new ArrayAdapter<Order>(getActivity(),
android.R.layout.simple_list_item_1, items);
listView1.setAdapter(adapter);
listView1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String item = ((TextView) view).getText().toString();
Toast.makeText(getActivity(), item, Toast.LENGTH_LONG).show();
}
});
return v;
}
}