1
まあ、問題はタイトルにあります。私は、カスタムアダプタのリストビューを持っています。リストビューに表示されるリストビュー項目の名前を取得しようとすると、システムがクラッシュします。どうすれば解決できますか?カスタムアダプターを使用したリストビューは、リストビューの項目名を取得するために意図的にクラッシュする
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);
}
}
}
アダプタクラスはこれです:
public class AlphabetListAdapter extends BaseAdapter {
public static abstract class Row {}
public static final class Section extends Row {
public final String text;
public Section(String text) {
this.text = text;
}
}
public static final class Item extends Row {
public final String text;
public Item(String text) {
this.text = text;
}
}
private List<Row> rows;
public void setRows(List<Row> rows) {
this.rows = rows;
}
@Override
public int getCount() {
return rows.size();
}
@Override
public Row getItem(int position) {
return rows.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
if (getItem(position) instanceof Section) {
return 1;
} else {
return 0;
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (getItemViewType(position) == 0) { // Item
if (view == null) {
LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = (LinearLayout) inflater.inflate(R.layout.row_item, parent, false);
}
Item item = (Item) getItem(position);
TextView textView = (TextView) view.findViewById(R.id.textView1);
textView.setText(item.text);
} else { // Section
if (view == null) {
LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = (LinearLayout) inflater.inflate(R.layout.row_section, parent, false);
}
Section section = (Section) getItem(position);
TextView textView = (TextView) view.findViewById(R.id.textView1);
textView.setText(section.text);
}
return view;
}
}
は最後に、これは私のxmlレイアウトです:
<?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>
Click here to see how the activity display
あなたが触れたときにすべてが、素晴らしい作品ならば "パナマ "の場合、リストビューはパナマを返すでしょう。しかし、listviewは代わりにエラーを返します。私を助けるため
java.lang.ClassCastException: mypackage.AlphabetListAdapter$Item cannot be cast to mypackage.AlphabetListAdapter$Section
ありがとう:
これはログです!
優秀のOZOにライン
を変更する必要があります!私を助けてくれてありがとう –