英文:
Trouble with custom listview
问题
以下是翻译好的代码部分:
package com.example.myapplication;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private ListView listView;
public ArrayList<String> arrayList;
private MyAdapter myAdapter;
private Button addButton;
private Activity activity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
arrayList = new ArrayList<>();
listView = (ListView) findViewById(R.id.list_view);
addButton = (Button) findViewById(R.id.addBtn);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText input = findViewById(R.id.input);
String addItem = input.getText().toString();
if(addItem.length()>0){
arrayList.add(addItem);
input.setText("");
}
else {
Toast.makeText(getApplicationContext(),"Please Enter ...", Toast.LENGTH_SHORT).show();
}
}
});
myAdapter = new MyAdapter(this, arrayList);
listView.setAdapter(myAdapter);
}
public class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
return arrayList.size();
}
@Override
public Object getItem(int position) {
return arrayList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = activity.getLayoutInflater();
View view = convertView;
view = inflater.inflate(R.layout.item, null);
TextView textView = (TextView) view.findViewById(R.id.tv_name);
Button rmvButton = (Button) view.findViewById(R.id.rmvButton);
textView.setText(arrayList.get(position));
rmvButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
arrayList.remove(position);
notifyDataSetChanged();
}
});
return view;
}
public MyAdapter(Activity a, ArrayList<String> list) {
activity = a;
arrayList = list;
}
}
}
<!-- main_activity.xml -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/input"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/addBtn"
android:layout_alignParentLeft="true"/>
<Button
android:id="@+id/addBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginEnd="6dp"
android:layout_marginRight="6dp"
android:text="Add" />
</RelativeLayout>
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<!-- item.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="16dp">
<TextView
android:id="@+id/tv_name"
android:layout_width="329dp"
android:layout_height="wrap_content" />
<Button
android:id="@+id/rmvButton"
android:layout_width="48dp"
android:layout_height="48dp"
android:background="@android:color/holo_orange_light"
android:text="X" />
</LinearLayout>
注意:我已经移除了问题描述和链接。如果您需要进一步的帮助或解释,请随时提问。
英文:
I'm using a listview to display an item with a textview and a button. If I click the button, an item will be removed. An item is added by input of EditText. Here is my code. My problem is when I run this app, I can't add an new item. Problem
<h2>This is my MainActivity</h2>
package com.example.myapplication;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private ListView listView;
public ArrayList<String> arrayList;
private MyAdapter myAdapter;
private Button addButton;
private Activity activity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
arrayList = new ArrayList<>();
listView = (ListView) findViewById(R.id.list_view);
addButton = (Button) findViewById(R.id.addBtn);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText input = findViewById(R.id.input);
String addItem = input.getText().toString();
if(addItem.length()>0){
arrayList.add(addItem);
input.setText("");
}
else {
Toast.makeText(getApplicationContext(),"Please Enter ...", Toast.LENGTH_SHORT).show();
}
}
});
myAdapter = new MyAdapter(this, arrayList);
listView.setAdapter(myAdapter);
}
public class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
return arrayList.size();
}
@Override
public Object getItem(int position) {
return arrayList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = activity.getLayoutInflater();
View view = convertView;
view = inflater.inflate(R.layout.item, null);
TextView textView = (TextView) view.findViewById(R.id.tv_name);
Button rmvButton = (Button) view.findViewById(R.id.rmvButton);
textView.setText(arrayList.get(position));
rmvButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
arrayList.remove(position);
notifyDataSetChanged();
}
});
return view;
}
public MyAdapter(Activity a, ArrayList<String> list) {
activity = a;
arrayList = list;
}
}
}
<h2>This is my main_activity.xml</h2>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/input"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/addBtn"
android:layout_alignParentLeft="true"/>
<Button
android:id="@+id/addBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginEnd="6dp"
android:layout_marginRight="6dp"
android:text="Add" />
</RelativeLayout>
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<h2>And my item.xml</h2>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="16dp">
<TextView
android:id="@+id/tv_name"
android:layout_width="329dp"
android:layout_height="wrap_content" />
<Button
android:id="@+id/rmvButton"
android:layout_width="48dp"
android:layout_height="48dp"
android:background="@android:color/holo_orange_light"
android:text="X" />
</LinearLayout>
答案1
得分: 1
如果你添加了新项目,需要通知你的适配器。
if (addItem.length() > 0) {
arrayList.add(addItem);
myAdapter.notifyItemInserted(arrayList.size() - 1);
input.setText("");
}
英文:
You have to notify your adapter when you add new item
if(addItem.length()>0){
arrayList.add(addItem);
myAdapter.notifyItemInserted(arrayList.size()-1);
input.setText("");
}
答案2
得分: 1
当您首次使用arrayList = new ArrayList<>();
定义您的ArrayList
时,您定义了一个空的数组列表,然后设置了适配器。
下次当您更改列表的内容时,应在适配器上调用notifyDataSetChanged();
。
因此,在您的onClickListener()
中,您应该这样做:
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText input = findViewById(R.id.input);
String addItem = input.getText().toString();
if (addItem.length() > 0) {
arrayList.add(addItem);
// 调用 notifyDataSetChanged()
mAdapter.notifyDataSetChanged();
input.setText("");
} else {
Toast.makeText(getApplicationContext(), "请输入...", Toast.LENGTH_SHORT).show();
}
}
});
英文:
When you first define your ArrayList
using arrayList = new ArrayList<>();
, you define a empty array list and then set the adapter.
The next time when you change the contents of the list you should call notifyDataSetChanged();
on the adapter.
so in your onClickListener()
you would do
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText input = findViewById(R.id.input);
String addItem = input.getText().toString();
if(addItem.length()>0){
arrayList.add(addItem);
//call notifyDataSetChanged()
mAdapter.notifyDataSetChanged();
input.setText("");
}
else {
Toast.makeText(getApplicationContext(),"Please Enter ...", Toast.LENGTH_SHORT).show();
}
}
});
答案3
得分: 1
你需要通知适配器有数据添加到列表中,为此,你应该像这样调用 notifyDataSetChanged()
方法。
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText input = findViewById(R.id.input);
String addItem = input.getText().toString();
if (addItem.length() > 0) {
arrayList.add(addItem);
input.setText("");
mAdapter.notifyDataSetChanged();
} else {
Toast.makeText(getApplicationContext(), "请输入...", Toast.LENGTH_SHORT).show();
}
}
});
另一种方法是直接将数据添加到适配器中。
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText input = findViewById(R.id.input);
String addItem = input.getText().toString();
if (addItem.length() > 0) {
mAdapter.add(addItem);
input.setText("");
} else {
Toast.makeText(getApplicationContext(), "请输入...", Toast.LENGTH_SHORT).show();
}
}
});
当你使用 mAdapter.add()
时,它会自动通知数据集已更改。
英文:
You need to notify the adapter about the data being added to the list and to do that you should call notifyDataSetChanged() method like this.
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText input = findViewById(R.id.input);
String addItem = input.getText().toString();
if(addItem.length()>0){
arrayList.add(addItem);
input.setText("");
mAdapter.notifyDataSetChanged();
}
else {
Toast.makeText(getApplicationContext(),"Please Enter ...", Toast.LENGTH_SHORT).show();
}
}
});
The alternate method for this can be to add data directly to the adapter.
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText input = findViewById(R.id.input);
String addItem = input.getText().toString();
if(addItem.length()>0){
mAdapter.add(additem);
input.setText("");
}
else {
Toast.makeText(getApplicationContext(),"Please Enter ...", Toast.LENGTH_SHORT).show();
}
}
});
When you use mAdapter.add() it will automatically notify that the dataset has been changed.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论