我正在尝试使用customAdapter类添加功能更新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上的第一篇文章,所以如果我犯了任何错误(比如违反了一些潜规则),请告诉我。
答案 0 :(得分:1)
您已经声明了两个不同的集合,一个数组和另一个列表,并使用其中一个,即数组作为适配器的实际数据,另一个仅在getView中用于getItem。 有两种方法可以解决这个问题:
在customAdapter上的add方法中。而不是
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++){
或者您应该将数据作为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;
}