ListAdapter.javaAndroid的 - 在GridView的A卡項目重複兩次
public class ListAdapter extends ArrayAdapter<String>
{
private Activity context;
private int[] icon;
private String[] title;
public ListAdapter(Activity context, int[] icon, String[] title)
{
super(context, R.layout.list_item, title);
this.context = context;
this.icon = icon;
this.title = title;
}
public View getView(final int position, View view, final ViewGroup parent)
{
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.list_item, null, true);
try
{
TextView txtTitle = (TextView) rowView.findViewById(R.id.txtTitle);
ImageView imgIcon = (ImageView) rowView.findViewById(R.id.imgIcon);
txtTitle.setText(title[position]);
imgIcon.setBackgroundResource(icon[position]);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
return rowView;
}
}}
MainActivity.java
在此卡活動被替換服務。當我重新洗牌時,更改會反映到應用程序中,但是當我再次運行應用程序時,Gallery將被服務取代。
public class MainActivity extends AppCompatActivity
{
String[] title = {
"Services" ,
"Solutions",
"Customers",
"Events",
"Gallery",
"Contact Us"
};
int[] icon = {
R.drawable.services_icon,
R.drawable.solution_icon,
R.drawable.customer_icon,
R.drawable.event_icon,
R.drawable.gallery_icon,
R.drawable.contact_us_icon
};
HomeGrid adapter = new HomeGrid(MainActivity.this, title, icon, back);
grid = (GridView) findViewById(R.id.grid);
grid.setAdapter(adapter);
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
switch (title[position]) {
case "Services":
Intent i1 = new Intent(MainActivity.this, Listing.class);
startActivity(i1);
break;
case "Solutions":
//Intent i2 = new Intent(MainActivity.this, Listing.class);
//startActivity(i2);
Toast.makeText(MainActivity.this, "You Clicked at " + title[position], Toast.LENGTH_SHORT).show();
break;
case "Customers":
Toast.makeText(MainActivity.this, "You Clicked at " + title[position], Toast.LENGTH_SHORT).show();
break;
case "Gallery":
Toast.makeText(MainActivity.this, "You Clicked at " + title[position], Toast.LENGTH_SHORT).show();
break;
case "Events":
Intent i3 = new Intent(MainActivity.this, Form.class);
startActivity(i3);
break;
case "Contact Us":
Toast.makeText(MainActivity.this, "You Clicked at " + title[position], Toast.LENGTH_SHORT).show();
break;
}
}
});
}
什麼是「HomeGrid」? – njzk2
'inflater.inflate(...,true)' - >'inflater.inflate(...,false)'您不想將視圖附加到父對象上。網格負責這一點。 – njzk2