有关自定义列表视图的问题

huangapple go评论71阅读模式
英文:

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&lt;String&gt; 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&lt;&gt;();
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()&gt;0){
arrayList.add(addItem);
input.setText(&quot;&quot;);
}
else {
Toast.makeText(getApplicationContext(),&quot;Please Enter ...&quot;, 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&lt;String&gt; list) {
activity = a;
arrayList = list;
}
}
}

<h2>This is my main_activity.xml</h2>

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;LinearLayout
xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:id=&quot;@+id/activity_main&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:orientation=&quot;vertical&quot;
tools:context=&quot;.MainActivity&quot;&gt;
&lt;RelativeLayout
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;&gt;
&lt;EditText
android:id=&quot;@+id/input&quot;
android:layout_width=&quot;0dp&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_toLeftOf=&quot;@id/addBtn&quot;
android:layout_alignParentLeft=&quot;true&quot;/&gt;
&lt;Button
android:id=&quot;@+id/addBtn&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_alignParentEnd=&quot;true&quot;
android:layout_alignParentRight=&quot;true&quot;
android:layout_marginEnd=&quot;6dp&quot;
android:layout_marginRight=&quot;6dp&quot;
android:text=&quot;Add&quot; /&gt;
&lt;/RelativeLayout&gt;
&lt;ListView
android:id=&quot;@+id/list_view&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot; /&gt;
&lt;/LinearLayout&gt;

<h2>And my item.xml</h2>

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:orientation=&quot;horizontal&quot;
android:padding=&quot;16dp&quot;&gt;
&lt;TextView
android:id=&quot;@+id/tv_name&quot;
android:layout_width=&quot;329dp&quot;
android:layout_height=&quot;wrap_content&quot; /&gt;
&lt;Button
android:id=&quot;@+id/rmvButton&quot;
android:layout_width=&quot;48dp&quot;
android:layout_height=&quot;48dp&quot;
android:background=&quot;@android:color/holo_orange_light&quot;
android:text=&quot;X&quot; /&gt;
&lt;/LinearLayout&gt;

答案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()&gt;0){
arrayList.add(addItem);
myAdapter.notifyItemInserted(arrayList.size()-1);
input.setText(&quot;&quot;);
}

答案2

得分: 1

当您首次使用arrayList = new ArrayList&lt;&gt;();定义您的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() &gt; 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&lt;&gt;();, 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()&gt;0){
arrayList.add(addItem);
//call notifyDataSetChanged()
mAdapter.notifyDataSetChanged();
input.setText(&quot;&quot;);
}
else {
Toast.makeText(getApplicationContext(),&quot;Please Enter ...&quot;, 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()&gt;0){
arrayList.add(addItem);
input.setText(&quot;&quot;);
mAdapter.notifyDataSetChanged();
}
else {
Toast.makeText(getApplicationContext(),&quot;Please Enter ...&quot;, 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()&gt;0){
mAdapter.add(additem);
input.setText(&quot;&quot;);
}
else {
Toast.makeText(getApplicationContext(),&quot;Please Enter ...&quot;, Toast.LENGTH_SHORT).show();
}
}
});

When you use mAdapter.add() it will automatically notify that the dataset has been changed.

huangapple
  • 本文由 发表于 2020年8月10日 11:10:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/63333625.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定