listView动态添加项目

我用ListView来动态添加项目,但是有一个关于不是Smooth添加的问题。 有TextView的和按钮,在我的listActivity,Iwant按下按钮,然后TextView的文本可以自动添加到ListView,但我按下按钮,它DONOT工作,除非以后我进入内容,按‘OK’键,再按下按钮,TextView's文字可以自动添加到ListView。我不知道为什么。如果我连续按下的按钮,为3次,然后按“确定”键,内容listView动态添加项目

自动添加列表

的看法,但3次。

public class DynamicListItems extends ListActivity { 

private static final String ITEM_KEY = "key";

ArrayList<HashMap<String, String>> list= new ArrayList<HashMap<String, String>>();

private SimpleAdapter adapter;

private EditText newValue;@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.dynamic_list);

newValue = (EditText) findViewById(R.id.new_value_field);

setListAdapter(new SimpleAdapter(this, list, R.layout.row, new String[] { ITEM_KEY }, new int[] { R.id.list_value }));

((ImageButton) findViewById(R.id.button)).setOnClickListener(getBtnClickListener());

}

private OnClickListener getBtnClickListener() {

return new OnClickListener() {

public void onClick(View view) {

try {

HashMap<String, String> item = new HashMap<String, String>();

item.put(ITEM_KEY, newValue.getText().toString());

list.add(item);

adapter.notifyDataSetChanged();

} catch (NullPointerException e) {

Log.i("[Dynamic Items]", "Tried to add null value");

}

}

};

}}

如何动态删除项目?

  • dynamic_list.xml只包含ListView控件,按钮,TextView的
  • row.xml包含TextView

回答:

notifyDataSetChanged()方法用于更新适配器。

在这里,我发布了一个工作答案的步骤。

首先main.xml中文件:

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

<LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_alignParentBottom="true"

android:layout_centerHorizontal="true"

android:id="@+id/input">

<EditText

android:id="@+id/editText_input"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:ems="10" >

<requestFocus />

</EditText>

<Button

android:id="@+id/button_add"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="0"

android:text="Add" />

</LinearLayout>

<LinearLayout

android:layout_width="wrap_content"

android:layout_height="match_parent"

android:layout_above="@+id/input"

android:layout_alignParentTop="true"

android:layout_centerHorizontal="true"

android:orientation="vertical" >

<ListView

android:id="@+id/listView_items"

android:layout_width="match_parent"

android:layout_height="wrap_content" >

</ListView>

</LinearLayout>

</RelativeLayout>

这里MainActivity.java:

import java.util.ArrayList; 

import android.app.Activity;

import android.os.Bundle;

import android.view.KeyEvent;

import android.view.View;

import android.widget.ArrayAdapter;

import android.widget.Button;

import android.widget.EditText;

import android.widget.ListView;

public class MainActivity extends Activity {

private EditText etInput;

private Button btnAdd;

private ListView lvItem;

private ArrayList<String> itemArrey;

private ArrayAdapter<String> itemAdapter;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.dyanamic_list);

setUpView();

}

private void setUpView() {

etInput = (EditText)this.findViewById(R.id.editText_input);

btnAdd = (Button)this.findViewById(R.id.button_add);

lvItem = (ListView)this.findViewById(R.id.listView_items);

itemArrey = new ArrayList<String>();

itemArrey.clear();

itemAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,itemArrey);

lvItem.setAdapter(itemAdapter);

btnAdd.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

addItemList();

}

});

etInput.setOnKeyListener(new View.OnKeyListener() {

public boolean onKey(View v, int keyCode, KeyEvent event) {

if (keyCode == KeyEvent.KEYCODE_ENTER) {

addItemList();

}

return true;

}

});

}

protected void addItemList() {

if (isInputValid(etInput)) {

itemArrey.add(0,etInput.getText().toString());

etInput.setText("");

itemAdapter.notifyDataSetChanged();

}

}

protected boolean isInputValid(EditText etInput2) {

if (etInput2.getText().toString().trim().length()<1) {

etInput2.setError("Please Enter Item");

return false;

} else {

return true;

}

}

}

回答:

是对ListActivityArrayAdapter类的你getBtnClickListener方法的一部分?

对我来说,当我从ListActivity类更新,我用这个代码...

// code to add a Contact to my database 

// code to add a Contact to the list that

// that is used by the ListView

setListAdapter(adapter);

getListView().setTextFilterEnabled(true);

当我从一个方法更新ArrayAdapter类中,我使用此代码...

// from a LongPress on a ListView item 

convertView.setOnLongClickListener(new OnLongClickListener(){

@Override

public boolean onLongClick(View view) {

view.performHapticFeedback(0, View.HAPTIC_FEEDBACK_ENABLED);

// code to remove a Contact name from my database

// code to remove that Contact name from my list

// that is used by the ListView

ContactsAdapter.this.notifyDataSetChanged();

return true;

});

回答:

我使用一个线程在后台添加更多的数据,以我的列表,然后notifydatasetchange ,它工作成功

这里是完整的代码:http://code.google.com/p/dynamic-listview/source/checkout

回答:

是!,方法notifyDataSetChanged()在一个ArrayAdapter应用你缩绒他面前,对我来说是解决方案。从Firebase阅读。

对象

private DatabaseReference myRef; 

ArrayList<String> lista;

ArrayAdapter<String> adapter;

的OnCreate

FirebaseDatabase database = FirebaseDatabase.getInstance(); 

myRef = database.getReference("chat");

//GETTIN MY DATA TO SHOW IN CHAT

lista = new ArrayList<String>();

的onResume

myRef.addValueEventListener(new ValueEventListener() { 

@Override

public void onDataChange(DataSnapshot dataSnapshot) {

for (DataSnapshot postSnappy : dataSnapshot.getChildren()){

for (DataSnapshot rePostSnappy : postSnappy.getChildren()){

// Defined Array values to show in ListView

lista.add(new String(rePostSnappy.getValue().toString()));

adapter.notifyDataSetChanged();//Notyfing adapter that will goes to change

}

}

}

@Override

public void onCancelled(DatabaseError databaseError) {

}

});

adapter = new ArrayAdapter<String>(this,

android.R.layout.simple_list_item_1, lista);

ListView listVista = (ListView) findViewById(R.id.list);

listVista.setAdapter(adapter);

以上是 listView动态添加项目 的全部内容, 来源链接: utcz.com/qa/265832.html

回到顶部