我在this教程之后开发了一个购物清单应用。现在,在完成整个教程后,我想为每个项目列表添加单选按钮,以便了解哪些项目已从购物清单中购买。但我不知道如何做到这一点。如果你可以指导我这将有所帮助。
这是我的源代码
package com.dopenets.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import android.app.AlertDialog;
import android.widget.EditText;
import android.content.DialogInterface;
import android.content.Context;
import android.content.SharedPreferences;
import android.app.Activity;
import android.widget.Toast;
import android.widget.AdapterView;
import android.widget.TextView;
import android.view.View;
public class MainActivity extends AppCompatActivity {
ArrayList<String> shoppingList = null;
ArrayAdapter<String> adapter = null;
ListView lv = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
shoppingList = getArrayVal(getApplicationContext());
Collections.sort(shoppingList);
adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, shoppingList);
lv = (ListView) findViewById(R.id.listView);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView parent, View view, final int position, long id) {
String selectedItem = ((TextView) view).getText().toString();
if (selectedItem.trim().equals(shoppingList.get(position).trim())) {
removeElement(selectedItem, position);
} else {
Toast.makeText(getApplicationContext(),"Error Removing Element", Toast.LENGTH_LONG).show();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_add){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Add Item");
final EditText input = new EditText(this);
builder.setView(input);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
shoppingList.add(preferredCase(input.getText().toString()));
Collections.sort(shoppingList);
storeArrayVal(shoppingList, getApplicationContext());
lv.setAdapter(adapter);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
return true;
}
return super.onOptionsItemSelected(item);
}
public static String preferredCase (String original)
{
if (original.isEmpty())
return original;
return original.substring(0,1).toUpperCase() + original.substring(1).toLowerCase();
}
public static void storeArrayVal( ArrayList<String> inArrayList, Context context)
{
Set<String> WhatToWrite = new HashSet<String>(inArrayList);
SharedPreferences WordSearchPutPrefs = context.getSharedPreferences("dbArrayValues", Activity.MODE_PRIVATE);
SharedPreferences.Editor prefEditor = WordSearchPutPrefs.edit();
prefEditor.putStringSet("myArray", WhatToWrite);
prefEditor.commit();
}
public static ArrayList getArrayVal( Context dan)
{
SharedPreferences WordSearchGetPrefs = dan.getSharedPreferences("dbArrayValues",Activity.MODE_PRIVATE);
Set<String> tempSet = new HashSet<String>();
tempSet = WordSearchGetPrefs.getStringSet("myArray", tempSet);
return new ArrayList<String>(tempSet);
}
public void removeElement(String selectedItem, final int position){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Remove " + selectedItem + "?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
shoppingList.remove(position);
Collections.sort(shoppingList);
storeArrayVal(shoppingList, getApplicationContext());
lv.setAdapter(adapter);
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
}
}
答案 0 :(得分:0)
查看Android Developer,它解释了如何实现Radio Buttons。 https://developer.android.com/guide/topics/ui/controls/radiobutton.html
使用单选按钮选项在布局中添加无线电组,如示例所示。因为听起来你在点击它们时没有做任何动作,你只需将Radio Group(带单选按钮)添加到simple_list_item_1然后当调用onRadioButtonClicked时它只更改收音机以显示它为已选中(此代码为添加到您的MainActivity):
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
希望这有帮助!
答案 1 :(得分:0)
1.create custom layout with radiobutton or checkbox and textview (xml)
2.create custom adapter with custom class
3. custom class which contains string productname,boolean checked;
Your Custom Layout
<RelativeLayout>
<RadioButton></RadioButton>
<TextView></TextView>
</RelativeLayout>
Your Custom Class
public class mycustomclass
{
public String ProductName;
public boolean productChecked;
}
Your Custom Adapter
public class MyAdapter extends ArrayAdapter<mycustomclass>
{
//your code
}
update mycustom class boolean field when radio checked and unchecked