我是android的新手。我已经创建了一个抽屉菜单,并在其中制作了菜单,通过添加片段来实现。现在,对于每个菜单,我都想创建一个带有数组适配器类的自定义列表视图。我实现了它,但问题是列表的每一项中的标题和地址都是相同的。列表只显示最后一项。有人能帮我吗?!我想不出解决方案。这里有一些代码和一张照片:
import static com.example.user.appsightseeing.R.layout;
public class ParksFragment extends Fragment {
private ArrayList<Park> parks = new ArrayList<>();
public ParksFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(layout.fragment_parks, container, false);
parks.add(new Park("Artificial Lake of Tirana", "The Grand Park of Tirana, also known as the Tirana Park on the Artificial Lake or even the Park of Saint Procopius, is a 230 hectare public park situated on the southern part of Tirana.",
"At the end of Rruga Sami Frasheri.", R.drawable.artificiallake));
parks.add(new Park("Zoo park", "The only one of its kind in Albania, Tirana Zoo is concentrated in an area of \u200B\u200B7 hectares in the southern part of town, between the Grand Park and the Botanic Garden of Tirana . The zoo was established in 1966.",
"Near Rruga Liqeni i Thate", R.drawable.zoopark));
parks.add(new Park("Memorial park of the Cemetery of the Nation's Martyrs", "The National Martyrs Cemetery of Albania is the largest cemetery in Albania, located on a hill overlooking Tirana. The \"Mother Albania\" statue is located at the Cemetery.",
"Near street Rruga Ligor Lubonja", R.drawable.memorialpark));
parks.add(new Park("Kashar park", "The main core of Kashar’s Park, is the Reservoir of Purez- Kus. The reservoir and its surrounding territory are considered as one of the most picturesque and biologically unsoiled suburbs of Tirana.",
"Kashar", R.drawable.kasharpark));
parks.add(new Park("Vaqarr park", "The second park in Vaqarr, is a recreational area of 97 ha, that is more than useful to inhabitants in Tirana.",
"Vaqarr", R.drawable.vaqarripark));
parks.add(new Park("Farka Lake park", "To the East of the East of Tirana’s city center, Lake Farka is a local favorite for waterborne fun in Summer. Picnicking, jet and water skiing, swimming, boating, all the usual wet sports suspects.",
"At Lake of Farka, near Rruga Pjeter Budi", R.drawable.farkapark));
parks.add(new Park("Peza park", "Peza, a village approximately 20 minutes from the center of Tirana, is a popular place for locals to go for a coffee or lunch on the weekends to escape the city.",
"Peze", R.drawable.pezapark));
parks.add(new Park("Dajti Recreative park", "This park is one of the components of Dajti National Park, located 26 km east of Tirana and 50 km from \"Mother Teresa\" airport. This place is very frequented by tourists and is also known as the \"Natural Balcon of Tirana\" which offers recreation and accommodation facilities for tourists.",
"Dajti mountain", R.drawable.dajtirecreative));
parks.add(new Park("Dajti National park", "Dajti National Park is very important on local, national and regional level, for its biodiversity, landscape, recreational and cultural values. Among others it is considered as a live museum of the natural vertical structure of vegetation.",
"Dajti mountain", R.drawable.dajtinational));
parks.add(new Park("Botanic garden", "The Botanical Gardens of Tirana are scenic botanical gardens located in southern Tirana, Albania. It is the only botanical garden in Albania. Construction commenced in 1964, with the original site covering approximately 15 hectares.",
"Near Zoo park", R.drawable.botanicpark));
parks.add(new Park("Rinia park", "The park, 500 metres (1,600 ft) from the central square, was built in 1950[5] as part of a major urban development program which developed after World War II. It was initially a pleasant family park where inhabitants of Tirana could take their children.",
"Near Bulevardi Deshmoret e Kombit and near Rruga Myslym Shyri", R.drawable.riniapark));
ArrayAdapter<Park> adapter = new parkArrayAdapter(getActivity(), 0, parks);
ListView listView = (ListView) rootView.findViewById(R.id.customListView);
listView.setAdapter(adapter);
return rootView;
//add event listener so we can handle clicks
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
}
数组适配器类:
import java.util.List;
public class parkArrayAdapter extends ArrayAdapter<Park> {
private Context context;
private List<Park> parks;
private AnimatedStateListDrawable inflater;
//constructor, call on creation
public parkArrayAdapter(Context context, int resource, ArrayList<Park> objects) {
super(context, resource, objects);
this.context = context;
this.parks = objects;
}
//called when rendering the list
@NonNull
public View getView(int position, View convertView, ViewGroup parent) {
//get the park we are displaying
Park par = parks.get(position);
//get the inflater and inflate the XML layout for each item
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.park_layout, null);
TextView title = (TextView) view.findViewById(R.id.p_title);
TextView description = (TextView) view.findViewById(R.id.p_description);
TextView streetname = (TextView) view.findViewById(R.id.address);
ImageView image = (ImageView) view.findViewById(R.id.image);
//set title and description
String titleT = par.getPark_title();
title.setText(titleT);
//display trimmed excerpt for description
int descriptionLength = par.getPark_description().length();
if(descriptionLength >= 100){
String descriptionTrim = par.getPark_description().substring(0, 100) + "...";
description.setText(descriptionTrim);
}else{
description.setText(par.getPark_description());
}
streetname.setText(par.getPark_streetname());
//get the image associated with this park
int imageID = context.getResources().getIdentifier(String.valueOf(par.getPark_image()), "drawable", context.getPackageName());
image.setImageResource(imageID);
return view;
}
}
公园班:
public class Park {
private static String title;
private String description;
private static String streetname;
private int image;
public Park(String title, String description, String streetname, int image){
this.title = title;
this.description = description;
this.streetname = streetname;
this.image = image;
}
public static String getPark_title() { return title; }
public String getPark_description() {
return description;
}
public static String getPark_streetname() {
return streetname;
}
public int getPark_image() {
return image;
}
}
一排的公园布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="10dp"
android:paddingBottom="10dp">
<ImageView
android:id="@+id/image"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginRight="10dp"
android:contentDescription="Park Image" />
<LinearLayout
android:id="@+id/infoSection"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/image"
android:orientation="vertical">
<TextView
android:id="@+id/p_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="Park Title"
android:textColor="@android:color/black"
android:textSize="18sp" />
<TextView
android:id="@+id/p_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginRight="5dp"
android:text="Park Description"
android:textColor="@android:color/black"
android:textSize="15sp" />
</LinearLayout>
<RelativeLayout
android:id="@+id/addressSection"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/infoSection"
android:orientation="vertical"
android:layout_alignParentBottom="true">
<TextView
android:id="@+id/address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:text="Address:"
android:textColor="@android:color/black" />
</RelativeLayout>
</RelativeLayout>
列表视图的屏幕截图,所有项目的标题和地址相同
删除class Park中标题和街道名称的静态字段。
此外,类应该大写,所以ParkArrayAdapter,而不是parkArrayAdapter
因为您使用的是静态变量,所以在Park类中从title和streetname中删除static
private String title;
private String streetname;
Java中的static关键字意味着变量或函数在该类的所有实例之间共享,因为它属于该类型,而不是实际对象本身。
这与Android无关。如果你检查你的类,你会发现你描述了title和streetname字段及其getter方法。您正面临这个问题,因为当您从适配器获取值时,所有标题和streetname字段的值都是相同的。您为描述任何平台上任何应用程序中静态成员和实例成员之间的差异提供了一个很好的示例。实例成员位于不同的内存地址上,但静态成员位于相同的地址上,当您为静态成员赋值时,所有静态成员的值都在更改,因为它们位于内存中的同一位置。当你删除“静态”单词时,你的listview会运行良好。
在getView中,我使用了如下内容: 正如您在getView(…)方法中所看到的,我使用了三种不同类型的绘图(即:-在线、本地、默认) 如果在线节目数据已上载到在线服务器,则本地节目已添加到本地数据库,默认为….(既不上载到服务器,也不存储到本地数据库) 问题: 每当我进行批量上传时,只能在线绘制列表中的最后一行项目,而我已经将整个列表项目数据上传到服务器 我只是想显示所有的列表项目,我上传到服
我试图理解在我试图识别列表中哪些元素是可见的过程中发生了什么。 应用程序使用变量菜单下拉列表,其中存在标准选项列表,但在应用程序的每一行上可见的选项因某些参数而异。 因此,对于上面的示例,HTML 看起来像... 如您所见,显示的两个项目具有一种显示样式,其他项目为“无”。 我的想法是抓取所有列表项,然后循环浏览它们以确定显示哪些选项。 但是,使用此方法时,仅显示两个选项中的第一个选项。 但是,如
嘿,我想计算项目价格的总和,但它只显示最后一个项目的价格:这里是我的代码与react-monge-form: 这是我在reducer.js中的减少函数: 我在安装react currency时出错,我--强迫它。这是错误:npm i react货币格式 npm通知npm通知npm通知新的npm次要版本可用!7.3.0 - NPM ERR!此运行的完整日志可以在npm ERR中找到!C:\用户\程序
英文原文:http://emberjs.com/guides/templates/displaying-a-list-of-items/ 如果你需要枚举一个对象列表,可以使用Handlebar的{{#each}}助手: 1 2 3 4 5 <ul> {{#each people}} <li>Hello, {{name}}!</li> {{/each}} </ul> 对于数组中的
问题内容: 我有两个,一个包含一个由选择查询()填充的客户端列表,另一个包含选定的客户端的详细信息()。我想在显示细节的同时让客户保持选中状态。 XML: Java: 问题答案: 若要在按下时保持列表视图项目的颜色,请在列表视图项目布局中包括以下行: 然后在这样的文件夹中定义: 最后,将此包含在你的中: 这样,在任何时候都只能选择一项。你可以使用以下方式定义颜色值res/values/colors
问题内容: 在我的应用中有一段时间,无论用户输入了什么内容,我都必须强制显示建议列表中的所有项目。我怎样才能做到这一点? 我尝试使用过滤进行某些操作,但是对我而言,作为初学者进行过滤实在太复杂了,我尝试在没有任何运气的情况下搜索初学者教程进行过滤。也许,有一种更简单的方法可以强制显示所有建议项? 编辑:基本上我的想法是,当用户键入列表中未包含的内容时,它将显示他可以拥有的所有可用选项。 我已经找到