我有一个arraylist List<Cars> myCars = new ArrayList<Cars>();
我的MainActivity布局有一个列表视图和一个按钮。还有一个布局
(&#34; each_car.xml&#34;)及其内容如下: -
<?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="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/tv_carName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/tv_carMaker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
</LinearLayout>
My Cars课程有两个字段,&#34; name&#34;和&#34;制造商&#34;。
public class Cars{
String name;
String manufacturer;
}
我通过异步任务填充此列表(myCars)。任务完成后,即在onPostExecute()内部,我在MainActivity中调用一个方法 这将使用列表中的项目填充列表视图。以下是方法: -
public void populateListView(List<Cars> data){
ArrayAdapter<Cars> carAdapter = new CarAdapter(MainActivity.this, data);
ListView dataList = (ListView) findViewById(R.id.car_listView);
dataList.setAdapter(carAdapter);
}
以下是我的CarAdapter课程: -
public class CarAdapter extends ArrayAdapter<Cars>{
private LayoutInflater inflater;
List<Cars> dummyData = new ArrayList<Cars>();
MainActivity host;
public CarAdapter(MainActivity host, List<Cars> data)
{
super(host,R.layout.each_car,data);
inflater = host.getWindow().getLayoutInflater();
dummyData = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View itemView = convertView;
if(itemView == null)
{
itemView = inflater.inflate(R.layout.each_car,parent,false);
}
Cars currentData = dummyData.get(position);
Log.d("Testing Position","Position :- "+position);
TextView carName = (TextView) itemView.findViewById(R.id.tv_carName);
carName.setText(currentData.getName());
TextView carMaker = (TextView) itemView.findViewById(R.id.tv_carMaker);
carMaker.setText(currentData.getManufacturer());
return itemView;
}
}
这将立即用整个List(myCars)填写listview。
问题: - 当我点击按钮时,我想一次填充listView一个项目。我不知道应该如何修改我的代码。
答案 0 :(得分:0)
选中此链接以查看自定义适配器enter link description here。填充列表后,将列表传递给适配器的构造函数,不要忘记调用notifydatasetchanged。每当在列表视图中添加或删除新项目时,它都会更新列表视图。
答案 1 :(得分:0)
以下是一些教程。如果您不知道该怎么做,您需要做一些研究并遵循教程。 link one,link two和link three
activity_main.xml中
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</RelativeLayout>
适配器类
public class myListAdapter extends BaseAdapter{
List<String> item_list = new ArrayList<String>();
public myListAdapter(){
}
void add(String item_text){
item_list.add(item_text);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return item_list.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.list_item, null);
TextView tvText = (TextView)view.findViewById(R.id.list_text);
tvText.setText(item_list.get(position));
return null;
}
}
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="@+id/list_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
onCreate中的实现:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myListAdapter adapter = new myListAdapter();
ListView list = (ListView) findViewById(R.id.list);
adapter.add("Some value");
list.setAdapter(adapter);
}
答案 2 :(得分:0)
This may help you ...
public class car extends AsyncTask<Void, Void, ArrayList<Cars> {
@Override
protected ArrayList<Cars> doInBackground(Void... params) {
... add your list here...
return myCars ;
}
@Override
protected void onPostExecute(ArrayList<Cars> result) {
super.onPostExecute(result);
//as per your requirement
adapter_car = new SimpleAdapter(ExchangeRateActivity.this, result,
R.layout.row, from, to);
list_view.setAdapter(adapter_car);
}
答案 3 :(得分:0)
这是一个示例和简单的方法首先你想要给出0索引元素的元素然后在下一次单击时首先清除listview然后用2个元素调用你的Adapter类并用一些计数器重复同样的事情点击并调用你的数量适配器类与该数组列表的限制可能是这个策略帮助你..