我尝试制作一个简单的自定义ListView列表项目,以帮助我了解它是如何工作的,但它不会起作用。
当我在MainActivity.java中使用注释行时,一切正常(它使用内置布局)。 但是当我尝试自己的布局row_layout而不是simple_list_item_1时,我在启动程序时会得到一个ANR。
MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] colors = {"blue", "red", "yellow", "green", "purple", "orange"};
//ListAdapter listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, colors);
ListAdapter listAdapter = new ArrayAdapter<String>(this, R.layout.row_layout, colors);
ListView listView = (ListView)findViewById(R.id.theListView);
listView.setAdapter(listAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String auswahl = "Auswahl:" + String.valueOf(parent.getItemAtPosition(position));
Toast.makeText(MainActivity.this, auswahl, Toast.LENGTH_LONG).show();
}
});
}
}
activity_main.xml中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/theListView"></ListView></LinearLayout>
row_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView1"
android:textSize="30sp"
android:textStyle="bold"
android:padding="15dp"/></LinearLayout>
答案 0 :(得分:2)
当您调用适配器时,需要在自定义行中提供TextView的ID。
ListAdapter listAdapter = new ArrayAdapter<String>(this, R.layout.row_layout, R.id.textView1, colors);
答案 1 :(得分:0)
将文字视图ID更改为
android:id="@android:id/text1"
答案 2 :(得分:0)
您的列表适配器无法知道您的自定义布局上的文字必须如何显示。
显示您必须创建一个自定义适配器来绘制listview行,或者您可以用
替换row_layout.xml<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:minHeight="?android:attr/listPreferredItemHeightSmall" />
并在该textview上进行更改。
(实际上这是simple_list_item_1的代码)
答案 3 :(得分:0)
最有效的方法是创建自己的适配器
public class YourAdapter extends BaseAdapter {
private final Context context;
private TextView mTextView
private LayoutInflater inflater;
private String[] colors;
public YourAdapter(Context context, String[] colors) {
this.context = context;
this.colors = colors;
}
/**
* How many items are in the data set represented by this Adapter.
*/
@Override
public int getCount() {
return colors.length;
}
/**
* Get the data item associated with the specified position in the data set.
*
* @param position Position of the item whose data we want within the adapter's
* data set.
* @return The data at the specified position.
*/
@Override
public Object getItem(int position) {
return position;
}
/**
* Get the row id associated with the specified position in the list.
*
* @param position The position of the item within the adapter's data set whose row id we want.
* @return The id of the item at the specified position.
*/
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (view == null) view = inflater.inflate(R.layout.row_layout, null);
mTextView = view.findItemById(R.id.textview1);
String color = colors[position];
mTextView.setText(color);
return view;
}
}