1
我可以发誓此工作之前,但现在当我测试它我有一个问题。我有一个自定义列表视图,它有一系列不同高度的文字视图。在Android 4.x中,它完美运行。在我的旧手机2.3列表中出现,但它只有一行高,所以大部分的textview无法看到。它的行为应该像它应该的那样,它滚动并且我可以通过长时间按下删除项目。Android ListView无法正常工作姜饼
我试图只是改变了TextView
高度为固定值,这适用于4.x的,但使在2.3
没有区别这里是代码的相关位: notam_list.xml
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listOfNotams"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
mylistviewstyle.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listviewText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dp"
android:textSize="12sp" />
从活动片段:
public class NotamList extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notam_list);
final ListView listOfNotams = (ListView) findViewById(R.id.listOfNotams);
....
}
final StableArrayAdapter adapter = new StableArrayAdapter(this, R.layout.mylistviewstyle, NOTAMS);
listOfNotams.setAdapter(adapter);
listOfNotams.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public boolean onItemLongClick(AdapterView<?> parent, final View view, int position, long id) {
final String item = (String) parent.getItemAtPosition(position);
Pattern pattern;
Matcher matcher;
if (item.substring(4, 5).matches("-")) {
pattern = Pattern.compile("[A-Z][A-Z][A-Z][A-Z]-[A-Z][0-9]+/[12]\\d");
matcher = pattern.matcher(item);
matcher.find();
String notamSelected = matcher.group(0);
HiddenNotam hiddenNotam = new HiddenNotam(notamSelected, 1);
if (db.checkHiddenNotamExists(notamSelected)) {
db.deleteHiddenNotam(hiddenNotam);
Toast toast = Toast.makeText(getApplicationContext(), notamSelected + " Un-Hidden", Toast.LENGTH_SHORT);
toast.show();
} else {
db.addHiddenNotam(hiddenNotam);
if (hideNotams) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
// POSSIBLE SOLUTION TO THIS
// http://nineoldandroids.com/
NOTAMS.remove(item);
adapter.notifyDataSetChanged();
} else {
view.animate().setDuration(2000).alpha(0).withEndAction(new Runnable() {
@Override
public void run() {
NOTAMS.remove(item);
adapter.notifyDataSetChanged();
view.setAlpha(1);
}
});
}
}
Toast toast = Toast.makeText(getApplicationContext(), notamSelected + " Hidden", Toast.LENGTH_SHORT);
toast.show();
}
} else {
Toast toast = Toast.makeText(getApplicationContext(), "This can't be hidden", Toast.LENGTH_SHORT);
toast.show();
}
return true;
}
});
}
private class StableArrayAdapter extends ArrayAdapter<String> {
HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();
public StableArrayAdapter(Context context, int textViewResourceId, List<String> objects) {
super(context, textViewResourceId, objects);
for (int i = 0; i < objects.size(); ++i) {
mIdMap.put(objects.get(i), i);
}
}
@Override
public long getItemId(int position) {
String item = getItem(position);
return mIdMap.get(item);
}
@Override
public boolean hasStableIds() {
return true;
}
}
任何想法?
编辑:我也刚刚发现,在我的帮助屏幕,这只是一系列TextViews文本没有包装。当然,每个TextView只是变成一个大的行,没有水平滚动。我已经用完全相同的方式编写了其他应用程序(据我所知),我以前没有遇到过这个问题?
你在哪里设置文本值? – keshav
它们被加载到onCreate方法中称为NOTAMS的ArrayList中。 –
Gavin
在你的StableArrayAdapter中,你应该重写getView() – keshav