ArrayList不在ListView中显示

时间:2018-02-01 17:20:19

标签: java android listview arraylist

我一直试图让ArrayListAdapterListView显示网址历史记录,并且在显示网页时没有显示任何数据。我正在使用的代码没有错误,并与同事讨论。我们认为这不是使用的代码。我们认为的问题是布局文件夹中的ListView自我。从MainFile访问。

我们无法找到任何帮助,请:

history.xml

<android.support.constraint.ConstraintLayout 
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:id="@+id/activity_history_layout"
android:layout_width="275dp"
android:layout_height="500dp"
android:layout_gravity="center_horizontal|center"
android:layout_margin="10dp"
android:background="@drawable/history_layout"
android:elevation="10dp">

<android.support.constraint.ConstraintLayout
    android:id="@+id/b_Toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:layout_marginEnd="1dp"
    android:layout_marginStart="1dp"
    android:layout_marginTop="3dp"
    android:background="@drawable/history_toolbar"
    app:layout_constraintBottom_toTopOf="@+id/b_HistoryList"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="History"
        android:textColor="#000"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/imageButton2"
        app:layout_constraintStart_toEndOf="@+id/b_HistoryClose"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageButton
        android:id="@+id/b_HistoryClose"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="66dp"
        android:layout_marginStart="8dp"
        android:background="@android:color/transparent"
        android:onClick="onHistoryClose"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/textView"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/close" />

    <ImageButton
        android:id="@+id/imageButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="66dp"
        android:background="@android:color/transparent"
        android:onClick="onHistoryDelete"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/textView"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/delete" />


</android.support.constraint.ConstraintLayout>

<ListView
    android:id="@+id/b_HistoryList"
    android:layout_width="270dp"
    android:layout_height="426dp"
    android:layout_marginBottom="16dp"
    android:layout_marginEnd="4dp"
    android:layout_marginStart="4dp"
    android:background="@drawable/history_listview"
    android:scrollbars="vertical"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/b_Toolbar" />
</android.support.constraint.ConstraintLayout>

ArrayAdapter.java

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public  class UrlHistoryAdapter extends BaseAdapter {

private final  Context context;

private final List<String> list = new ArrayList<>();

public UrlHistoryAdapter(Context context) {
    this.context = context;
}

public  void addItem(String item) {
    this.list.add(item);
}

@Override
public int getCount() {
    return list.size();
}

@Override
public String getItem(int position) {
    return list.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;

    if (view == null) {
        view = LayoutInflater.from(context)
                .inflate(android.R.layout.simple_list_item_1, null, false);
    }

    TextView textView = view.findViewById(android.R.id.title);
    textView.setText(getItem(position));
    return view;
}
}

Main Actvity

中使用
UrlHistoryAdapter UrlHistoryAdapter;
onCreate

之后

UrlHistoryAdapter = new UrlHistoryAdapter(this);
    b_HistoryList.setAdapter(UrlHistoryAdapter);

m_WebView.setWebViewClient(new WebViewClient(){

        String url = m_WebView.getUrl();

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            UrlHistoryAdapter.addItem(url);
            UrlHistoryAdapter.notifyDataSetChanged();

            return super.shouldOverrideUrlLoading(view, request);
        }

2 个答案:

答案 0 :(得分:0)

将你的arraylist传递到UrlHistoryAdapter下方,就像这样

class MainActivity extands Activity{
       ArrayList<String> arraylilst = new ArrayList<String>();
       Oncreate(){

       arraylilst.add("testingdatas");
       UrlHistoryAdapter = new UrlHistoryAdapter(this,arraylilst);
       b_HistoryList.setAdapter(UrlHistoryAdapter);

    }

 @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            arraylist.add(view.getUrl());
            UrlHistoryAdapter.notifyDataSetChanged();

            return super.shouldOverrideUrlLoading(view, request);
        }
}

然后你必须在你的UrlHistoryAdapter课程中获得Arraylist

public UrlHistoryAdapter(Context context, List<String> list) {
    this.context = context;
    this.list = list;
}

答案 1 :(得分:0)

首先,应在添加所有项目结束时设置适配器 所以修改你的代码,如下面的

UrlHistoryAdapter UrlHistoryAdapter = new UrlHistoryAdapter(this);

m_WebView.setWebViewClient(new WebViewClient(){

    String url = m_WebView.getUrl();

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
        UrlHistoryAdapter.addItem(url);
        UrlHistoryAdapter.notifyDataSetChanged();

        return super.shouldOverrideUrlLoading(view, request);
    }  

    b_HistoryList.setAdapter(UrlHistoryAdapter);

然后替换以下行

View view = convertView;
if (view == null) {
    view = LayoutInflater.from(context)
            .inflate(android.R.layout.simple_list_item_1, null, false);

这些行

    LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);

    convertView = layoutInflater.inflate(R.layout.simple_list_item_1,null);

您还在名为simple_list_item_1的布局文件夹中创建了一个xml文件,其中至少包含一个标题为id的textVied。