我有一个代码可以执行以下操作 -
的
for(int i=0; i < jArray.length() ; i++) {
json_data = jArray.getJSONObject(i);
int id=json_data.getInt("id");
String name=json_data.getString("name");
Log.d(name,"Output");
}
当我查看LogCat时,我会看到查询的所有“名称”,每条记录都会打印出来。我只需要将这些结果插入ListView。我怎么能做到这一点?
PS - 我没有ArrayAdapter的单独类。这可能是原因吗?
答案 0 :(得分:8)
如果您只想显示textView的列表,则不需要覆盖任何内容,只需将所有项添加到arrayList中并使用arrayAdapter即可。
在xml中放一个名为android:list的列表视图,然后用你想要使用的textView创建你的arrayAdapter。
之后你所要做的就是调用setListAdapter(mArrayAdapter)并填充你的列表。
ArrayList<String> items = new ArrayList<String>();
for(int i=0; i < jArray.length() ; i++) {
json_data = jArray.getJSONObject(i);
int id=json_data.getInt("id");
String name=json_data.getString("name");
items.add(name);
Log.d(name,"Output");
}
ArrayAdapter<String> mArrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_expandable_list_item_1, items));
setListAdapter(mArrayAdapter)
希望这有帮助!
答案 1 :(得分:1)
您可以查看此网页,其中有一个示例可以满足您的需求: http://p-xr.com/android-tutorial-how-to-parse-read-json-data-into-a-android-listview/
基本上,在打印名称的for循环中,您应该加载一个数组列表,其中包含您希望稍后插入列表的值。 请注意,该示例使用以下内容:
ArrayList< HashMap < String, String > > mylist = new ArrayList < HashMap < String, String > > ();
你有一个整数和一个字符串要添加到结构,所以你可以简单地将该ID转换为字符串并解决问题。 之后,您可以使用列表适配器,而无需创建单独的类:
ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main, new String[] { "name", "id" }, new int[] { R.id.item_title, R.id.item_subtitle });
其中“name”和“id”将是地图中json和item_title返回的名称和id值的关键字,item_subtitle用于“调整”文本的视图。
希望我足够清楚,看看这个例子无论如何都很简单。
答案 2 :(得分:1)
我不喜欢重新映射数据,而我已经将它们放在JSON数组中 所以这是我的代码...它对我来说很简单....希望它有所帮助
package ...;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class DataListFragment extends ListFragment{
JSONArray data;
public DataListFragment(){
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.i("mycode", "DataListFragment onActivityCreated");
data=((MainActivity)this.getActivity()).data;
Log.i("mycode", "data length "+data.toString());
setEmptyText("No Data Here");
final JSONArrayAdapter adapter = new JSONArrayAdapter(this.getActivity(),data);
setListAdapter(adapter);
// Start out with a progress indicator.
//setListShown(false);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Insert desired behavior here.
Log.i("mycode", "Item clicked: " + id);
}
private class JSONArrayAdapter extends BaseAdapter {
JSONArray data;
Context context;
public JSONArrayAdapter(Context context,JSONArray data) {
super();
this.context=context;
this.data=data;
}
@Override
public int getCount() {
return data.length();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
try {
return data.getJSONObject(arg0);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public boolean hasStableIds(){
return true;
}
@Override
public boolean isEmpty(){
return data==null || data.length()==0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.single_item, parent, false);
TextView textView1 = (TextView) rowView.findViewById(R.id.firstLine);
TextView textView2 = (TextView) rowView.findViewById(R.id.secondLine);
//ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
try {
JSONObject jo = (JSONObject) data.get(position);
textView1.setText(jo.getString("category_title"));
textView2.setText(jo.getString("description"));
} catch (JSONException e) {
e.printStackTrace();
}
return rowView;
}
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip" >
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginRight="6dip"
android:contentDescription="icon"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/secondLine"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_toRightOf="@id/icon"
android:ellipsize="marquee"
android:singleLine="true"
android:textSize="12sp" />
<TextView
android:id="@+id/firstLine"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@id/secondLine"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:layout_toRightOf="@id/icon"
android:gravity="center_vertical"
android:textSize="16sp" />
</RelativeLayout>
答案 3 :(得分:0)
将数据放入数组并使用ArrayAdapter将数据绑定到列表项。
见文章:
http://www.josecgomez.com/2010/05/03/android-putting-custom-objects-in-listview/