0

I am trying to put a listview within an activity, then retrieve data from my sqlite database. Then, user can check/uncheck multiple option from that listview that has checkbox included that shows the name of customers.. When the checkbox is checked, i wish to perform function such as add expenses to particular customer in a table in sqlite database that is checked by user. The problem is, i created the listView with checkbox in every row, but i got stucked because i don't know how to perform function on it. Can anyone guide me here? Or giving me idea how to do it? Thanks.

Here is my code with the listView:

public class AddExpense extends ListActivity implements OnClickListener{

EditText expenseName;
Spinner expenseType;
EditText expensePrice;
EditText expenseQuantity;
EventController controller = new EventController(this);
Button btnadd;
ListView lv;

@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.addexpense);
    expenseName = (EditText)findViewById(R.id.expenseName);
    expenseType = (Spinner)findViewById(R.id.expenseType);
    // Create an ArrayAdapter using the string array and a default spinner layout
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
            R.array.type, android.R.layout.simple_spinner_item);
    // Specify the layout to use when the list of choices appears
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    // Apply the adapter to the spinner
    expenseType.setAdapter(adapter);
    expensePrice = (EditText)findViewById(R.id.expensePrice);
    expenseQuantity = (EditText)findViewById(R.id.expenseQuantity);
    btnadd = (Button)findViewById(R.id.btnaddexp);
    btnadd.setOnClickListener(this);

    HashMap<String, String> queryValues = new HashMap<String, String>();
    Intent objIntent = getIntent();
    String eventId = objIntent.getStringExtra("eventId");
    queryValues.put("eventId", eventId);

    //Create Listview that retrieve all the customer data in that event
    ArrayList<HashMap<String, String>> friendList = controller
            .getAllFriends(queryValues);
    if (friendList.size() != 0) {
        lv = getListView();
    }
    SimpleAdapter adapter1 = new SimpleAdapter(AddExpense.this,
            friendList, R.layout.view_expenses_participant_entry, new String[] {
                    "friendId", "friendName" },
            new int[] { R.id.friendId, R.id.friendName});
    adapter.notifyDataSetChanged();
    setListAdapter(adapter1);
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    HashMap<String, String> queryValues = new HashMap<String, String>();
    Intent objIntent = getIntent();
    String eventId = objIntent.getStringExtra("eventId");
    queryValues.put("eventId", eventId);
    queryValues.put("expenseName", expenseName.getText().toString());
    queryValues.put("expenseType", expenseType.getSelectedItem().toString());
    queryValues.put("expensePrice", expensePrice.getText().toString());
    queryValues.put("expenseQuantity", expenseQuantity.getText().toString());
    controller.insertExpense(queryValues);
    this.callHomeActivity(v);
    finish();
}

public void callHomeActivity(View view) {
    super.onResume();
}

}

This is the xml file of every single row :

<CheckBox
    android:id="@+id/list_checkbox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checked="false" >
</CheckBox>

<TextView
    android:id="@+id/friendId"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:visibility="gone" />

<TextView
    android:id="@+id/friendName"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="5dp"
    android:paddingLeft="6dip"
    android:paddingTop="6dip"
    android:textColor="#A4C739"
    android:textSize="17sp"
    android:textStyle="bold" />

</LinearLayout>
4

1 回答 1

1

为 ListView 制作自定义适配器,就像 ArrayAdapter 一样。

在该适配器类中,您将不得不对该类的 getView() 方法进行编码。

在这里,您可以根据单击复选框更新代码。如果选中复选框,则插入记录,如果未选中,则按您的意愿进行操作。

如果您有任何疑问,请告诉我。

于 2013-11-08T09:01:11.010 回答