我有一个由自定义适配器填充的Spinner。
我可以在自定义适配器中注销所有TextView,并查看该位置的文本内容。
位置0处的文字视图:ABC公司
文字视图位置1文字:DEF公司
位置2的文字视图:GHI公司
文字观点位置3文:JKL公司
位置4的文字视图:MNO公司
文字视图第5位文字:PQR公司
所以我知道传递数据并不是问题所在。
选择微调器时,在应用程序中它只显示空白。
任何人都会看到我做错了什么?
public class MyActivity extends AppCompatActivity {
private Spinner spinnerCompanies;
private ArrayList<HashMap> arrayOfCompanies;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_call);
spinnerCompanies = (Spinner) findViewById(R.id.spCompany);
//do background task and pass JSONArray to processCompanies
processCompanies(jsonArray);
}
public void processCompanies(JSONArray jsonArray){
arrayOfCompanies = new ArrayList<HashMap>();
HashMap company;
JSONObject record;
for (int i = 0; i < jsonArray.length(); i++) {
record = jsonArray.getJSONObject(i);
company = new HashMap<String, String>();
company.put("NAME", record.getString("CompanyName"));
arrayOfCompanies.add(company);
}
CompanyAdapter companyAdapter = new CompanyAdapter(arrayOfCompanies);
spinnerCompanies.setAdapter(companyAdapter);
spinnerCompanies.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
didSelectCompany(position);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
private void didSelectCompany(int position){
Log.i(TAG, "didSelectCompany: " + position);
}
public class CompanyAdapter extends BaseAdapter {
private final ArrayList<HashMap> mArrayList;
public CompanyAdapter(ArrayList<HashMap> map) {
mArrayList = map;
}
@Override
public int getCount() {
return mArrayList.size();
}
@Override
public HashMap<String, String> getItem(int position) {
return mArrayList.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_company, parent, false);
}
HashMap<String, String> item = getItem(position);
TextView textView = (TextView) convertView.findViewById(R.id.tvSubTermainalName);
textView.setText(item.get("NAME"));
Log.d(TAG, "TEXT VIEW AT POSITION " + position + " TEXT: " + textView.getText());
return convertView;
}
}
}
自定义适配器使用R.layout.item_company XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tvSubTermainalName"/>
</LinearLayout>
答案 0 :(得分:0)
您是否尝试删除部件
if(convertView==null)
并尝试永远膨胀视图?