【问题标题】:fragment with tabs don't show listviews带有标签的片段不显示列表视图
【发布时间】:2017-10-29 15:00:19
【问题描述】:

我尝试将选项卡式片段与ListView 结合起来。我通过 JSON 获取数据并将其放在片段中的 ListView 中,但是当我启动应用程序片段时不显示列表。

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

我想知道为什么

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>

【问题讨论】:

  • 因为你在片段中什么也没做,只是返回一个空的rootview
  • 如何在片段中调用我的列表视图?
  • 我建议研究 Fragments 以及它们是如何工作的,然后我很乐意在您有一些实际实现时帮助您解决问题
  • 我知道它们是如何工作的,但我在我的代码中找不到错误。请帮忙
  • 我说由于某种原因,在活动中查看您正在获取数据,初始化列表视图,这意味着您需要按照片段说明在片段中执行相同操作

标签: android listview android-fragments


【解决方案1】:

因为您将 ListView 留空,没有来自 ReadJSON 任务的任何数据。

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

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

class ReadJSON extends AsyncTask&lt;String, Integer, String&gt; { } 类复制到您的 Fragment。

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

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

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

【讨论】:

    猜你喜欢
    • 2015-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-29
    • 1970-01-01
    • 1970-01-01
    • 2016-03-11
    相关资源
    最近更新 更多