我有一个BaseAdapter来显示带有部分的列表视图。对于每一行,我都有一个文本视图,现在我想为每一行添加另一个文本视图。在 Item
类和 getView()
方法中,我添加了一个新的字符串text_id
,但现在当我调用适配器时,出现此错误:Item(String, String) in item cannot be applied
。
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 final String text_id;
public Item(String text, String text_id ) {
this.text = text;
this.text_id = text_id;
}
}
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 textView_id = (TextView) view.findViewById(R.id.id_cl);
textView.setText(item.text);
textView_id.setText(item.text_id);
} 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;
}
}
in OnCreateView()
List<String> clienti = populateclienti();
Collections.sort(clienti);
List<AlphabetListAdapter.Row> rows = new ArrayList<AlphabetListAdapter.Row>();
int start = 0;
int end = 0;
String previousLetter = null;
Object[] tmpIndexItem = null;
for (String country : clienti) {
String firstLetter = country.substring(0, 1);
// 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.ITALIAN);
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 AlphabetListAdapter.Section(firstLetter));
sections.put(firstLetter, start);
}
// Add the country to the list
rows.add(new AlphabetListAdapter.Item(country));
previousLetter = firstLetter;
}
if (previousLetter != null) {
// Save the last letter
tmpIndexItem = new Object[3];
tmpIndexItem[0] = previousLetter.toUpperCase(Locale.ITALY);
tmpIndexItem[1] = start;
tmpIndexItem[2] = rows.size() - 1;
alphabet.add(tmpIndexItem);
}
adapter.setRows(rows);
lista.setAdapter(adapter);
lista.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView temp = (TextView) view.findViewById(R.id.textView1);
String str = temp.getText().toString();
Toast.makeText(getActivity(),str + " is pressed " + position, Toast.LENGTH_SHORT).show();
}
});
updateList();
return rootView;
}
row_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="10dp" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="#000000"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="@+id/id_cl" />
实现
此目的的最简单方法是在 XML 布局文件中添加另一个TextView
R.layout.row_section