我是android的新手,我正在尝试创建一个自定义列表视图,在每个行中都有多个文本视图。为此我创建了一个自定义列表适配器,通过扩展ArrayListAdapter并覆盖getView方法并将适配器设置为在我的主要活动中列出适应器。但是每次我运行app listview都拒绝填充。
Start.java
public class Start extends Activity {
String jasonString="[{\"name\": \"abc\",\n" +
"\"address\":\"Delhi\",\n" +
"\"email\":\"abc@gmail.com\"},{\"name\": \"def\",\n" +
"\"address\":\"Delhi\",\n" +
"\"email\":\"def@gmail.com\"},{\"name\": \"ghi\",\n" +
"\"address\":\"ferf\",\n" +
"\"email\":\"ghi@gmail.com\"},{\"name\": \"jkl\",\n" +
"\"address\":\"Delheri\",\n" +
"\"email\":\"jkl@gmail.com\"}\n" +
",{\"name\": \"mno\",\n" +
"\"address\":\"Delhi\",\n" +
"\"email\":\"mno@gmail.com\"},{\"name\": \"pqr\",\n" +
"\"address\":\"Delhi\",\n" +
"\"email\":\"pqr@gmail.com\"}]\n";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.i("mee","Activity created.");
ListView lv= (ListView) findViewById(R.id.list1);
ArrayList<Model> list=new ArrayList<>();
JsonArray array=new JsonParser().parse(jasonString).getAsJsonArray();
for(JsonElement element:array){
Model mod=new Gson().fromJson(element,Model.class);
list.add(mod);
}
Log.i("mee","List size"+list.size());
for(Model mod:list){
Log.i("mee", "name:" + mod.name);
}
CustomListAdapter adapter=new CustomListAdapter(this,R.layout.row_item,list);
lv.setAdapter(adapter);
}
}
CustomListAdapter.class
public class CustomListAdapter extends ArrayAdapter<Model>{
private Context mContext;
private ArrayList<Model> mObjects;
public CustomListAdapter(Context context, int resource,ArrayList<Model> objects) {
super(context, resource, objects);
mContext=context;
mObjects=objects;
// TODO Auto-generated constructor stub
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
Log.i("meee","inside CustomListAdapter, getView");
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_item, parent, false);
}
((TextView) convertView.findViewById(R.id.textView1)).setText(mObjects.get(position).name);
((TextView) convertView.findViewById(R.id.textView2)).setText(mObjects.get(position).address);
((TextView) convertView.findViewById(R.id.textView3)).setText(mObjects.get(position).sex);
return convertView;
}
}
row_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="This is the main layout"/>
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/list1"></ListView>
</LinearLayout>
有人可以告诉我,我做错了什么?我没有收到任何错误,数据列表总是可用的数据。
答案 0 :(得分:0)
我已经尝试过您的代码了。将您的main.xml
布局更改为如下所示。
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is the main layout"
/>
<ListView
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="@+id/list1"
/>
</LinearLayout>
它对我有用。
答案 1 :(得分:0)
在main.xml中,您将textview设置为android:layout_height="fill_parent"
,因此textview将从上到下占据整个屏幕,不会显示ListView的空间。
将其更改为android:layout_height="wrap_content"
,它应该有效。