我试图创建一个可以在其中显示列表视图的选项卡。在我创建了evrything后,红线显示在3部分(如下所示)显示问题..我尝试了一切仍然无法工作..我希望有人可以给我提示或一些有用的答案..
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;
}
}
}
这是Tab类..
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;
}
}
}
答案 0 :(得分:0)
我认为您应该尝试将Integer []更改为int [],也许我猜