我想将arrayList的字段添加到listView但是如何编写我不知道的适配器。请帮帮我!!!
我需要一个适配器类。我必须在listView tutar上看到:1.34 kalem =“xx”
类别:
public class Income {
String kalem;
int tutar;
public int getTutar() {
return tutar;
}
public void setTutar(int tutar) {
this.tutar = tutar;
}
int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getKalem() {
return kalem;
}
public void setKalem(String kalem) {
this.kalem = kalem;
}
}
的ArrayList:
sqliteHelper helper = new sqliteHelper(getApplicationContext());
ArrayList<Income> liste=helper.getAllIncome();
答案 0 :(得分:1)
SimpleCursorAdapter
ArrayAdapter,etc
ListView listView = (ListView) findViewById(R.id.<list_view_id>);
ArrayList<Income> income_array_list = <get the arraylist from whatever sources>;
ArrayAdapter<Income> arrayAdapter = new ArrayAdapter<Income>(
this,
android.R.layout.simple_list_item_1,
income_array_list );
listView.setAdapter(arrayAdapter);
我只使用了两个字段来向您展示如何构建自定义适配器,您可以通过查看此代码来构建。
private class IncomeListAdapter extends BaseAdapter {
ArrayList<Income> mDisplayedValues;
LayoutInflater inflater;
public IncomeListAdapter(Context context) {
// TODO Auto-generated constructor stub
mDisplayedValues = <your_Income_list>;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mDisplayedValues.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
// TODO Auto-generated method stub
View v = convertView;
ViewHolder holder;
if (v == null) {
v = inflater.inflate(R.layout.listrow_income, null);
holder = new ViewHolder();
holder.tvTutar = (TextView) v
.findViewById(R.id.tvTutar);
holder.tvId = (TextView) v.findViewById(R.id.tvId);
v.setTag(holder);
} else
holder = (ViewHolder) v.getTag();
holder.tvTutar.setText(mDisplayedValues.get(position).getTutar());
holder.tvId.setText(mDisplayedValues.get(position).getId());
return v;
}
class ViewHolder {
TextView tvTutar;
TextView tvId;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:gravity="center"
android:orientation="horizontal"
android:padding="8dp" >
<TextView
android:id="@+id/tvTutar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#323232" />
<TextView
android:id="@+id/tvId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white" />
</LinearLayout>
答案 1 :(得分:0)
试试这个
private ListView lv;
public void onCreate(Bundle saveInstanceState) {
setContentView(R.layout.your_layout);
lv = (ListView) findViewById(R.id.your_list_view_id);
// Instanciating an array list (you don't need to do this,
// you already have yours).
sqliteHelper helper = new sqliteHelper(getApplicationContext());
ArrayList<Income> your_array_list = helper.getAllIncome();
// This is the array adapter, it takes the context of the activity as a
// first parameter, the type of list view as a second parameter and your
// array as a third parameter.
ArrayAdapter<Income> arrayAdapter = new ArrayAdapter<Income>(
this,
android.R.layout.simple_list_item_1,
your_array_list );
lv.setAdapter(arrayAdapter);
}