我遇到了一个问题,我有一个listView
我使用自定义数组适配器填充,问题是当我使用自定义数组适配器时,存储在ArrayList
赢得的数据t显示但如果我使用正常ArrayAdapter
,则会显示数据。我需要使用自定义数组适配器来设置listview
的样式,因为我无法在正常ArrayAdapter
这是代码: -
CustomAdapter adapter;
Context context;
ArrayList<String> data;
ListView listView;
private static String newline = System.getProperty("line.separator");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_production_comments);
//defining list view
listView = (ListView)findViewById(R.id.listView);
//defining data array list to store retrieved data from database
data = new ArrayList<String>();
adapter = new CustomAdapter(this,android.R.layout.simple_list_item_1, data);
listView.setAdapter(adapter);
context = this;
Toast.makeText(this,"Loading Please Wait..",Toast.LENGTH_LONG).show();
new AsyncLoadProdComments().execute();
}
protected class AsyncLoadProdComments extends AsyncTask<Void, JSONObject,
ArrayList<ProductionCommentsTable>> {
ArrayList<ProductionCommentsTable> ProductionCommentsTable = null;
@Override
protected ArrayList<ProductionCommentsTable> doInBackground(Void... params) {
// TODO Auto-generated method stub
RestAPI api = new RestAPI();
try {
JSONObject jsonObj = api.GetProductionComments();
JSONParser parser = new JSONParser();
ProductionCommentsTable = parser.parseProductionComments(jsonObj);
} catch (Exception e) {
// TODO Auto-generated catch block
Log.d("AsyncLoadProdDetails", e.getMessage());
}
return ProductionCommentsTable;
}
@Override
protected void onPostExecute(ArrayList<ProductionCommentsTable> result) {
// TODO Auto-generated method stub
for (int i = 0; i < result.size(); i++) {
//Log.d("Data1", String.valueOf(result));
data.add("Date: " + result.get(i).getDate().substring(0, 10) + newline + newline +
"Item: " + result.get(i).getItem() + newline + newline +
result.get(i).getComments());
}
adapter.notifyDataSetChanged();
Toast.makeText(context,"Loading Completed", Toast.LENGTH_SHORT).show();
}
}
private class CustomAdapter extends ArrayAdapter {
private Context mContext;
private int id;
private List <String>items ;
public CustomAdapter(Context context, int textViewResourceId , ArrayList<String> list )
{
super(context, textViewResourceId, list);
mContext = context;
id = textViewResourceId;
items = list ;
}
@Override
public View getView(int position, View v, ViewGroup parent)
{
View mView = v ;
if(mView == null){
LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mView = vi.inflate(id, null);
}
if(position % 2 == 0){
mView.setBackgroundColor(Color.CYAN);
}else{
mView.setBackgroundColor(Color.YELLOW);
}
return mView;
}
}
答案 0 :(得分:0)
您使用阵列适配器是错误的。它有自己的数组存储数据,所以不要将数据添加到数据数组列表,而是将其添加到适配器。它有方法IOException
答案 1 :(得分:0)
假设您的布局'simple_list_item_1'包含带
的TextViewwxFormbuilder
要做一个非常简单的ViewHolder模式实现, 在“CustomAdapter”类中添加一个静态类:
android:id="@+id/tv_text"
然后改变你的'getView()'方法:
static class ViewHolder {
public TextView tvText1;
}
ViewHolder模式在处理由不同视图组成的列表行时非常有用,因此学习如何使用它当然是值得的。 以下链接导致一个很好的 example in SO