带标签的片段不显示列表视图

时间:2017-10-29 15:00:20

标签: android listview android-fragments

我尝试将标签片段与ListView结合起来。我通过JSON获取数据并将其放入ListView片段中,但是当我启动应用片段时,它不显示列表。

enter image description here

但是当我在片段外运行活动时,列表会出现!

enter image description here

我想知道为什么请

Testactivity.java

public class Testactivity extends AppCompatActivity {


ArrayList<articles> arrayList;
ListView lv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_testactivity);
    arrayList = new ArrayList<>();
    lv = (ListView) findViewById(R.id.ListView1);

    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            new ReadJSON().execute("http://wach.ma/mobile/home.php");
        }
    });
}

class ReadJSON extends AsyncTask<String, Integer, String> {

    @Override
    protected String doInBackground(String... params) {
        return readURL(params[0]);
    }

    @Override
    protected void onPostExecute(String content) {
        JSONObject jsonObject = null;
        try {
            jsonObject = new JSONObject(content);
        } catch (JSONException e1) {
            e1.printStackTrace();
        }
        JSONArray jsonArray = null;
        try {
            jsonArray = jsonObject.getJSONArray("articles");
        } catch (JSONException e1) {
            e1.printStackTrace();
        }
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject articlesobject = null;
            try {
                articlesobject = jsonArray.getJSONObject(i);
            } catch (JSONException e1) {
                e1.printStackTrace();
            }
            try {
                arrayList.add(new articles(
                        articlesobject.getString("picture"),
                        articlesobject.getString("title")

                ));
            } catch (JSONException e1) {
                e1.printStackTrace();
            }
            CustomListAdaper adaper = new CustomListAdaper(
                    getApplicationContext(), R.layout.custom_list_layout, 
arrayList
            );
            lv.setAdapter(adaper);
        }

    }


    private String readURL(String theURL) {
        StringBuilder content = new StringBuilder();
        try {
            URL url = new URL(theURL);
            URLConnection urlConnection = url.openConnection();
            BufferedReader bufferedReader = new BufferedReader(new 
InputStreamReader(urlConnection.getInputStream()));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                content.append(line + "\n");
            }
            bufferedReader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return content.toString();
    }
}
}

CustomListAdaper.java

public class CustomListAdaper extends ArrayAdapter<articles> {
ArrayList<articles> articles;
Context context;
int resource;
public CustomListAdaper(@NonNull Context context, @LayoutRes int resource, 
@NonNull ArrayList<articles> articles) {
    super(context, resource, articles);
    this.articles = articles;
    this.context = context;
    this.resource = resource;
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull 
ViewGroup parent) {
    if (convertView == null){
        LayoutInflater layoutInflater = (LayoutInflater) 
getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = layoutInflater.inflate(R.layout.custom_list_layout, 
null, true);
    }
    articles articles = getItem(position);
    ImageView imageView = (ImageView) convertView.findViewById(imageView2);
Picasso.with(context).load("http://wach.ma/"+articles.getPicture())
.into(imageVi
ew);
    TextView textView = (TextView) convertView.findViewById(R.id.textView);
    textView.setText((replacehtml(articles.getNom())));

    return convertView;
}
public String replacehtml(String str) {

    str = str.replaceAll("&eacute;", "é");
    return str;



}
}

Accueil.java :(我希望展示activity_testactivity.xml):

public class Accueil extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_testactivity, container, 
false);
return rootView;

}
}

activity_testactivity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
tools:context="com.example.lenovo.myapplication.Testactivity">


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/logo2" />

    <ListView
        android:id="@+id/ListView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:listitem="@layout/custom_list_layout"/>
</LinearLayout>

</RelativeLayout>

custom_list_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="350dp"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="1">

<de.hdodenhof.circleimageview.CircleImageView
    android:id="@+id/imageView2"
    android:layout_width="100dp"
    android:layout_height="120dp"
    android:layout_marginLeft="5dp"
    android:layout_marginTop="5dp"
    android:src="@mipmap/ic_launcher" />

<TextView
    android:id="@+id/textView"
    android:layout_width="250dp"
    android:layout_height="72dp"
    android:layout_marginTop="20dp"
    android:layout_weight="0.19"
    android:paddingLeft="25dp"
    android:text="TextView"
    android:textColor="@android:color/darker_gray"
    android:textSize="20sp" />


</LinearLayout>

1 个答案:

答案 0 :(得分:0)

因为您将ListView留空而没有ReadJSON任务中的任何数据。

为此,在Fragment中:找到ListView实例

lv = (ListView) findViewById(R.id.ListView1);

将班级class ReadJSON extends AsyncTask<String, Integer, String> { }复制到您的片段。

执行获取数据并显示在片段ListView内的onCreateView()的任务:

runOnUiThread(new Runnable() {
     @Override
     public void run() {
           new ReadJSON().execute("http://wach.ma/mobile/home.php");
     }
});

如果您不想复制ReadJSON任务的代码,但仍想在Activity中使用它片段:将ReadJSON带到一个单独的类,将ReadJSON的实例存储在{{{ 1}}任务并通过构造函数设置它。然后使用此本地ListView实例来显示数据。