簡體   English   中英

notifyDataSetChanged不更新listView

[英]notifyDataSetChanged not updating the listView

我正在嘗試使用customAdapter類的add函數更新listView。 但是我遇到了奇怪的行為。 第一次嘗試添加元素是可行的,但此后,listView不會得到更新。 我看過很多論壇(這是我到目前為止取得的成就),但這是我無法取得進步的地方。

這是代碼:HomeScreen.java

package com.apress.gerber.mathematicalcalculator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.content.Intent;


public class HomeScreen extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_screen);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_home_screen, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void onClickLogTable(View view){
        Intent logTableLauncherInternt = new Intent(this, LogTable.class);
        startActivity(logTableLauncherInternt);
    }
}

LogTable.java

package com.apress.gerber.mathematicalcalculator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.List;


public class LogTable extends AppCompatActivity {

    public customAdapter c;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_log_table);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    }

    public void onClickCalculate(View view){
        EditText logBaseValue = (EditText) findViewById(R.id.logBaseValue);

        if(logBaseValue.getText().toString().length()<=0) {
            Toast.makeText(LogTable.this, "Input Something", Toast.LENGTH_LONG).show();
            return;
        }

        int logBase = Integer.parseInt(logBaseValue.getText().toString());
        logBaseValue.setText(String.format("%d",logBase));

        if(logBase != logBase)
            Toast.makeText(LogTable.this, "Invalid Input", Toast.LENGTH_LONG).show();



        String[] numbers = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16"};
        c = new customAdapter(this, numbers, logBase);
        ListAdapter logTableAdapter = c;

        ListView logTable = (ListView) findViewById(R.id.logTable);
        logTable.setAdapter(logTableAdapter);


        logTable.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if(firstVisibleItem+visibleItemCount>=(totalItemCount-3)){
                    Integer asd = (int) Math.ceil(Math.random()*100);
                    Log.d("RedMango", "" + asd);
                    c.add(Integer.toString(asd));
                }
            }
        });

    }

}

customAdapter.java

package com.apress.gerber.mathematicalcalculator;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by as on 10/10/15.
 */
class customAdapter extends ArrayAdapter<String>{

    private static List<String> ListValues = new ArrayList<String>();
    private static List<String> LogValues = new ArrayList<String>();
    private Integer BaseValue;

    public customAdapter(Context context, String[] listValues, int baseValue) {
        super(context, R.layout.infiniteloglayout, listValues);
        for(int i=0; i<listValues.length-1; i++){
            ListValues.add(listValues[i]);
            LogValues.add(Double.toString(Math.log(Double.parseDouble(listValues[i])) / Math.log(baseValue)));
        }
        BaseValue = baseValue;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater logInflater = LayoutInflater.from(getContext());
        View infiniteloglayout = logInflater.inflate(R.layout.infiniteloglayout, parent, false);

        TextView value = (TextView) infiniteloglayout.findViewById(R.id.value);
        TextView logValue = (TextView) infiniteloglayout.findViewById(R.id.logValue);

        value.setText(ListValues.get(position));
        logValue.setText(LogValues.get(position));
        return infiniteloglayout;

    }

    public void add(String value) {
        ListValues.add(value);
        LogValues.add(Double.toString(Math.log(Double.parseDouble(value)) / Math.log(BaseValue)));
        Log.d("RedMango", "ListValues " + ListValues.size());
        this.notifyDataSetChanged();
    }
}

問題是在調用onScrollListner之后,第一個隨機元素被添加到屏幕上的列表中。 但是,此后,屏幕上沒有添加任何列表。 我嘗試記錄數據,我可以確認功能正在運行,正在將數據添加到適配器的列表中,但是並沒有更新數據(第一項除外)。 誰能指出我犯了一個錯誤。 我還嘗試將notifyDataSetChanged移出類,但這並沒有改變行為。

注意:這是我關於stackoverflow的第一篇文章,因此,如果我犯了任何錯誤(例如違反了一些潛規則),請告訴我。

您已經聲明了兩個不同的集合,一個數組和另一個列表,並使用了其中的一個,即數組作為適配器的實際數據,另一個僅在getView中用於getItem。 有兩種方法可以解決此問題:

  1. 在您的customAdapter方法上。 代替

     public void add(String value) { ListValues.add(value); 

    你應該使用

      public void add(String value) { super.add(value); 

    從自定義適配器中刪除ListValues變量,並在getView中使用

      value.setText(getItem(position)); 

    代替。 並且您正在使用for(int i=0; i<listValues.length-1; i++){您應該將其更改為for(int i=0; i<listValues.length; i++){

  2. 或者,您應該改為通過活動將數據作為arraylist傳遞。 示例:在您的活動中,將適配器更改為:

     String[] numbers = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16"}; ArrayList<String> arrayListValues = new ArrayList<>(Arrays.asList(numbers)); c = new customAdapter(this, arrayListValues, logBase); 

    現在將適配器更改為:

     private static ArrayList<String> ListValues; private static List<String> LogValues = new ArrayList<String>(); private Integer BaseValue; public customAdapter(Context context, ArrayList<String> listValues, int baseValue) { super(context, R.layout.infiniteloglayout, listValues); this.ListValues = listValues; for (int i = 0; i < listValues.size(); i++) { LogValues.add(Double.toString(Math.log(Double.parseDouble(listValues[i])) / Math.log(baseValue))); } BaseValue = baseValue; } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM