2017-05-25 98 views
1

伊夫看着这个问题上计算器的几个其他问题,但是我现在无法落实到我的代码这是我做的事情有点不同,我相信。我仍然在学习Android Studio,因此请耐心等待任何愚蠢。Android Studio中的ListView复选框保存

我期待在列表视图右侧创建复选框我目前有检查将留在该国,当用户加载应用早在

XML:

<TextView 
    android:id="@+id/textView3" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:text="Which Will Be Next" 
    android:textAllCaps="false" 
    android:textColor="#f1b75a" 
    android:textSize="32sp" 
    android:textStyle="bold" /> 

<ListView 
    android:id="@+id/results_listview" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_below="@+id/textView3"></ListView> 
</RelativeLayout> 

活动代码:

public class .... extends AppCompatActivity { 



@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_all_flavour); 

    ListView resultListView = (ListView) findViewById(R.id.results_listview); 

    HashMap<String, String> nameAddress = new HashMap<>(); 
    nameAddress.put("exmp ", "Locate"); 


    List<HashMap<String, String>> listItems = new ArrayList<>(); 
    SimpleAdapter adapter = new SimpleAdapter(this, listItems, R.layout.list_item, new String[]{"First Address", "Second Address"},new int[]{R.id.tv1, R.id.tv3}); 

    Iterator it = nameAddress.entrySet().iterator(); 
    while (it.hasNext()){ 
     HashMap<String, String> resultsMap = new HashMap<>(); 
     Map.Entry pair = (Map.Entry)it.next(); 
     resultsMap.put("First Address", pair.getKey().toString()); 
     resultsMap.put("Second Address", pair.getValue().toString()); 
     listItems.add(resultsMap); 

    } 

    resultListView.setAdapter(adapter); 


} 


} 

列表Items.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<CheckedTextView 
    android:id="@+id/tv1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:textColor="#ffa200" 
    android:textSize="21sp" 
    android:textStyle="bold" 
    android:drawableLeft="?android:attr/listChoiceIndicatorMultiple" 
    /> 
<CheckedTextView 
    android:id="@+id/tv3" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:textColor="#ffffff" 
    android:textSize="16sp" 
    android:textStyle="bold" 

    /> 

</LinearLayout> 

任何帮助表示赞赏:)

+0

因此,实际上什么ü想要做什么? –

+0

@LokeshDesai我想一个复选框添加到列表视图中的每一项,如果他们点击它的状态被保存为下一次登录时 – 77rshah

+0

简单的只是使用份额偏好研究用于存储选择复选框的状态 –

回答

0

,我建议你可以使用Recyclerview代替列表视图,试试这个办法

public class CardViewDataAdapter extends 
    RecyclerView.Adapter<CardViewDataAdapter.ViewHolder> { 

private List<Student> stList; 

public CardViewDataAdapter(List<Student> students) { 
    this.stList = students; 

} 

// Create new views 
@Override 
public CardViewDataAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, 
    int viewType) { 
    // create a new view 
    View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(
    R.layout.cardview_row, null); 

    // create ViewHolder 

    ViewHolder viewHolder = new ViewHolder(itemLayoutView); 

    return viewHolder; 
} 

@Override 
public void onBindViewHolder(ViewHolder viewHolder, int position) { 

    final int pos = position; 

    viewHolder.tvName.setText(stList.get(position).getName()); 

    viewHolder.tvEmailId.setText(stList.get(position).getEmailId()); 

    viewHolder.chkSelected.setChecked(stList.get(position).isSelected()); 

    viewHolder.chkSelected.setTag(stList.get(position)); 


    viewHolder.chkSelected.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View v) { 
    CheckBox cb = (CheckBox) v; 
    Student contact = (Student) cb.getTag(); 

    contact.setSelected(cb.isChecked()); 
    stList.get(pos).setSelected(cb.isChecked()); 

    Toast.makeText(
     v.getContext(), 
     "Clicked on Checkbox: " + cb.getText() + " is " 
     + cb.isChecked(), Toast.LENGTH_LONG).show(); 
    } 
    }); 

} 

// Return the size arraylist 
@Override 
public int getItemCount() { 
    return stList.size(); 
} 

public static class ViewHolder extends RecyclerView.ViewHolder { 

    public TextView tvName; 
    public TextView tvEmailId; 

    public CheckBox chkSelected; 

    public Student singlestudent; 

    public ViewHolder(View itemLayoutView) { 
    super(itemLayoutView); 

    tvName = (TextView) itemLayoutView.findViewById(R.id.tvName); 

    tvEmailId = (TextView) itemLayoutView.findViewById(R.id.tvEmailId); 
    chkSelected = (CheckBox) itemLayoutView 
    .findViewById(R.id.chkSelected); 

    } 

} 

// method to access in activity after updating selection 
public List<Student> getStudentist() { 
    return stList; 
} 

} 

http://android-pratap.blogspot.in/2015/01/recyclerview-with-checkbox-example.html

+0

我只是在了解一些这一点,这也像它需要更多一点,以使其发挥作用。我试图保持简单,所以我可以先学习简单的方法,但如果我找不到另一种方式,我会进一步研究这一点。谢谢 – 77rshah

+0

好吧我试图实现这一点,但仍然没有运气,不知道我在做什么,并获得错误分配。你是否有可能展示你将如何以我拥有它的方式来实现? – 77rshah

+0

似乎在博客中的代码是配发的不幸:( – 77rshah