从 ListView 中检索 CheckBox 的值并分配给数组列表中的对象
Retrieve value of CheckBox from ListView and assign to Object in array list
我有一个当前在 ListView 中显示姓名列表的应用程序。 ListView 中的每个项目都由一个 CheckBox 和一个 EditText 组成。
名称列表由 'Prospects' 个对象的 ArrayList 提供
当用户选择 CheckBox 时,我想 return 此复选框的状态,并在 ArrayList 中的相关位置为它分配 Propsects 对象,以便下次应用程序可以保存它以供使用已打开(这将使用 CSV 完成)。
我正在努力寻找正确的侦听器来执行此操作,因为这是我第一次尝试 Android 编程。我一直无法找到任何与我想要实现的目标相匹配的例子,或者如果我有我不理解它们!。
如有任何帮助,我们将不胜感激!
我的代码如下:
package com.veetox.networkmarketingmanager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import com.veetox.networkmarketingmanager.data.Prospects;
import com.veetox.networkmarketingmanager.helper.FileHelper;
import com.veetox.networkmarketingmanager.helper.ProspectListAdapter;
import com.veetox.networkmarketingmanager.helper.ProspectViewHolder;
import java.io.File;
import java.lang.reflect.Field;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity
{
public static ArrayList<Prospects> prospectList = new ArrayList<>();
Resources res;
FileHelper fileHelper = new FileHelper(this);
ListView prospectsListEntry;
CheckBox prospectsCheckBox;
EditText prospectsEditText;
ProspectListAdapter pla;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prospectsCheckBox = (CheckBox) findViewById(R.id.pr_checkBox);
prospectsEditText = (EditText) findViewById(R.id.pr_name);
res = getResources();
createProspectList();
prospectsListEntry.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View item, int position, long id)
{
//Create new Prospect cloning Prospect at row in ItemList that was clicked
Prospects prospect = pla.getItem(position);
//Change the state of contacted from true to false or vice versa
prospect.toggleContacted();
//Create a ProspectViewHolder to hold the view for the row clicked
ProspectViewHolder viewHolder = (ProspectViewHolder) item.getTag();
viewHolder.getCheckBox().setChecked(prospect.isChecked());
prospectList.get(position) ;
pla.notifyDataSetChanged();
}
});
}
public void testProspects(View view)
{
prospectList = fileHelper.testProspects();
pla.notifyDataSetChanged();
}
public void createProspectList()
{
fileHelper = new FileHelper(this);
//Comment before testing
//prospectList = fileHelper.loadProspects();
//Uncomment for testing
prospectList = fileHelper.testProspects();
//Create the list
pla = new ProspectListAdapter(this, R.layout.prospects_list_layout, prospectList);
prospectsListEntry = (ListView) findViewById(R.id.prospects_list);
prospectsListEntry.setAdapter(pla);
}
}
package com.veetox.networkmarketingmanager.helper;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import com.veetox.networkmarketingmanager.MainActivity;
import com.veetox.networkmarketingmanager.R;
import com.veetox.networkmarketingmanager.data.Prospects;
import java.util.ArrayList;
/**
* Created by Matt on 29/09/2016.
*/
public class ProspectListAdapter extends ArrayAdapter<Prospects>
{
private Context context;
private int layoutResourceId;
private ArrayList<Prospects> prospect;
public ProspectListAdapter(Context context, int layoutResourceId, ArrayList<Prospects> prospect)
{
super(context, layoutResourceId, prospect);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.prospect = prospect;
}
public int getCount()
{
return prospect.size();
}
public Prospects getItem(int position)
{
return prospect.get(position);
}
public long getItemId(int position)
{
return position;
}
@Override
@NonNull
public View getView(int position, View convertView, @NonNull ViewGroup parent)
{
View v = convertView;
if (v == null)
{
LayoutInflater vi;
vi = LayoutInflater.from(context);
v = vi.inflate(layoutResourceId, parent, false);
}
Prospects prospects = prospect.get(position);
if (prospects != null)
{
TextView prname = (TextView) v.findViewById(R.id.pr_name);
CheckBox complete = (CheckBox) v.findViewById(R.id.pr_checkBox);
if (prname != null)
{
prname.setText(prospects.getName());
}
if (complete != null)
{
complete.setChecked(prospects.isChecked());
}
}
return v;
}
}
package com.veetox.networkmarketingmanager.data;
/**
* A Prospective customer
*/
public class Prospects
{
private String prospectname;
private boolean contacted;
private String stringContacted;
public Prospects(String aName, boolean contacted)
{
prospectname = aName;
this.contacted = contacted;
}
public Prospects(String aName, String contacted)
{
prospectname = aName;
this.stringContacted = contacted;
}
public Prospects(String aName)
{
prospectname = aName;
}
public String getName()
{
return prospectname;
}
public boolean isChecked()
{
return contacted;
}
public void setName (String aName)
{
prospectname = aName;
}
public void setContacted(boolean contacted)
{
this.contacted = contacted;
}
public void toggleContacted()
{
if (contacted)
{
contacted = false;
}
else
{
contacted = true;
}
}
}
package com.veetox.networkmarketingmanager.helper;
import android.content.Context;
import android.widget.Toast;
import com.veetox.networkmarketingmanager.data.Prospects;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Map;
import java.util.Scanner;
/**
* Created by Matt on 28/09/2016.
*/
public final class FileHelper
{
Context context;
public FileHelper(Context aContext)
{
context = aContext;
}
public void saveProspects(ArrayList<Prospects> prospects)
{
ArrayList<Prospects> p = prospects;
BufferedWriter bufferedFileWriter = null;
try
{
File aFile = new File(context.getFilesDir(), "prospects.csv");
aFile.createNewFile();
bufferedFileWriter = new BufferedWriter(new FileWriter(aFile));
for (Prospects eachProspect : p)
{
bufferedFileWriter.write(eachProspect.getName());
bufferedFileWriter.write(",");
bufferedFileWriter.write(String.valueOf(eachProspect.isChecked()));
bufferedFileWriter.newLine();
}
}
catch (Exception e)
{
e.printStackTrace();
// Toast.makeText(context, e.toString(), Toast.LENGTH_SHORT).show();
}
finally
{
try
{
bufferedFileWriter.close();
}
catch (Exception e)
{
e.printStackTrace();
//Toast.makeText(context, e.toString(), Toast.LENGTH_SHORT).show();
}
}
}
public ArrayList<Prospects> loadProspects()
{
ArrayList<Prospects> prospectsList = new ArrayList<>();
Scanner bufferedScanner = null;
Scanner lineScanner = null;
try
{
File aFile = new File(context.getFilesDir(), "prospects.csv");
bufferedScanner = new Scanner(new BufferedReader(new FileReader(aFile)));
while (bufferedScanner.hasNextLine())
{
lineScanner = new Scanner(bufferedScanner.nextLine());
lineScanner.useDelimiter(",");
String prospect = lineScanner.next();
String contacted = lineScanner.next();
Prospects aProspect = new Prospects(prospect, contacted);
prospectsList.add(aProspect);
}
}
catch (Exception e)
{
e.printStackTrace();
//Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();
}
finally
{
try
{
bufferedScanner.close();
}
catch (Exception e)
{
e.printStackTrace();
//Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();
}
}
return prospectsList;
}
public ArrayList<Prospects> testProspects()
{
ArrayList<Prospects> p = new ArrayList<>();
Prospects q = new Prospects("Matt Lee", true);
Prospects r = new Prospects("Rosemary Watson", false);
Prospects s = new Prospects("Joe Bloggs", false);
Prospects t = new Prospects("Ronny Corbit", false);
Prospects u = new Prospects("Mr Man", false);
Prospects v = new Prospects("Mr Bond", true);
Prospects w = new Prospects("Mr Blobby", true);
Prospects x = new Prospects("Mary Rose", false);
Prospects y = new Prospects("Jane Doe", false);
Prospects z = new Prospects("Lucy Sanders", false);
p.add(q);
p.add(r);
p.add(s);
p.add(t);
p.add(u);
p.add(v);
p.add(w);
p.add(x);
p.add(y);
p.add(z);
return p;
}
}
您需要在适配器中移动您的复选框 check(checked/notChecked)。您需要 ItemId 才能访问和检索 ListView 中单击的项目。
我有一个当前在 ListView 中显示姓名列表的应用程序。 ListView 中的每个项目都由一个 CheckBox 和一个 EditText 组成。
名称列表由 'Prospects' 个对象的 ArrayList 提供
当用户选择 CheckBox 时,我想 return 此复选框的状态,并在 ArrayList 中的相关位置为它分配 Propsects 对象,以便下次应用程序可以保存它以供使用已打开(这将使用 CSV 完成)。
我正在努力寻找正确的侦听器来执行此操作,因为这是我第一次尝试 Android 编程。我一直无法找到任何与我想要实现的目标相匹配的例子,或者如果我有我不理解它们!。
如有任何帮助,我们将不胜感激!
我的代码如下:
package com.veetox.networkmarketingmanager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import com.veetox.networkmarketingmanager.data.Prospects;
import com.veetox.networkmarketingmanager.helper.FileHelper;
import com.veetox.networkmarketingmanager.helper.ProspectListAdapter;
import com.veetox.networkmarketingmanager.helper.ProspectViewHolder;
import java.io.File;
import java.lang.reflect.Field;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity
{
public static ArrayList<Prospects> prospectList = new ArrayList<>();
Resources res;
FileHelper fileHelper = new FileHelper(this);
ListView prospectsListEntry;
CheckBox prospectsCheckBox;
EditText prospectsEditText;
ProspectListAdapter pla;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prospectsCheckBox = (CheckBox) findViewById(R.id.pr_checkBox);
prospectsEditText = (EditText) findViewById(R.id.pr_name);
res = getResources();
createProspectList();
prospectsListEntry.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View item, int position, long id)
{
//Create new Prospect cloning Prospect at row in ItemList that was clicked
Prospects prospect = pla.getItem(position);
//Change the state of contacted from true to false or vice versa
prospect.toggleContacted();
//Create a ProspectViewHolder to hold the view for the row clicked
ProspectViewHolder viewHolder = (ProspectViewHolder) item.getTag();
viewHolder.getCheckBox().setChecked(prospect.isChecked());
prospectList.get(position) ;
pla.notifyDataSetChanged();
}
});
}
public void testProspects(View view)
{
prospectList = fileHelper.testProspects();
pla.notifyDataSetChanged();
}
public void createProspectList()
{
fileHelper = new FileHelper(this);
//Comment before testing
//prospectList = fileHelper.loadProspects();
//Uncomment for testing
prospectList = fileHelper.testProspects();
//Create the list
pla = new ProspectListAdapter(this, R.layout.prospects_list_layout, prospectList);
prospectsListEntry = (ListView) findViewById(R.id.prospects_list);
prospectsListEntry.setAdapter(pla);
}
}
package com.veetox.networkmarketingmanager.helper;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import com.veetox.networkmarketingmanager.MainActivity;
import com.veetox.networkmarketingmanager.R;
import com.veetox.networkmarketingmanager.data.Prospects;
import java.util.ArrayList;
/**
* Created by Matt on 29/09/2016.
*/
public class ProspectListAdapter extends ArrayAdapter<Prospects>
{
private Context context;
private int layoutResourceId;
private ArrayList<Prospects> prospect;
public ProspectListAdapter(Context context, int layoutResourceId, ArrayList<Prospects> prospect)
{
super(context, layoutResourceId, prospect);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.prospect = prospect;
}
public int getCount()
{
return prospect.size();
}
public Prospects getItem(int position)
{
return prospect.get(position);
}
public long getItemId(int position)
{
return position;
}
@Override
@NonNull
public View getView(int position, View convertView, @NonNull ViewGroup parent)
{
View v = convertView;
if (v == null)
{
LayoutInflater vi;
vi = LayoutInflater.from(context);
v = vi.inflate(layoutResourceId, parent, false);
}
Prospects prospects = prospect.get(position);
if (prospects != null)
{
TextView prname = (TextView) v.findViewById(R.id.pr_name);
CheckBox complete = (CheckBox) v.findViewById(R.id.pr_checkBox);
if (prname != null)
{
prname.setText(prospects.getName());
}
if (complete != null)
{
complete.setChecked(prospects.isChecked());
}
}
return v;
}
}
package com.veetox.networkmarketingmanager.data;
/**
* A Prospective customer
*/
public class Prospects
{
private String prospectname;
private boolean contacted;
private String stringContacted;
public Prospects(String aName, boolean contacted)
{
prospectname = aName;
this.contacted = contacted;
}
public Prospects(String aName, String contacted)
{
prospectname = aName;
this.stringContacted = contacted;
}
public Prospects(String aName)
{
prospectname = aName;
}
public String getName()
{
return prospectname;
}
public boolean isChecked()
{
return contacted;
}
public void setName (String aName)
{
prospectname = aName;
}
public void setContacted(boolean contacted)
{
this.contacted = contacted;
}
public void toggleContacted()
{
if (contacted)
{
contacted = false;
}
else
{
contacted = true;
}
}
}
package com.veetox.networkmarketingmanager.helper;
import android.content.Context;
import android.widget.Toast;
import com.veetox.networkmarketingmanager.data.Prospects;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Map;
import java.util.Scanner;
/**
* Created by Matt on 28/09/2016.
*/
public final class FileHelper
{
Context context;
public FileHelper(Context aContext)
{
context = aContext;
}
public void saveProspects(ArrayList<Prospects> prospects)
{
ArrayList<Prospects> p = prospects;
BufferedWriter bufferedFileWriter = null;
try
{
File aFile = new File(context.getFilesDir(), "prospects.csv");
aFile.createNewFile();
bufferedFileWriter = new BufferedWriter(new FileWriter(aFile));
for (Prospects eachProspect : p)
{
bufferedFileWriter.write(eachProspect.getName());
bufferedFileWriter.write(",");
bufferedFileWriter.write(String.valueOf(eachProspect.isChecked()));
bufferedFileWriter.newLine();
}
}
catch (Exception e)
{
e.printStackTrace();
// Toast.makeText(context, e.toString(), Toast.LENGTH_SHORT).show();
}
finally
{
try
{
bufferedFileWriter.close();
}
catch (Exception e)
{
e.printStackTrace();
//Toast.makeText(context, e.toString(), Toast.LENGTH_SHORT).show();
}
}
}
public ArrayList<Prospects> loadProspects()
{
ArrayList<Prospects> prospectsList = new ArrayList<>();
Scanner bufferedScanner = null;
Scanner lineScanner = null;
try
{
File aFile = new File(context.getFilesDir(), "prospects.csv");
bufferedScanner = new Scanner(new BufferedReader(new FileReader(aFile)));
while (bufferedScanner.hasNextLine())
{
lineScanner = new Scanner(bufferedScanner.nextLine());
lineScanner.useDelimiter(",");
String prospect = lineScanner.next();
String contacted = lineScanner.next();
Prospects aProspect = new Prospects(prospect, contacted);
prospectsList.add(aProspect);
}
}
catch (Exception e)
{
e.printStackTrace();
//Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();
}
finally
{
try
{
bufferedScanner.close();
}
catch (Exception e)
{
e.printStackTrace();
//Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();
}
}
return prospectsList;
}
public ArrayList<Prospects> testProspects()
{
ArrayList<Prospects> p = new ArrayList<>();
Prospects q = new Prospects("Matt Lee", true);
Prospects r = new Prospects("Rosemary Watson", false);
Prospects s = new Prospects("Joe Bloggs", false);
Prospects t = new Prospects("Ronny Corbit", false);
Prospects u = new Prospects("Mr Man", false);
Prospects v = new Prospects("Mr Bond", true);
Prospects w = new Prospects("Mr Blobby", true);
Prospects x = new Prospects("Mary Rose", false);
Prospects y = new Prospects("Jane Doe", false);
Prospects z = new Prospects("Lucy Sanders", false);
p.add(q);
p.add(r);
p.add(s);
p.add(t);
p.add(u);
p.add(v);
p.add(w);
p.add(x);
p.add(y);
p.add(z);
return p;
}
}
您需要在适配器中移动您的复选框 check(checked/notChecked)。您需要 ItemId 才能访问和检索 ListView 中单击的项目。