Я должен создать список, в котором одна строка имеет 4 текстовых поля. Моя проблема в том, что я не понимаю, как я могу использовать адаптер, чтобы кто-то мог мне помочь с адаптером и его реализацией?
это мой макет
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="192dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="20dp"
android:text="Grazie per aver parcheggiato da noi!"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ListView
android:id="@+id/mylist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
</ListView>
<Button
android:id="@+id/btnEsci"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Torna a scelta" />
</LinearLayout>
это единственная строка (single_feed)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- TEXT OF FEED -->
<TextView
android:id="@+id/txtId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Numero macchina: #"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/txtTarga"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Targa: #"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/txtOra"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ora ingresso: #"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/txtCosto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Prezzo uscita: #"
android:textAppearance="?android:attr/textAppearanceMedium"
android:lines="1"
/>
это мой адаптер:
public class MioAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public MioAdapter(Context context, String[] values) {
super(context, R.layout.single_feed, values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.single_feed, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
textView.setText(values[position]);
// Change the icon for Windows and iPhone
return rowView;
}
и, наконец, это моя деятельность:
public class checkout extends Activity{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
//apro la view
super.onCreate(savedInstanceState);
setContentView(R.layout.checkout);
//gestsco l'adapter per creare la lista
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2" };
MioAdapter adapter = new MioAdapter(this, values);
setListAdapter(adapter);
};
Кто-нибудь может помочь мне с адаптером? спасибо всем, что может мне помочь в этой легкой неприятности :)
См. Этот пользовательский listView
И есть два текстовых поля в строке, и вы просто добавляете четыре текстовых поля, чтобы добавить в файл макета строки. И добавьте в textview android:maxLines="1"
для однострочного текстового просмотра.
Для одной строки (single_feed):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
Также сохраняйте layout_width фиксированным для всех текстовых представлений, чтобы они могли вписываться в одну строку.
Сначала что-то не так. Если вам нужен ListView, вам нужен список элементов (один элемент, одна строка).
Поэтому, если вам нужно несколько TextViews для каждой строки, вам понадобится несколько значений для каждого элемента. Таким образом, вам понадобится массив массива, например.
Вы также можете попробовать SimpleCursorAdapter. Вы создаете MatrixCursor своими данными: MatrixCursor mc = new MatrixCursor(your_column_names);
и добавить элементы (по одному для каждой строки): mc.addRow(row_values);
(где your_column_names
и row_values
имеют тип String[]
)
Затем вы используете SimpleCursorAdapter, где String[] from = your_column_values
и int[] to = your_TextView_ids
(ids from single_feed.xml)
height
какfill_parent