0
我實施ListView
並輸入getView()
時,我一直都想與位置0位置指數始終在getView返回0和程序違反
這是我的活動代碼:
public class CompaniesActivity extends AppCompatActivity {
ListView listViewCompanies;
int categoryId;
CompanyLoader companyLoader;
ListCompanyDtoOuterClass.ListCompanyDto companiesDto;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_companies);
listViewCompanies = (ListView)findViewById(R.id.listViewCompanies);
categoryId = getIntent().getIntExtra("IdCategory",0);
companyLoader = new CompanyLoader();
getCompaniesByCategoryId();
}
private <R extends ListCompanyDtoOuterClass.ListCompanyDto> void getCompaniesByCategoryId() {
new AsyncTask<Object, Void, R>() {
@Override
protected R doInBackground(Object... params) {
return (R) companyLoader.GetCompaniesByCategoryId(categoryId);
}
@Override
protected void onPostExecute(R companies) {
startList(companies);
}
}.execute();
}
private void startList(ListCompanyDtoOuterClass.ListCompanyDto companies){
companiesDto = companies;
ArrayList<CompanyDtoOuterClass.CompanyDto> cc = new ArrayList<>();
for(int i=0;i<companies.getCompaniesCount();i++){
cc.add(companies.getCompanies(i));
}
final CompanyAdapter adapter = new CompanyAdapter(this,R.layout.company_item_layout,cc);
listViewCompanies.setAdapter(adapter);
}
}
而下面的代碼是我Adapter
package com.example.cristian_franco.cic.Adapters;
public class CompanyAdapter extends ArrayAdapter<CompanyDtoOuterClass.CompanyDto>{
private Context context;
private ArrayList<CompanyDtoOuterClass.CompanyDto> companies;
public CompanyAdapter(Context context, int resource, ArrayList<CompanyDtoOuterClass.CompanyDto> objects) {
super(context, resource, objects);
this.companies = objects;
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vista = convertView;
if(vista == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
vista = inflater.inflate(R.layout.company_item_layout, parent, false);
}
if(companies.get(position).getURLImage() == null)
return vista;
ImageView image = (ImageView) vista.findViewById(R.id.IconComapny);
TextView name = (TextView) vista.findViewById(R.id.nombreCompanyTextView);
name.setText(companies.get(position).getName());
try {
new DownloadImageTask((ImageView) vista.findViewById(R.id.Icon))
.execute(companies.get(position).getURLImage());
}catch (Exception e){
Toast.makeText(getContext(), "valio", Toast.LENGTH_SHORT).show();
}
return vista;
}
@Override
public Context getContext() {
return context;
}
public void setContext(Context context) {
this.context = context;
}
}
這裏是我的佈局:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<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: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="com.example.cristian_franco.cic.CompaniesActivity">
<ListView
android:id="@+id/listViewCompanies"
android:layout_width="wrap_content"
android:layout_height="match_parent">
</ListView>
可以採取什麼問題嗎?
你不發表您的佈局,但我會檢查你的'ListView'高度不設置爲'wrap_content',否則'的ListView '可能會多次請求位置0。 –
公司Dto =公司;檢查這一行你使用它 – Haroon
我貼布局 –