在用户输入的列表视图中动态显示项目
Dynamically displaying items in List View that is entered by User
我有两个编辑文本,用户在其中输入他们的名字和姓氏。
我想在列表视图中显示下面的名字和姓氏。
因此,如果用户第一次输入姓名和姓氏,它应该显示在列表视图的第 1 行,而对于第二次输入,它应该显示在列表视图的第 2 行。
我的问题是,当我输入新条目时,它会替换列表视图第一行中的文本,而不会显示在列表视图的第二行或下一行中。
请看我的代码。
如果你能帮助我就太好了。
下面是我的Java代码
''' package com.nitin.inflaterproject;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
ListView list;
EditText editText1, editText2;
LinearLayout linearLayoutMain;
MyAdapter adapter;
int counter;
public void addData(View view){
String text1 = editText1.getText().toString();
String text2 = editText2.getText().toString();
if(text1.equals("") || text2.equals("")){
Toast.makeText(this, "Please enter required field", Toast.LENGTH_SHORT).show();
}else{
counter++;
Toast.makeText(this, "Counter value is " + counter, Toast.LENGTH_SHORT).show();
adapter = new MyAdapter(this, text1, text2,counter);
list.setAdapter(adapter);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayoutMain = (LinearLayout)findViewById(R.id.linearLayoutMain);
editText1 = (EditText)findViewById(R.id.editText1);
editText2 = (EditText)findViewById(R.id.editText2);
list = (ListView)findViewById(R.id.listViewMain);
counter = 0;
}
}'''
And then Base Adapter Code
'''package com.nitin.inflaterproject;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
class MyList{
String text1;
String text2;
MyList(String text1, String text2){
this.text1 = text1;
this.text2 = text2;
}
}
class MyAdapter extends BaseAdapter {
ArrayList<MyList> list;
private Context context;
private int count;
MyAdapter(Context c, String text1, String text2, int counter){
count = counter - 1;
context = c;
list = new ArrayList<MyList>();
String[] textstring1 = new String[counter];
String[] textstring2 = new String[counter];
textstring1[count] = text1;
textstring2[count] = text2;
list.add(new MyList(textstring1[count], textstring2[count]));
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if(row==null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.myadapter, parent, false);
}
TextView text1 = (TextView)row.findViewById(R.id.textView1);
TextView text2 = (TextView)row.findViewById(R.id.textView2);
MyList temp = list.get(position);
text1.setText(temp.text1);
text2.setText(temp.text2);
return row;
}
}'''
my XML Code
Main Activity
'''<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:ems="10"
android:inputType="textPersonName"
android:hint="@string/enter_name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:hint="@string/enter_surname"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText1" />
<Button
android:id="@+id/buttonAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="68dp"
android:onClick="addData"
android:padding="10dp"
android:text="@string/add"
android:textSize="20sp"
app:layout_constraintStart_toEndOf="@+id/editText1"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:id="@+id/linearLayoutMain"
android:layout_width="311dp"
android:layout_height="468dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:background="#77BBCC00"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText2">
<ListView
android:id="@+id/listViewMain"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
'''
XML code for Layout that needs to be inflated
'''<?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:padding="16dp"
android:id="@+id/linearLayout">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="24sp"
android:background="#99BBCC00"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="24sp"
android:background="#BBCC00"/>
</LinearLayout>'''
I solved it by changing the code:
'''package com.nitin.inflaterproject;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ListView list;
EditText editText1, editText2;
LinearLayout linearLayoutMain;
MyAdapter adapter;
int counter;
int count = 0;
ArrayList<MyList> list1;
public void addData(View view){
String text1 = editText1.getText().toString();
String text2 = editText2.getText().toString();
if(text1.equals("") || text2.equals("")){
Toast.makeText(this, "Please enter required field", Toast.LENGTH_SHORT).show();
}else{
//counter++;
//Toast.makeText(this, "Counter value is " + counter, Toast.LENGTH_SHORT).show();
// adapter = new MyAdapter(this, text1, text2,counter);
//list.setAdapter(adapter);
counter++;
count = counter - 1;
String[] textstring1 = new String[counter];
String[] textstring2 = new String[counter];
textstring1[count] = text1;
textstring2[count] = text2;
list1.add(new MyList(textstring1[count], textstring2[count]));
adapter = new MyAdapter(this, list1);
list.setAdapter(adapter);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list1 = new ArrayList<MyList>();
linearLayoutMain = (LinearLayout)findViewById(R.id.linearLayoutMain);
editText1 = (EditText)findViewById(R.id.editText1);
editText2 = (EditText)findViewById(R.id.editText2);
list = (ListView)findViewById(R.id.listViewMain);
counter = 0;
}
}
class MyList{
String text1;
String text2;
MyList(String text1, String text2){
this.text1 = text1;
this.text2 = text2;
}
}'''
'''package com.nitin.inflaterproject;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
class MyAdapter extends BaseAdapter {
ArrayList<MyList> list;
private Context context;
MyAdapter(Context c, ArrayList<MyList> list1){
context = c;
list = new ArrayList<MyList>(list1);
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if(row==null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.myadapter, parent, false);
}
TextView text1 = (TextView)row.findViewById(R.id.textView1);
TextView text2 = (TextView)row.findViewById(R.id.textView2);
MyList temp = list.get(position);
text1.setText(temp.text1);
text2.setText(temp.text2);
return row;
}
}'''
我有两个编辑文本,用户在其中输入他们的名字和姓氏。 我想在列表视图中显示下面的名字和姓氏。 因此,如果用户第一次输入姓名和姓氏,它应该显示在列表视图的第 1 行,而对于第二次输入,它应该显示在列表视图的第 2 行。 我的问题是,当我输入新条目时,它会替换列表视图第一行中的文本,而不会显示在列表视图的第二行或下一行中。
请看我的代码。 如果你能帮助我就太好了。
下面是我的Java代码
''' package com.nitin.inflaterproject;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
ListView list;
EditText editText1, editText2;
LinearLayout linearLayoutMain;
MyAdapter adapter;
int counter;
public void addData(View view){
String text1 = editText1.getText().toString();
String text2 = editText2.getText().toString();
if(text1.equals("") || text2.equals("")){
Toast.makeText(this, "Please enter required field", Toast.LENGTH_SHORT).show();
}else{
counter++;
Toast.makeText(this, "Counter value is " + counter, Toast.LENGTH_SHORT).show();
adapter = new MyAdapter(this, text1, text2,counter);
list.setAdapter(adapter);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayoutMain = (LinearLayout)findViewById(R.id.linearLayoutMain);
editText1 = (EditText)findViewById(R.id.editText1);
editText2 = (EditText)findViewById(R.id.editText2);
list = (ListView)findViewById(R.id.listViewMain);
counter = 0;
}
}'''
And then Base Adapter Code
'''package com.nitin.inflaterproject;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
class MyList{
String text1;
String text2;
MyList(String text1, String text2){
this.text1 = text1;
this.text2 = text2;
}
}
class MyAdapter extends BaseAdapter {
ArrayList<MyList> list;
private Context context;
private int count;
MyAdapter(Context c, String text1, String text2, int counter){
count = counter - 1;
context = c;
list = new ArrayList<MyList>();
String[] textstring1 = new String[counter];
String[] textstring2 = new String[counter];
textstring1[count] = text1;
textstring2[count] = text2;
list.add(new MyList(textstring1[count], textstring2[count]));
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if(row==null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.myadapter, parent, false);
}
TextView text1 = (TextView)row.findViewById(R.id.textView1);
TextView text2 = (TextView)row.findViewById(R.id.textView2);
MyList temp = list.get(position);
text1.setText(temp.text1);
text2.setText(temp.text2);
return row;
}
}'''
my XML Code
Main Activity
'''<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:ems="10"
android:inputType="textPersonName"
android:hint="@string/enter_name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:hint="@string/enter_surname"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText1" />
<Button
android:id="@+id/buttonAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="68dp"
android:onClick="addData"
android:padding="10dp"
android:text="@string/add"
android:textSize="20sp"
app:layout_constraintStart_toEndOf="@+id/editText1"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:id="@+id/linearLayoutMain"
android:layout_width="311dp"
android:layout_height="468dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:background="#77BBCC00"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText2">
<ListView
android:id="@+id/listViewMain"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
'''
XML code for Layout that needs to be inflated
'''<?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:padding="16dp"
android:id="@+id/linearLayout">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="24sp"
android:background="#99BBCC00"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="24sp"
android:background="#BBCC00"/>
</LinearLayout>'''
I solved it by changing the code:
'''package com.nitin.inflaterproject;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ListView list;
EditText editText1, editText2;
LinearLayout linearLayoutMain;
MyAdapter adapter;
int counter;
int count = 0;
ArrayList<MyList> list1;
public void addData(View view){
String text1 = editText1.getText().toString();
String text2 = editText2.getText().toString();
if(text1.equals("") || text2.equals("")){
Toast.makeText(this, "Please enter required field", Toast.LENGTH_SHORT).show();
}else{
//counter++;
//Toast.makeText(this, "Counter value is " + counter, Toast.LENGTH_SHORT).show();
// adapter = new MyAdapter(this, text1, text2,counter);
//list.setAdapter(adapter);
counter++;
count = counter - 1;
String[] textstring1 = new String[counter];
String[] textstring2 = new String[counter];
textstring1[count] = text1;
textstring2[count] = text2;
list1.add(new MyList(textstring1[count], textstring2[count]));
adapter = new MyAdapter(this, list1);
list.setAdapter(adapter);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list1 = new ArrayList<MyList>();
linearLayoutMain = (LinearLayout)findViewById(R.id.linearLayoutMain);
editText1 = (EditText)findViewById(R.id.editText1);
editText2 = (EditText)findViewById(R.id.editText2);
list = (ListView)findViewById(R.id.listViewMain);
counter = 0;
}
}
class MyList{
String text1;
String text2;
MyList(String text1, String text2){
this.text1 = text1;
this.text2 = text2;
}
}'''
'''package com.nitin.inflaterproject;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
class MyAdapter extends BaseAdapter {
ArrayList<MyList> list;
private Context context;
MyAdapter(Context c, ArrayList<MyList> list1){
context = c;
list = new ArrayList<MyList>(list1);
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if(row==null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.myadapter, parent, false);
}
TextView text1 = (TextView)row.findViewById(R.id.textView1);
TextView text2 = (TextView)row.findViewById(R.id.textView2);
MyList temp = list.get(position);
text1.setText(temp.text1);
text2.setText(temp.text2);
return row;
}
}'''