有沒有方法在點擊時獲取所選項目的標題文本(來自該部分)?在ListView中獲取所選項目的標題文本
使用此代碼我可以得到選定的項目文本,但我也想知道該項目被點擊的標題。
@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());
}
創建我的頭和行動態取決於下載並保存在包含密鑰和包含密鑰和一個String(TreeMap<String, TreeMap<String, String>>)
。
然後我循環兩種地圖另一樹形一個樹形圖中數據獲得標題和行:
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"/>
header.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>
接口項目:
import 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);
}
}
在我的活動我剛加入新片段(該LocationViewFragment.java)
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.viewLayout, fragment)
.commit();
您能列出您的listview佈局嗎? –
我不完全明白你的意思。你想要更多的代碼嗎? 基本上我動態地生成頭和行。生病更新我的問題的更多代碼 –
請發佈您的活動代碼和LocationsArrayAdapter類,因爲上述代碼仍然缺少 –