带有自定义适配器的listview总是在获取listview项名称时崩溃
问题出在标题上。我有一个带有自定义适配器的listview。当我试图获取listview中显示的listview项的名称时,系统崩溃。我怎样才能解决它带有自定义适配器的listview总是在获取listview项名称时崩溃,listview,android-studio,android-adapter,Listview,Android Studio,Android Adapter,问题出在标题上。我有一个带有自定义适配器的listview。当我试图获取listview中显示的listview项的名称时,系统崩溃。我怎样才能解决它 public class MenuPrincipalCiudades extends ListActivity { private AlphabetListAdapter adapter = new AlphabetListAdapter(); private GestureDetector mGestureDetector;
public class MenuPrincipalCiudades extends ListActivity {
private AlphabetListAdapter adapter = new AlphabetListAdapter();
private GestureDetector mGestureDetector;
List<Row> rows;
private List<Object[]> alphabet = new ArrayList<>();
private HashMap<String, Integer> sections = new HashMap<String, Integer>();
private int sideIndexHeight;
private static float sideIndexX;
private static float sideIndexY;
private int indexListSize;
private ProgressDialog pDialog;
JSONArray ciudades = null;
JSONObject c;
ListView lv;
boolean esCancelado = false;
ArrayList<String> listaCiudades;
class SideIndexGestureListener extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
sideIndexX = sideIndexX - distanceX;
sideIndexY = sideIndexY - distanceY;
if (sideIndexX >= 0 && sideIndexY >= 0) {
displayListItem();
}
return super.onScroll(e1, e2, distanceX, distanceY);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_alphabet);
lv = (ListView) findViewById(android.R.id.list);
listaCiudades = new ArrayList<>();
mGestureDetector = new GestureDetector(this, new SideIndexGestureListener());
listaCiudades.add("New York");
listaCiudades.add("Panama");
listaCiudades.add("Spain");
aplicarEstilo();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
AlphabetListAdapter.Section obj_itemDetails = (AlphabetListAdapter.Section) adapter.getItem(position);
Toast.makeText(MenuPrincipalCiudades.this, "You have chosen : " + " " + obj_itemDetails.text, Toast.LENGTH_LONG).show();
}
});
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (mGestureDetector.onTouchEvent(event)) {
return true;
} else {
return false;
}
}
public void aplicarEstilo(){
Collections.sort(listaCiudades);
rows = new ArrayList<>();
int start = 0;
int end = 0;
String previousLetter = null;
Object[] tmpIndexItem = null;
Pattern numberPattern = Pattern.compile("[0-9]");
for (String country : listaCiudades) {
String firstLetter = country.substring(0, 1);
// Group numbers together in the scroller
if (numberPattern.matcher(firstLetter).matches()) {
firstLetter = "#";
}
// If we've changed to a new letter, add the previous letter to the alphabet scroller
if (previousLetter != null && !firstLetter.equals(previousLetter)) {
end = rows.size() - 1;
tmpIndexItem = new Object[3];
tmpIndexItem[0] = previousLetter.toUpperCase(Locale.UK);
tmpIndexItem[1] = start;
tmpIndexItem[2] = end;
alphabet.add(tmpIndexItem);
start = end + 1;
}
// Check if we need to add a header row
if (!firstLetter.equals(previousLetter)) {
rows.add(new Section(firstLetter));
sections.put(firstLetter, start);
}
// Add the country to the list
rows.add(new Item(country));
previousLetter = firstLetter;
}
if (previousLetter != null) {
// Save the last letter
tmpIndexItem = new Object[3];
tmpIndexItem[0] = previousLetter.toUpperCase(Locale.UK);
tmpIndexItem[1] = start;
tmpIndexItem[2] = rows.size() - 1;
alphabet.add(tmpIndexItem);
}
adapter.setRows(rows);
setListAdapter(adapter);
updateList();
}
public void updateList() {
LinearLayout sideIndex = (LinearLayout) findViewById(R.id.sideIndex);
sideIndex.removeAllViews();
indexListSize = alphabet.size();
if (indexListSize < 1) {
return;
}
int indexMaxSize = (int) Math.floor(sideIndex.getHeight() / 20);
int tmpIndexListSize = indexListSize;
while (tmpIndexListSize > indexMaxSize) {
tmpIndexListSize = tmpIndexListSize / 2;
}
double delta;
if (tmpIndexListSize > 0) {
delta = indexListSize / tmpIndexListSize;
} else {
delta = 1;
}
TextView tmpTV;
for (double i = 1; i <= indexListSize; i = i + delta) {
Object[] tmpIndexItem = alphabet.get((int) i - 1);
String tmpLetter = tmpIndexItem[0].toString();
tmpTV = new TextView(this);
tmpTV.setText(tmpLetter);
tmpTV.setGravity(Gravity.CENTER);
tmpTV.setTextSize(15);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1);
tmpTV.setLayoutParams(params);
sideIndex.addView(tmpTV);
}
sideIndexHeight = sideIndex.getHeight();
sideIndex.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// now you know coordinates of touch
sideIndexX = event.getX();
sideIndexY = event.getY();
// and can display a proper item it country list
displayListItem();
return false;
}
});
}
public void displayListItem() {
LinearLayout sideIndex = (LinearLayout) findViewById(R.id.sideIndex);
sideIndexHeight = sideIndex.getHeight();
// compute number of pixels for every side index item
double pixelPerIndexItem = (double) sideIndexHeight / indexListSize;
// compute the item index for given event position belongs to
int itemPosition = (int) (sideIndexY / pixelPerIndexItem);
// get the item (we can do it since we know item index)
if (itemPosition < alphabet.size()) {
Object[] indexItem = alphabet.get(itemPosition);
int subitemPosition = sections.get(indexItem[0]);
getListView().setSelection(subitemPosition);
}
}
}
谢谢你帮助我 因为adapter.getItem(position)方法返回Item not节。因此,你必须改变路线
AlphabetListAdapter.Section obj_itemDetails = (AlphabetListAdapter.Section) adapter.getItem(position);
到
卓越的奥佐!谢谢你帮助我
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@android:id/list"
android:scrollbars="none"
android:layout_width="0dp"
android:overScrollMode="never"
android:layout_height="match_parent"
android:layout_weight="1"
android:dividerHeight="7.0sp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:divider="#eeeeee"
android:fastScrollEnabled="true" />
<LinearLayout
android:id="@+id/sideIndex"
android:layout_width="40dip"
android:layout_height="fill_parent"
android:background="#FFF"
android:gravity="center_horizontal"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
java.lang.ClassCastException: mypackage.AlphabetListAdapter$Item cannot be cast to mypackage.AlphabetListAdapter$Section
AlphabetListAdapter.Section obj_itemDetails = (AlphabetListAdapter.Section) adapter.getItem(position);
AlphabetListAdapter.Item obj_itemDetails = (AlphabetListAdapter.Item) adapter.getItem(position);