ListAdapter.java
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;
}
}
});
}
答案 0 :(得分:0)
使用此功能。
public class ListAdapter extends ArrayAdapter<String>{
private Activity context;
private int[] icon;
private String[] title;
private LayoutInflater inflater;
public ListAdapter(Activity context, int[] icon, String[] title)
{
super(context, R.layout.list_item, title);
this.context = context;
this.icon = icon;
this.title = title;
inflater = LayoutInflater.from(context);
}
public View getView(final int position, View view, final ViewGroup parent)
{
view= inflater.inflate(R.layout.list_item, parent, false);
try
{
TextView txtTitle = (TextView) view.findViewById(R.id.txtTitle);
ImageView imgIcon = (ImageView) view.findViewById(R.id.imgIcon);
txtTitle.setText(title[position]);
imgIcon.setBackgroundResource(icon[position]);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
return view;
}
}
}