我在ListView
中使用DialogFragment
。列表从SQLite
数据库填充
使用SimpleCursorAdaptor
。
现在我正在使用上下文菜单从列表中选择一个项目并且一切正常,但我想使用单选按钮从列表中选择一个项目而不是上下文菜单。
DialogFragment中的代码:
package com.example.saggu.myapplication;
import android.app.DialogFragment;
import android.database.Cursor;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
/**
* Created by Saggu on 1/25/2017.
*/
public class DialogSTB extends DialogFragment implements View.OnClickListener{
SimpleCursorAdapter simpleCursorAdapter;
DbHendler dbHendler;
ListView listViewStb;
String TAG = "MyApp_DialogSTB";
TextView stbcountUA;
int stbcountUa;
int custId;
Button unAssign;
private Cursor mCursor;
EditText searchboxstb;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_stb, null);
dbHendler = new DbHendler(this.getActivity(), null, null, 1);
stbcountUA = (TextView) view.findViewById(R.id.totalStbsUnAssigned);
listViewStb = (ListView) view.findViewById(R.id.stb_list_dialog);
unAssign = (Button) view.findViewById(R.id.unassign_button);
searchboxstb= (EditText) view.findViewById(R.id.searchboxstb);
searchboxstb.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
displaySearchList();
}
@Override
public void afterTextChanged(Editable s) {
if (searchboxstb.getText().toString().equals("")) {
displaySTBList();
}
}
});
unAssign.setOnClickListener(this);
Bundle bundle = getArguments();
custId = bundle.getInt("CUSTID");
registerForContextMenu(listViewStb);
displaySTBList();
stbcount();
return view;
}
//region Create all List
public void displaySTBList() {
try {
Cursor cursor = dbHendler.getUnAssignedSTBs();
if (cursor == null) {
Toast.makeText(getActivity(), "Cursor is Null", Toast.LENGTH_SHORT).show();
return;
}
if (cursor.getCount() == 0) {
Toast.makeText(getActivity(), "No Record Found", Toast.LENGTH_SHORT).show();
return;
}
String[] columns = new String[]{
DbHendler.KEY_SN,
DbHendler.KEY_VC,
DbHendler.KEY_STATUS
};
int[] boundTo = new int[]{
R.id.stb_sn,
R.id.stb_vc,
R.id.stb_status
};
simpleCursorAdapter = new SimpleCursorAdapter(this.getActivity(),
R.layout.stb_list_item,
cursor,
columns,
boundTo,
0);
listViewStb.setAdapter(simpleCursorAdapter);
} catch (Exception ex) {
Toast.makeText(getActivity(), ""+ex, Toast.LENGTH_SHORT).show();
}
}
//endregion
//region Create Search List
public void displaySearchList() {
String searchItem = searchboxstb.getText().toString();
try {
Cursor cursor = dbHendler.searchSTBToList(searchItem);
if (cursor == null) {
// textView4.setText("Unable to generate cursor.");
return;
}
if (cursor.getCount() == 0) {
// textView4.setText("No Customer Found");
return;
} else {
// textView4.setText("");
String[] columns = new String[]{
//DbHendler.KEY_ID,
DbHendler.KEY_SN,
DbHendler.KEY_VC,
DbHendler.KEY_STATUS
// DbHendler.KEY_SN
};
int[] boundTo = new int[]{
//R.id.pId,
R.id.stb_sn,
R.id.stb_vc,
R.id.stb_status
// R.id.vc_mac
};
simpleCursorAdapter = new SimpleCursorAdapter(this.getActivity(),
R.layout.stb_list_item,
cursor,
columns,
boundTo,
0);
listViewStb.setAdapter(simpleCursorAdapter);
}
} catch (Exception ex) {
Log.d(TAG,""+ex);
}
}
//region context menu
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuItem.OnMenuItemClickListener listener = new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
onContextItemSelected(item);
return true;
}
};
menu.add("Assign STB");
for (int i = 0, n = menu.size(); i < n; i++)
menu.getItem(i).setOnMenuItemClickListener(listener);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
super.onContextItemSelected(item);
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
if (item.getTitle() == "Assign STB") {
//entry in assigned column
int stbId = (int) info.id;
int assigned = custId;
if (dbHendler.getSTBID(custId) > 0) {
Toast.makeText(getActivity(), "Already Assigned STB", Toast.LENGTH_LONG).show();
} else {
dbHendler.assignSTB(new STB(stbId, assigned));
dbHendler.SetStbID(custId, stbId);
Toast.makeText(this.getActivity(), "Now Assigned STB", Toast.LENGTH_LONG).show();
swapRefreshCursor();
}
}
return true;
}
@Override
public void onClick(View v) {
if (v.getId()==R.id.unassign_button);
try {
String stbSN = dbHendler.getAssignedSN(getActivity(),custId);
dbHendler.unAssignSTB(stbSN); //from stb table
dbHendler.unSetId(custId); //From cust table
Toast.makeText(getActivity(), "Unassigned cust: " + custId+" STB SN: "+stbSN, Toast.LENGTH_SHORT).show();
displaySTBList();
stbcount();
} catch (Exception e) {
e.printStackTrace();
}
}
//endregion
public void swapRefreshCursor(){
try {
mCursor = dbHendler.getUnAssignedSTBs();
simpleCursorAdapter.swapCursor(mCursor);
stbcount();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getActivity(), ""+e, Toast.LENGTH_SHORT).show();
}
}
public void stbcount(){
stbcountUa= dbHendler.countSTBsUA;
stbcountUA.setText("STBs:"+stbcountUa);
}
}
单个项目的自定义布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:layout_width="match_parent"
android:padding="5dip"
android:layout_height="60dip"
android:clickable="true">
<TextView
android:text="Serial No."
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/textView17"
android:textSize="14sp"
android:textAllCaps="true" />
<TextView
android:text="MAC/VC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/textView18"
android:textSize="14sp" />
<TextView
android:text="Serial No."
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/stb_sn"
android:textSize="14sp"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/textView17"
android:layout_toEndOf="@+id/textView17"
android:layout_marginLeft="34dp"
android:layout_marginStart="34dp" />
<TextView
android:text="VC No."
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/stb_vc"
android:textSize="14sp"
android:layout_alignParentBottom="true"
android:layout_alignLeft="@+id/stb_sn"
android:layout_alignStart="@+id/stb_sn" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/stb_status"
tools:text="ON/OFF"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/Status"
android:layout_alignEnd="@+id/Status" />
<TextView
android:text="Status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Status"
android:textAllCaps="false"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/stb_sn"
android:layout_toEndOf="@+id/stb_sn"
android:layout_marginLeft="36dp"
android:layout_marginStart="36dp" />
</RelativeLayout>
DialogFragment Layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<ListView
android:layout_width="match_parent"
android:layout_height="400dip"
android:id="@+id/stb_list_dialog"
android:layout_weight="0.74"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
/>
<TextView
android:text="Total:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/totalStbsUnAssigned"
android:textSize="18sp"
android:layout_marginLeft="40dp"
android:layout_marginStart="40dp"
android:layout_alignBaseline="@+id/unassign_button"
android:layout_alignBottom="@+id/unassign_button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:text="UnAssign"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/unassign_button"
android:layout_weight="0.74"
android:textAllCaps="false"
android:layout_marginRight="15dp"
android:layout_marginEnd="15dp"
android:layout_below="@+id/stb_list_dialog"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
答案 0 :(得分:0)
只需修改列表视图项布局即可拥有一个单选按钮。 像这样:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:layout_width="match_parent"
android:padding="5dip"
android:layout_height="60dip"
android:clickable="true">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/radio_button" />
<TextView
android:text="Serial No."
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/radio_button"
android:layout_toEndOf="@+id/radio_button"
android:id="@+id/textView17"
android:textSize="14sp"
android:textAllCaps="true" />
<TextView
android:text="MAC/VC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@+id/radio_button"
android:layout_toEndOf="@+id/radio_button"
android:id="@+id/textView18"
android:textSize="14sp" />
<TextView
android:text="Serial No."
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/stb_sn"
android:textSize="14sp"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/textView17"
android:layout_toEndOf="@+id/textView17"
android:layout_marginLeft="34dp"
android:layout_marginStart="34dp" />
<TextView
android:text="VC No."
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/stb_vc"
android:textSize="14sp"
android:layout_alignParentBottom="true"
android:layout_alignLeft="@+id/stb_sn"
android:layout_alignStart="@+id/stb_sn" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/stb_status"
tools:text="ON/OFF"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/Status"
android:layout_alignEnd="@+id/Status" />
<TextView
android:text="Status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Status"
android:textAllCaps="false"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/stb_sn"
android:layout_toEndOf="@+id/stb_sn"
android:layout_marginLeft="36dp"
android:layout_marginStart="36dp" />
</RelativeLayout>
您不需要上下文菜单。从片段代码中删除它。