所以我试图在片段中创建一个listview,虽然代码有效,但这个列表本身看起来很奇怪。 Here's a picture of it. 我很感激你的帮助,弄清楚为什么列表看起来很奇怪。
ussd.java(片段)
package layout;
import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import com.buckydroid.app.droidcpu.R;
import com.buckydroid.app.droidcpu.ussdadaper;
import com.buckydroid.app.droidcpu.ussdz;
import com.google.android.gms.tagmanager.Container;
import java.util.ArrayList;
import java.util.List;
/**
* A simple {@link Fragment} subclass.
* Activities that contain this fragment must implement the
* {@link ussd.OnFragmentInteractionListener} interface
* to handle interaction events.
* Use the {@link ussd#newInstance} factory method to
* create an instance of this fragment.
*/
public class ussd extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private ListView listView;
private ussdadaper ussd;
private List<ussdz> ussdlist;
private OnFragmentInteractionListener mListener;
public ussd() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment ussd.
*/
// TODO: Rename and change types and number of parameters
public static ussd newInstance(String param1, String param2) {
ussd fragment = new ussd();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState )
{
View rootView = inflater.inflate(R.layout.fragment_ussd, container, false);
listView = (ListView)rootView.findViewById(R.id.listView);
ussdlist = new ArrayList<>();
ussdlist.add(new ussdz("*#44336#","Software Version Info"));
ussdlist.add(new ussdz("*#44336#","Software Version Info"));
ussdlist.add(new ussdz("*#44336#","Software Version Info"));
ussdlist.add(new ussdz("*#44336#","Software Version Info"));
ussdlist.add(new ussdz("*#44336#","Software Version Info"));
ussdlist.add(new ussdz("*#44336#","Software Version Info"));
ussdlist.add(new ussdz("*#44336#","Software Version Info"));
ussdlist.add(new ussdz("*#44336#","Software Version Info"));
ussd = new ussdadaper(getActivity(),ussdlist);
listView .setAdapter(ussd); //you should setthe adapter to the listview
return rootView;
}
// TODO: Rename method, update argument and hook method into UI event
@Override
public void onAttach(Context context) {
super.onAttach(context);
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
public void onFragmentInteractionHome(Uri uri);
public void openHome(View view);
}
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p/>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
适配器
package com.buckydroid.app.droidcpu;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.List;
/**
* Created by BuckyDroid on 14-06-2016.
*/
public class ussdadaper extends BaseAdapter {
private Context ussds;
private List<ussdz> ussdlist;
public ussdadaper(Context ussds, List<ussdz> ussdlist) {
this.ussds = ussds;
this.ussdlist = ussdlist;
}
@Override
public int getCount() {
return ussdlist.size();
}
@Override
public Object getItem(int position) {
return ussdlist.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v= View.inflate(ussds,R.layout.item_listview,null);
TextView ussdcode = (TextView)v.findViewById(R.id.textView1);
TextView ussddesc = (TextView)v.findViewById(R.id.textView2);
ussdcode.setText(ussdlist.get(position).getUSSD());
ussddesc.setText(ussdlist.get(position).getDescription());
return v;
}
}
ussdz.java
package com.buckydroid.app.droidcpu;
/**
* Created by BuckyDroid on 14-06-2016.
*/
public class ussdz
{
private int id;
private String USSD;
private String Description;
public ussdz(String description, String s) {
Description = description;
this.USSD = USSD;
this.id = id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUSSD() {
return USSD;
}
public void setUSSD(String USSD) {
this.USSD = USSD;
}
public String getDescription() {
return Description;
}
public void setDescription(String description) {
Description = description;
}
}
item_listview.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:background="@android:color/transparent"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:orientation="vertical"
>
<LinearLayout
android:id="@+id/lyt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="1px"
android:gravity="left|top"
android:padding="2dp"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_weight="5"
android:layout_height="wrap_content"
android:textColor="@color/darker_grey"
android:gravity="left|top"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Name"
android:textStyle="bold"/>
<TextView
android:gravity="left|top"
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_height="wrap_content"
android:textColor="@color/darker_grey"
android:paddingLeft="5dp"
android:paddingRight="10dp"
android:text=":" />
<TextView
android:textStyle="bold"
android:gravity="left|top"
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_weight="6"
android:layout_height="wrap_content"
android:textColor="@color/darker_grey"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"/>
</LinearLayout>
</LinearLayout>
更新: 片段的xml文件
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="layout.ussd">
<!-- TODO: Update blank fragment layout -->
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_gravity="center" />
</FrameLayout>
答案 0 :(得分:1)
在你的ussdz对象中你没有设置USSD:
public ussdz(String description, String s) {
Description = description;
this.USSD = USSD; //No need to do that, it will be still null
this.id = id; //No need to do that, it will be still null
}
所以你可以在构造函数中添加SDD值:
private int id;
private String USSD;
private String description;
public ussdz(int mId, String mDescription, String mUSSD) {
description = mDescription;
this.USSD = mUSSD;
this.id = mId;
}
构建一个像:
的对象ussdz myObject = new ussdz(1, "description", "4529697");