我有一个应用程序,当前在ListView中显示名称列表。 ListView中的每个项目都包含一个CheckBox和一个EditText。
名称列表由'Prospects'对象的ArrayList提供
当用户选择CheckBox时,我想返回此复选框的状态,并在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;
}
}
答案 0 :(得分:0)
您需要在适配器中移动复选框检查(选中/未检查)。您将需要ItemId来访问和检索ListView中单击的项目。