我正在关注NewBoston教程(https://www.youtube.com/watch?v=nOdSARCVYic&list=PL6gx4Cwl9DGBsvRxJJOzG4r4k_zLKrnxl&index=48)
他展示了如何将图像放入列表中,但他从未展示过如何为每段文本分配不同的图像。
这是我的MainActivity
。
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] Jobsites = {"River Park Place", "Mayfair", "Jameson House"};
ListAdapter jobsiteAdapter = new CustomAdapter(this, Jobsites);
ListView jobsiteListView = (ListView) findViewById(R.id.jobsiteListView);
jobsiteListView.setAdapter(jobsiteAdapter);
jobsiteListView.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String jobsite = String.valueOf(parent.getItemAtPosition(position));
//Toast.makeText(MainActivity.this, jobsite, Toast.LENGTH_LONG).show();
if (jobsite == "River Park Place"){
//Perform segue to the proper view where employess can sign in
//******************************************
System.out.println("*****************");
System.out.println("Attempting to segue");
System.out.println("*****************");
//******************************************
}else{
System.out.println("*****************");
System.out.println("These jobsites aren't avaliable yet!");
System.out.println("*****************");
Toast.makeText(MainActivity.this, "**These Sites aren't avaliable yet!**", Toast.LENGTH_LONG).show();
}
}
}
);
}
}
在视频中我们制作了一个处理图像的自定义视图。这是代码。
class CustomAdapter extends ArrayAdapter<String> {
public CustomAdapter(Context context, String[] jobsites) {
super(context,R.layout.custom_row ,jobsites);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater jobsiteInflater = LayoutInflater.from(getContext());
View customView = jobsiteInflater.inflate(R.layout.custom_row, parent, false);
String singleJobsiteItem = getItem(position);
ImageView josbiteImage = (ImageView) customView.findViewById(R.id.josbiteImage);
josbiteImage.setImageResource(R.drawable.riverparkplace);
return customView;
}
}
我还要为列表中最下面的两个文本添加另外两个图像。现在它只是为列表中的所有三行反复加载SAME图片。
答案 0 :(得分:0)
You need to实现了baseadapter
答案 1 :(得分:0)
让我一步一步指导您。在我们继续之前,您需要了解>map (("hello" !) . listIx) [0..5]
[Just 'h',Just 'e',Just 'l',Just 'l',Just 'o',Nothing]
的{{1}}使用您指定的数据填充每一行。换句话说,您希望将图像传递给适配器,就像使用 case 'F':
case 'f':
if(start < end || (start < 0 && end < 0 && end > start)) // start < end or start = -3 and end = -7
{
for (int i=start; i<=end; i+=inc)
{
System.out.print(i + " ");
}
}
else if(start > end || (start < 0 && end < 0 && start > end)) // if start = - 7 and end = -3
{
for (int i=end; i>=start; i-=inc) // should be greater than
{
System.out.print(i + " ");
}
}
else if(start == end)
{
System.out.println(end);
}
String数组一样。
定义一个ArrayAdapter
,其中包含您想要分配给它的字符串(ListView
)和图片。
Jobsites
初始化适配器使用的simple wrapper object
数组。在主要活动的Jobsites
中,执行以下操作:
public class SimpleObject {
private String jobSite;
private int imageID; // your R.drawable.image
public SimpleObject(String jobSite, int imageID) {
this.jobSite = jobSite;
this.imageID = imageID;
}
public String getJobSite() {
return jobSite;
}
public int getImageID() {
return imageID;
}
}
现在,将SimpleObject
改为onCreate()
而不是ArrayList<SimpleObject> objectList = new ArrayList<>();
objectList.add(new SimpleObject("River Park Place", R.drawable.image1);
objectList.add(new SimpleObject("Mayfair", R.drawable.image2);
// the list goes on....
:
CustomAdapter
现在确保使用新的SimpleObject
列表初始化主活动中的适配器:
String