public class Setting extends Activity implements OnClickListener {
ListView listView1;
ImageView backbutton;
String Url = "http://182.71.212.110:8083/api/values/userdetails";
String Id;
String Designation;
String EmployeeName;
JSONArray _jarray;
List<RowItem> rowItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.setting);
listView1 = (ListView) findViewById(R.id.listView1);
backbutton = (ImageView) findViewById(R.id.backbutton);
backbutton.setOnClickListener(this);
new GetUserdetail().execute();
CustomList adapter = new CustomList(this, rowItems);
listView1.setAdapter(adapter);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId() == R.id.backbutton) {
finish();
}
}
class GetUserdetail extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
try {
String json = HttpHitter.ExecuteData(Url);
_jarray = new JSONArray(json);
System.out.println("_jarray" + _jarray);
for (int i = 0; i <= _jarray.length(); i++) {
JSONObject _obj = _jarray.getJSONObject(i);
Id = _obj.getString("Id");
Designation = _obj.getString("Designation");
EmployeeName = _obj.getString("EmployeeName");
System.out.println(Id + "" + Designation + ""
+ EmployeeName);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
rowItems = new ArrayList<RowItem>();
}
}
}
这是我的代码,我能够在Jason解析这一行System.out.println之后显示值(Id +&#34;&#34; + Designation +&#34;&#34; + EmployeeName);
但我无法在Listview中打印数据,我创建时会出现错误 数据模型
public class RowItem {
public RowItem(String title, String desc) {
this.title = title;
this.desc = desc;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
private String title;
private String desc;
}
我已经创建了适配器,我从Base适配器扩展,工作正常,请告诉我如何绑定值并在列表视图中显示Json Parsing之后请建议我尝试实现。
答案 0 :(得分:1)
使用Custom Adapter
将 JSON数据绑定到Listview ,如下所示:
public class ListViewAdapterForLead extends BaseAdapter {
Context context;
LayoutInflater inflater;
ArrayList<SpinnerNavItem> data;
TextView txtText;
public ListViewAdapterForLead(Context context,ArrayList<SpinnerNavItem> arraylist) {
this.context = context;
data = arraylist;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int index) {
return data.get(index);
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.row_lead, null);
}
txtText = (TextView) convertView.findViewById(R.id.textLeadMenu);
txtText.setText(data.get(position).getTitle());
return convertView;
}
public View getDropDownView(int position,View convertView,ViewGroup parent){
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.row_lead, null);
}
txtText = (TextView) convertView.findViewById(R.id.textLeadMenu);
txtText.setText(data.get(position).getTitle());
return convertView;
}
}
答案 1 :(得分:0)
在自定义适配器的getView()
方法中绑定您的值。
答案 2 :(得分:0)
您应该在后台任务完成后设置数据。
*首先尝试在onPostExecute()方法中设置列表适配器
*确保模型中的所有细节都已填满
*在Adapter类中使用getView()方法来解析数据。
有关使用BaseAdapter的ListView的示例代码,请参阅以下链接
http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/
答案 3 :(得分:0)
你忘了一件事,这就是List为空的原因
将解析后的数据添加到列表
//initialize your list here
rowItems = new List<RowItems>();
for (int i = 0; i <= _jarray.length(); i++) {
JSONObject _obj = _jarray.getJSONObject(i);
RowItemsr = new RowItems();
Id = _obj.getString("Id");
Designation = _obj.getString("Designation");
EmployeeName = _obj.getString("EmployeeName");
System.out.println(Id + "" + Designation + ""
+ EmployeeName);
// you must create an object and add it to your list
r.setTitle(EmployeeName);
r.setDesc(Designation);
rowItems.add(r);
}
这就是你需要的