0
我試圖創建一個可以在其中顯示列表視圖的標籤..在我創建evrything後,紅線出現在3部分(如下面)顯示的問題..我嘗試了一切還沒有working..I希望有人可以給我暗示或一些有用的答案..在標籤片段中創建列表視圖
import android.app.Activity;
import android.app.Fragment;
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.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
/**
* Created by Alhaadi on 23/3/2017.
*/
public class Drink extends android.support.v4.app.Fragment {
ListView list;
String[] itemname ={
"Safari",
"Camera",
"Global",
"FireFox",
"UC Browser",
"Android Folder",
"VLC Player",
"Cold War",
"Nasi Basi",
};
String[] price = {
"rm 9.00",
"rm 7.00",
"rm 8.00",
"rm 5.00",
"rm 9.00",
"rm 15.00",
"rm 8.00",
"rm 8.00",
"rm 9.00",
};
Integer[] imgid={
R.drawable.food1,
R.drawable.food2,
R.drawable.food3,
R.drawable.food4,
R.drawable.food5,
R.drawable.food6,
R.drawable.food7,
R.drawable.food8,
R.drawable.food9,
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.drink, container, false);
return rootView;
CustomListAdapter adapter=new CustomListAdapter(this, itemname, imgid, price);//here the problem
list=(ListView)findViewById(R.id.mainlistdrink); //here the problem redline at findviewbyId
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
String Slecteditem= itemname[+position];
Toast.makeText(getApplicationContext(), Slecteditem, Toast.LENGTH_SHORT).show(); //here the problem redline at getaApplicationContext()
}
});
}
public class CustomListAdapter extends ArrayAdapter<String> {
private final Activity context;
private final String[] itemname;
private final Integer[] imgid;
private final String[] price;
public CustomListAdapter(Activity context, String[] itemname, Integer[] imgid, String[] price) {
super(context, R.layout.drinklist, itemname);
// TODO Auto-generated constructor stub
this.context=context;
this.itemname=itemname;
this.imgid=imgid;
this.price=price;
}
public View getView(int position,View view,ViewGroup parent) {
LayoutInflater inflater=context.getLayoutInflater();
View rowView=inflater.inflate(R.layout.drinklist, null,true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.drinkname);
ImageView imageView = (ImageView) rowView.findViewById(R.id.drinklistview);
TextView extratxt = (TextView) rowView.findViewById(R.id.drinkname);
txtTitle.setText(itemname[position]);
imageView.setImageResource(imgid[position]);
extratxt.setText(price[position]);
return rowView;
}
}
}
這是標籤類..
package com.example.alhaadi.foodvault;
import android.app.Activity;
import android.support.design.widget.TabLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class Tabmenu extends AppCompatActivity{
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link FragmentPagerAdapter} derivative, which will keep every
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
private SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {@link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabmenu);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_tabmenu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
Food food = new Food();
return food;
case 1:
Drink drink = new Drink();
return drink;
case 2:
Bread bread = new Bread();
return bread;
case 3:
Dessert dessert = new Dessert();
return dessert;
default:
return null;
}
}
@Override
public int getCount() {
// Show 3 total pages.
return 4;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "FOOD";
case 1:
return "DRINK";
case 2:
return "BREAD";
case 3:
return "DESSERT";
}
return null;
}
}
}
1)你的'適配器'需要是'Context','this' - 'Fragment' - 不是'Context'。使用'getActivity()'而不是'this',因爲'Activity'是'Context'。 2)片段沒有'findViewById()'方法。你需要[在你剛剛膨脹的'rootView'中找到你的'View'](http://stackoverflow.com/a/6496013) - 'rootView.findViewById(R.id.mainlistdrink)'。 3)'Fragment'沒有'getApplicationContext()'方法。再次,您需要一個'Context',所以使用'getActivity()'而不是'getApplicationContext()'。 4)最後,'return rootView;'行必須位於'onCreateView()'的末尾。 –
oohhh ...先生..非常感謝你..我花了兩天的時間來判斷髮生了什麼事情......你只需要保存我的一天..上帝保佑你,先生,他們現在正在工作.. –