gpt4 book ai didi

java - 获取ListView中所选项目的标题文本

转载 作者:行者123 更新时间:2023-12-02 07:18:17 26 4
gpt4 key购买 nike

有没有办法在点击时获取所选项目的标题文本(来自该部分)?

使用此代码,我可以获取所选项目的文本,但我也想知道在哪个标题中单击了该项目。

@Override
public void onListItemClick(ListView lv, View v, int position, long id) {
System.out.println( "CLICKED LOCATION: " +
((TextView) v.findViewById(R.id.list_content1)).getText().toString() );
}

我根据下载并保存在包含一个键的 TreeMap 和另一个包含键和字符串的 TreeMap 中的数据动态创建标题和行 ( TreeMap<String, TreeMap<String, String>> )

然后我循环两个 map 以获取标题和行:

public void setLocationsCompanyHashMap(TreeMap<String, TreeMap<String, Object>> map)
{
companyMap = map;

List<Item> items = new ArrayList<Item>();

Iterator<?> it = companyMap.entrySet().iterator();
while (it.hasNext()) {


Map.Entry pairs = (Map.Entry)it.next();
items.add(new Header(null, (String) pairs.getKey()));

Iterator<?> ite = ((Map<String, Object>) pairs.getValue()).entrySet().iterator();
while (ite.hasNext()) {
Map.Entry in_pairs = (Map.Entry)ite.next();
items.add(new ListItem(null, (String) in_pairs.getKey()));
}

}

LocationsArrayAdapter adapter = new LocationsArrayAdapter(getActivity(), items);
setListAdapter(adapter);
}

这是我的 xml

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="@android:color/transparent"
android:choiceMode="singleChoice"/>

标题.java:

import android.view.LayoutInflater;

public class Header implements Item {

private final String name;

public Header(LayoutInflater inflater, String name) {
this.name = name;
}

@Override
public int getViewType() {
return RowType.HEADER_ITEM.ordinal();
}

@Override
public View getView(LayoutInflater inflater, View convertView) {
View view;
if (convertView == null) {
view = (View) inflater.inflate(R.layout.header, null);
// Do some initialization
} else {
view = convertView;
}

TextView text = (TextView) view.findViewById(R.id.separator);
text.setText(name);

return view;
}

}

location_header.xml:

<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center" android:scrollbars="none"
style="?android:attr/listSeparatorTextViewStyle" />

Listitem.java

import android.view.LayoutInflater;

public class ListItem implements Item {
private final String str1;
private final LayoutInflater inflater;

public ListItem(LayoutInflater inflater, String text1) {
this.str1 = text1;
this.inflater = inflater;
}

@Override
public int getViewType() {
return RowType.LIST_ITEM.ordinal();
}

@Override
public View getView(LayoutInflater inflater, View convertView) {
View view;
if (convertView == null) {
view = (View) inflater.inflate(R.layout.locations_list_item, null);
// Do some initialization
} else {
view = convertView;
}

TextView text1 = (TextView) view.findViewById(R.id.list_content1);
text1.setText(str1);

return view;
}

}

location_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="@drawable/listitem_background">

<TextView
android:id="@+id/list_content1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="5dip"
android:clickable="false"
android:gravity="center"
android:longClickable="false"
android:paddingBottom="1dip"
android:paddingTop="1dip"
android:textColor="#28465a"
android:textSize="17sp"
android:textStyle="bold"
/>


</LinearLayout>

界面项目:

导入 android.view.LayoutInflater;

public interface Item {
public int getViewType();
public View getView(LayoutInflater inflater, View convertView);
}

LocationsArrayAdapter.java:

import java.util.List;

public class LocationsArrayAdapter extends ArrayAdapter<Item> {

private LayoutInflater mInflater;

public enum RowType {
LIST_ITEM, HEADER_ITEM
}

private List<Item> items;

public LocationsArrayAdapter(Context context, List<Item> items) {
super(context, 0, items);
this.items = items;
mInflater = LayoutInflater.from(context);
}

@Override
public int getViewTypeCount() {
return RowType.values().length;

}

@Override
public int getItemViewType(int position) {
return items.get(position).getViewType();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
return items.get(position).getView(mInflater, convertView);
}

}

在我的 Activity 中,我只是添加一个新 fragment (LocationViewFragment.java)

getSupportFragmentManager()
.beginTransaction()
.replace(R.id.viewLayout, fragment)
.commit();

最佳答案

好的,我能想到的最简单的解决方案就是将 Item 对象标记到与其相关的 TextView 对象。代码如下:

text1.setText(str1);
text1.setTag(this);

以及onListItemClick:

Header header = (Header) v.getTag();

但我强烈建议您在适配器本身内部设置 getView 方法,而不是从 subview 中调用它。并将数组中的所有项目保存在其中,这样您就可以使用onListItemClick位置值直接访问您想要的Header

关于java - 获取ListView中所选项目的标题文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14628465/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com