JSON 使用自定义适配器的 ListView 中的数据
JSON Data in ListView using Custom Adapter
我正在尝试从 JSON
数据创建一个自定义 Adapter
,我可以创建一个 ListView
,所有数据都打包在每条记录的一行中,但我希望每个值在它的行。示例 JSON
{
"outages": [
{
"outagnumber": "567273",
"impact": "half a block",
"status": "almost complete",
"timestamp": "07-19-2015 12:00:00am"
},
{
"outagnumber": "567243",
"impact": "whole block",
"status": "whating on crew",
"timestamp": "07-19-2015 11:00:00am"
},
]
}
实现此目标的最佳方法是什么?
适配器
public class CustomListOutageAdapter extends ArrayAdapter{
private final Activity context;
private final LinkedList<HashMap<String, String>> itemname;
public CustomListOutageAdapter(Activity context, LinkedList<HashMap<String, String>> itemname) {
super(context, R.layout.mylistoutagedetails, itemname);
// TODO Auto-generated constructor stub
this.context = context;
}
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.mylistoutagedetails, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.name);
txtTitle.setText(itemname.get(position).toString());
return rowView;
};
}
将项目更改为 JSONArray 怎么样?
我不熟悉您的 ArrayAdapter,已更改为 BaseAdapter。
代码如下所示..
有一些像viewHolder这样的成语,它只是保留它的视图,以方便访问。
import org.json.JSONArray;
public class CustomListOutageAdapter extends BaseAdapter{
private final Activity mContext;
private final JSONArray mData;
/// items is
public CustomListOutageAdapter(Activity context, JSONArray items) {
//super(context, R.layout.mylistoutagedetails, itemname);
this.mContext = context;
this.mData = items;
}
public View getView(int position, View view, ViewGroup parent) {
ViewHolder vh;
if (view == null) {
LayoutInflater inflater = context.getLayoutInflater();
view = inflater.inflate(R.layout.mylistoutagedetails, null, true);
vh = new ViewHolder();
vh.txtName = (TextView) view.findViewById(R.id.name);
view.setTag(vh);
}
else {
vh = (ViewHolder) view.getTag();
}
if (mData.size() > position) {
vh.txtTitle.setText(mData.get(position).toString());
}
return view;
}
public class ViewHolder {
TextView txtName; // so on...
}
我觉得您在理解适配器方面需要一些帮助,所以让我们从头开始。希望对你有帮助。
您遇到的问题是适配器只是一个 "cell",适配器将创建 x 个单元格。
适配器需要一个列表(arraylist、list、linkedlist...)并将列表中的每个元素用作单元格的内容,默认情况下,它将生成单元格的数量。
您可以覆盖 getCount()
方法来指示您可能需要的不同数量的单元格。 (您甚至可以执行 someList.size() + 4
之类的操作,或者您想要添加到单元格数量的任何其他操作)
因此,如果您想在各自的单元格中显示这 8 个元素,您有以下几种选择:
在 activity(不是适配器)中,您可以使用 json 的每个元素创建一个新列表(arrayList 可能更容易)。这样一来,您就可以传递 1 个列表,其中所有对象都已单独分开。
这通常是一个糟糕的做法和一个坏主意,但既然你想以这种奇怪的方式显示它,这可能是最快的方式。
再次在 activity 中,您可以创建一个名为 "outages" 的对象,其中包含 "outages" 标记中的元素(outagnumber、impact...),然后将这些对象添加到一个新列表中,并将这个新列表传递给适配器。然后适配器将有一个包含 2 个对象的列表(在此示例中),然后您可以创建 2 个单元格,每个单元格包含 4 个 textview 元素,并将每个对象的每个元素添加到每个 textview。
这可能是最好的方法,但每个元素都不会在自己的单元格中。
您可以使用@nolleh 建议的 viewHolder 方法,尽管由于您现在使用的是 Recycler 视图,这会使这个问题变得过于复杂。
如果有帮助请告诉我。
编辑:添加示例
下面是一个示例,说明如何使用适用于 2 个单元格的适配器执行此操作,每个单元格都包含您想要的文本:
Activity class
public class OutagesActivity extends ListActivity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Please don't create objects like this. This is just an example
Outages outagesObject = new Outages();
outagesObject.outagnumber = jsonValue;
outagesObject.impact = jsonValue;
outagesObject.status = jsonValue;
outagesObject.timestamp = jsonValue;
List<Outages> listOfOutagesObjects = new ArrayList<Outages>();
listOfOutagesObjects.add(outagesObject);
listOfOutagesObjects.add(outagesObject2);
OutagesAdapter adapter = new OutagesAdapter(listOfOutagesObjects);
setListAdapter(adapter);
}
}
适配器class
public class OutagesAdapter extends ArrayAdapter<Outages>
{
private List<Outages> listOfOutagesObjects;
public InitialGetDataActivityAdapter(List<Outages> listOfOutagesObjects)
{
super(context, R.layout.single_row, listOfOutagesObjects);
this.listOfOutagesObjects = listOfOutagesObjects;
}
@Override
public int getCount() {
return listOfOutagesObjects.size();
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
Outages outagesObject = listOfParseObjects.get(position);
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.single_row, parent, false);
TextView tv_outagnumber = (TextView) rowView.findViewById(R.id.tv_outagnumber);
TextView tv_impact = (TextView) rowView.findViewById(R.id.tv_impact);
TextView tv_status = (TextView) rowView.findViewById(R.id.tv_status);
TextView tv_timestamp = (TextView) rowView.findViewById(R.id.tv_timestamp);
tv_outagnumber.setText(outagesObject.outagnumber);
tv_impact.setText(outagesObject.impact);
tv_status.setText(outagesObject.status);
tv_timestamp.setText(outagesObject.timestamp);
return rowView;
}
}
并且您需要按照您想要的任何布局顺序为您想要的每个文本视图创建布局文件 (R.layout.single_row)。
我正在尝试从 JSON
数据创建一个自定义 Adapter
,我可以创建一个 ListView
,所有数据都打包在每条记录的一行中,但我希望每个值在它的行。示例 JSON
{
"outages": [
{
"outagnumber": "567273",
"impact": "half a block",
"status": "almost complete",
"timestamp": "07-19-2015 12:00:00am"
},
{
"outagnumber": "567243",
"impact": "whole block",
"status": "whating on crew",
"timestamp": "07-19-2015 11:00:00am"
},
]
}
实现此目标的最佳方法是什么?
适配器
public class CustomListOutageAdapter extends ArrayAdapter{
private final Activity context;
private final LinkedList<HashMap<String, String>> itemname;
public CustomListOutageAdapter(Activity context, LinkedList<HashMap<String, String>> itemname) {
super(context, R.layout.mylistoutagedetails, itemname);
// TODO Auto-generated constructor stub
this.context = context;
}
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.mylistoutagedetails, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.name);
txtTitle.setText(itemname.get(position).toString());
return rowView;
};
}
将项目更改为 JSONArray 怎么样? 我不熟悉您的 ArrayAdapter,已更改为 BaseAdapter。
代码如下所示.. 有一些像viewHolder这样的成语,它只是保留它的视图,以方便访问。
import org.json.JSONArray;
public class CustomListOutageAdapter extends BaseAdapter{
private final Activity mContext;
private final JSONArray mData;
/// items is
public CustomListOutageAdapter(Activity context, JSONArray items) {
//super(context, R.layout.mylistoutagedetails, itemname);
this.mContext = context;
this.mData = items;
}
public View getView(int position, View view, ViewGroup parent) {
ViewHolder vh;
if (view == null) {
LayoutInflater inflater = context.getLayoutInflater();
view = inflater.inflate(R.layout.mylistoutagedetails, null, true);
vh = new ViewHolder();
vh.txtName = (TextView) view.findViewById(R.id.name);
view.setTag(vh);
}
else {
vh = (ViewHolder) view.getTag();
}
if (mData.size() > position) {
vh.txtTitle.setText(mData.get(position).toString());
}
return view;
}
public class ViewHolder {
TextView txtName; // so on...
}
我觉得您在理解适配器方面需要一些帮助,所以让我们从头开始。希望对你有帮助。
您遇到的问题是适配器只是一个 "cell",适配器将创建 x 个单元格。
适配器需要一个列表(arraylist、list、linkedlist...)并将列表中的每个元素用作单元格的内容,默认情况下,它将生成单元格的数量。
您可以覆盖 getCount()
方法来指示您可能需要的不同数量的单元格。 (您甚至可以执行 someList.size() + 4
之类的操作,或者您想要添加到单元格数量的任何其他操作)
因此,如果您想在各自的单元格中显示这 8 个元素,您有以下几种选择:
在 activity(不是适配器)中,您可以使用 json 的每个元素创建一个新列表(arrayList 可能更容易)。这样一来,您就可以传递 1 个列表,其中所有对象都已单独分开。 这通常是一个糟糕的做法和一个坏主意,但既然你想以这种奇怪的方式显示它,这可能是最快的方式。
再次在 activity 中,您可以创建一个名为 "outages" 的对象,其中包含 "outages" 标记中的元素(outagnumber、impact...),然后将这些对象添加到一个新列表中,并将这个新列表传递给适配器。然后适配器将有一个包含 2 个对象的列表(在此示例中),然后您可以创建 2 个单元格,每个单元格包含 4 个 textview 元素,并将每个对象的每个元素添加到每个 textview。 这可能是最好的方法,但每个元素都不会在自己的单元格中。
您可以使用@nolleh 建议的 viewHolder 方法,尽管由于您现在使用的是 Recycler 视图,这会使这个问题变得过于复杂。
如果有帮助请告诉我。
编辑:添加示例
下面是一个示例,说明如何使用适用于 2 个单元格的适配器执行此操作,每个单元格都包含您想要的文本:
Activity class
public class OutagesActivity extends ListActivity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Please don't create objects like this. This is just an example
Outages outagesObject = new Outages();
outagesObject.outagnumber = jsonValue;
outagesObject.impact = jsonValue;
outagesObject.status = jsonValue;
outagesObject.timestamp = jsonValue;
List<Outages> listOfOutagesObjects = new ArrayList<Outages>();
listOfOutagesObjects.add(outagesObject);
listOfOutagesObjects.add(outagesObject2);
OutagesAdapter adapter = new OutagesAdapter(listOfOutagesObjects);
setListAdapter(adapter);
}
}
适配器class
public class OutagesAdapter extends ArrayAdapter<Outages>
{
private List<Outages> listOfOutagesObjects;
public InitialGetDataActivityAdapter(List<Outages> listOfOutagesObjects)
{
super(context, R.layout.single_row, listOfOutagesObjects);
this.listOfOutagesObjects = listOfOutagesObjects;
}
@Override
public int getCount() {
return listOfOutagesObjects.size();
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
Outages outagesObject = listOfParseObjects.get(position);
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.single_row, parent, false);
TextView tv_outagnumber = (TextView) rowView.findViewById(R.id.tv_outagnumber);
TextView tv_impact = (TextView) rowView.findViewById(R.id.tv_impact);
TextView tv_status = (TextView) rowView.findViewById(R.id.tv_status);
TextView tv_timestamp = (TextView) rowView.findViewById(R.id.tv_timestamp);
tv_outagnumber.setText(outagesObject.outagnumber);
tv_impact.setText(outagesObject.impact);
tv_status.setText(outagesObject.status);
tv_timestamp.setText(outagesObject.timestamp);
return rowView;
}
}
并且您需要按照您想要的任何布局顺序为您想要的每个文本视图创建布局文件 (R.layout.single_row)。