How do I choose in setconnectdevice?
LinearLayout linearLayout = new LinearLayout(this);//自定義一個佈局文件
linearLayout.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
ListView listView = new ListView(mContext);
listView.setFadingEdgeLength(0);
List<Map<String, Object>> deviceList = new ArrayList<Map<String, Object>>();
for (int i = 0; i < remoteDeviceList.size(); i++) {
Map<String, Object> nameMap = new HashMap<String, Object>();
//標題
nameMap.put("ModelName",
remoteDeviceList.get(i).getDetails().getModelDetails().getModelName());
//內容
nameMap.put("Model", remoteDeviceList.get(i));
deviceList.add(nameMap);
}
SimpleAdapter adapter = new SimpleAdapter(mContext,
deviceList, R.layout.listview,
new String[]{"ModelName"}, new int[]{R.id.tv_popup_title});
listView.setAdapter(adapter);
linearLayout.addView(listView);//往這個佈局中加入listview
final AlertDialog.Builder dialog = new AlertDialog.Builder(mContext)
.setTitle("投放到").setView(linearLayout);
dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}).create();
dialog.show();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView tv = (TextView) view.findViewById(R.id.tv_popup_title);
RemoteDevice choiceDevice = (RemoteDevice) parent.getItemAtPosition(position);
ServiceManager.getInstance().setConnectedDevice(**choiceDevice**);
Related
In my code below I created a method which is: I would like to put a variable in putExtra that I can get it in the new activity. The problem is in the new activity gave me the first value(numtelephone) from first element in my list. I know that because of getItem(0), but how can I get the value( numtelephone ) for every element from the list?
public class ListClient extends Activity {
String myJSON,test;
TextView numt;
private static final String TAG_RESULTS="result";
private static final String TAG_NOMCLIENT = "nomclient";
private static String TAG_NUMTELEPHONE ="numtelephone";
private static final String TAG_DEPART ="depart";
private static final String TAG_DESTINATION ="destination";
private static final String TAG_NBRPERSONNE ="nbrpersonne";
JSONArray peoples = null;
ArrayList<HashMap<String, String>> personList;
ListView list;
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_client);
list = (ListView) findViewById(R.id.list);
personList = new ArrayList<HashMap<String,String>>();
getData();
}
protected void showList(){
//json parsing etc...
ListAdapter adapter = new SimpleAdapter(
ListClient.this, personList, R.layout.list_item,
new String[]{TAG_NOMCLIENT,TAG_NUMTELEPHONE,TAG_DEPART,TAG_DEPART,TAG_NBRPERSONNE},
new int[]{R.id.name, R.id.numt, R.id.depart, R.id.destination, R.id.nbrpersonne}
);
list.setAdapter(adapter);
}
public void getData(){
class GetDataJSON extends AsyncTask<String, Void, String>{
#Override
protected String doInBackground(String... params) {
// json dowload from web, not needed in this question
return "";
}
#Override
protected void onPostExecute(String result){
myJSON=result;
showList();
}
}
GetDataJSON g = new GetDataJSON();
g.execute();
}
public void GoUpdate(View v ) {
Intent in = new Intent(getBaseContext(), Update.class);
in.putExtra("NUMTELEPHONE",((HashMap<String, String>)list.getAdapter().getItem(0)).get("numtelephone"));
}
startActivity(in);
}
}
There is simple and only one solution. Delete your xml android:click property of list and add this list#setOnItemClickListener in your onCreate:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_client);
list = (ListView) findViewById(R.id.list);
personList = new ArrayList<HashMap<String,String>>();
getData();
//this thing has been added
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(id!=-1){
Intent in = new Intent(getBaseContext(), Update.class);
in.putExtra("NUMTELEPHONE", ((HashMap<String, String>) list.getAdapter().getItem((int)id)).get("numtelephone")); //TAG_NUMTELEPHONE
startActivity(in);
}
}
});
}
To move it from onCreate do:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_client);
list = (ListView) findViewById(R.id.list);
personList = new ArrayList<HashMap<String, String>>();
getData();
list.setOnItemClickListener(new OnMyListItemClickListener());
}
Context context = this;
private class OnMyListItemClickListener implements AdapterView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(id!=-1){
Intent in = new Intent(context, Update.class);
in.putExtra("NUMTELEPHONE", ((HashMap<String, String>) list.getAdapter().getItem((int) id)).get("numtelephone")); //TAG_NUMTELEPHONE
startActivity(in);
}
}
}
Lesson learned - dont create sucha buttons - they are not aware in which item they are, so you cant extract the id - instead just use something like AlertDialog
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(id!=-1){
AlertDialog.Builder al = new AlertDialog.Builder(context);
al.setMessage("Are you sure you want to do it?");
al.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent in = new Intent(context, Update.class);
in.putExtra("NUMTELEPHONE", ((HashMap<String, String>) list.getAdapter().getItem((int) id)).get(TAG_NUMTELEPHONE));
startActivity(in);
}
});
al.setNegativeButton(android.R.string.no, null);
al.show();
}
}
I am having a small application in GWT , the main page is having the initWidget as VerticalPanel and inside there are just other VerticalPanel and horizontalPanels.
But I am not able to see browser scrolling. The verticalpanels are going beyond browser height but there is scrollbars appearing .
Any idea what could be the reason please.
thanks
code::
private Button btnLoad = new Button("load more results");
private CheckBox check_byPakag= new CheckBox("by package");
private CheckBox check_Inheritance= new CheckBox("by inheritance");
private CheckBox check_byComposition= new CheckBox("by composition");
private CheckBox check_byDependency= new CheckBox("by dependency");
private VerticalPanel vpnl_Main = new VerticalPanel();
private FlowPanel flowpanelImages = new FlowPanel();
private VerticalPanel scrollImages;
private IndexServiceAsync rpcService = GWT.create(IndexService.class);
private SuggestBox text_Wild_Card;
private MultiWordSuggestOracle oracle = new MultiWordSuggestOracle();
private int start = 0;
private int finish = 10; // Total number of results to display at first time
private int totalNumberOfResultsToShow = 10; //Total number of results to display on every call(when load more results button is pressed)
private int scrollPosition = 0;
private VerticalPanel vpnlScroll = new VerticalPanel();
public MainPageView(){
check_byPakag.setChecked(true);
check_Inheritance.setChecked(true);
check_byComposition.setChecked(true);
check_byDependency.setChecked(true);
initWidget(vpnl_Main);
getSuggestions();
scrollImages = new VerticalPanel();
scrollImages.add(vpnlScroll);
vpnlScroll.add(flowpanelImages);
btnLoad.setEnabled(false);
vpnl_Main.setSpacing(4);
Window.addResizeHandler(new ResizeHandler(){
#Override
public void onResize(ResizeEvent event) {
}});
}
public void getSuggestions(){
rpcService.getAllSuggestions(new AsyncCallback<List<String>>(){
#Override
public void onFailure(Throwable caught) {
System.out.println(caught.getMessage());
}
#Override
public void onSuccess(List<String> result) {
for(int i=0;i<result.size(); i++){
oracle.add(result.get(i));
}
text_Wild_Card = new SuggestBox(createWildCardOracle());
text_Wild_Card.setText("*");
layout(vpnl_Main);
getIndexData();
}});
}
#SuppressWarnings("deprecation")
public void getIndexData(){
final DecoratedPopupPanel popup = new DecoratedPopupPanel();
popup.setWidget(new Label("Loading.."));
popup.center();
rpcService.getIndexData(text_Wild_Card.getText(), check_byPakag.isChecked(), check_Inheritance.isChecked(), check_byComposition.isChecked(), check_byDependency.isChecked(), start, finish, new AsyncCallback<IndexDataSet>(){
#Override
public void onFailure(Throwable caught) {
Window.alert("getIndexData failed"+ caught.getMessage());
}
#Override
public void onSuccess(IndexDataSet result) {
flowpanelImages.clear();
if(result.hasMore()){
btnLoad.setEnabled(true);
}else{
btnLoad.setEnabled(false);
}
for(int i=0; i< result.getResults().size(); i++){
final Image image = new Image(result.getResults().get(i).stampImageURL);
//////////////
final Label lbl = new Label("dummy");
lbl.setStyleName("invisibleImageLabel");
VerticalPanel vpnlImage = new VerticalPanel();
vpnlImage.add(image);
vpnlImage.add(lbl);
//////////
final DataImage dataImage = new DataImage();
dataImage.setUrl(result.getResults().get(i).stampImageURL);
dataImage.setDescription(result.getResults().get(i).description);
image.addMouseOverHandler(new MouseOverHandler(){
#Override
public void onMouseOver(MouseOverEvent event) {
lbl.setText(dataImage.getDescription());
lbl.setStyleName("imageLabel");
}});
image.addMouseOutHandler(new MouseOutHandler(){
#Override
public void onMouseOut(MouseOutEvent event) {
lbl.setStyleName("invisibleImageLabel");
}});
image.addClickHandler(new ClickHandler(){
#Override
public void onClick(ClickEvent event) {
RootPanel.get("bodyContainer").clear();
History.newItem("diagramView / "+dataImage.getUrl());
}});
vpnlImage.addStyleName("imageStyle");
vpnlImage.addStyleName("paddedFlowPanel");
flowpanelImages.add(vpnlImage);
}
//
HorizontalPanel hpnlBtn = new HorizontalPanel();
hpnlBtn.setWidth("100%");
hpnlBtn.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
hpnlBtn.add(btnLoad);
btnLoad.setStyleName("nextLine");
flowpanelImages.add(hpnlBtn);
popup.removeFromParent();
}});
}
private SuggestOracle createWildCardOracle() {
return oracle;
}
private void layout(VerticalPanel vpnl_Main) {
FlexTable flexWildCard = new FlexTable();
VerticalPanel vpnlCheckBoxes = new VerticalPanel();
HorizontalPanel hpnlWildCard = new HorizontalPanel();
flexWildCard.setWidget(0,0,new Label("Show only class diagrams containing classes named: (use * for wildcard)"));
flexWildCard.setWidget(0,1,text_Wild_Card);
text_Wild_Card.setWidth("200px");
vpnlCheckBoxes.setWidth("200px");
vpnlCheckBoxes.add(check_byPakag);
vpnlCheckBoxes.add(check_Inheritance);
vpnlCheckBoxes.add(check_byComposition);
vpnlCheckBoxes.add(check_byDependency);
hpnlWildCard.add(vpnlCheckBoxes);
hpnlWildCard.add(flexWildCard);
vpnl_Main.add(hpnlWildCard);
}
I have a listview in my main activity.In each row i have a image view,when i click that image view QuickAction(Like popover in ios) will appears.My request is, I want to setText the text from listview to the another Activity's edittext when i click the action item in the quick action.Please help..
Here is my Main Activity
public class ExampleActivity extends Activity {
private static final int ID_UP = 1;
private static final int ID_DOWN = 2;
private static final int ID_SEARCH = 3;
private static final int ID_INFO = 4;
private QuickAction quickAction;
private ActionItem nextItem;
private ActionItem prevItem;
private ActionItem searchItem;
private ListView view;
private ContactsAdapter adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
view = (ListView) findViewById(R.id.listView1);
final Weather weather_data[] = new Weather[] { new Weather(R.drawable.icon, "Cloudy"),
new Weather(R.drawable.icon, "Showers"),
new Weather(R.drawable.icon, "Snow"),
new Weather(R.drawable.icon, "Storm"),
new Weather(R.drawable.icon, "Sunny")
};
adapter = new ContactsAdapter(this, R.layout.main1,
weather_data);
}
void functiontorun(View view1) {
quickAction = new QuickAction(this,QuickAction.HORIZONTAL);
nextItem = new ActionItem(ID_DOWN, "Next", getResources().getDrawable(
R.drawable.menu_down_arrow));
prevItem = new ActionItem(ID_UP, "Prev", getResources().getDrawable(
R.drawable.menu_up_arrow));
searchItem = new ActionItem(ID_SEARCH, "Find", getResources()
.getDrawable(R.drawable.menu_search));
// use setSticky(true) to disable QuickAction dialog being dismissed
// after an item is clicked
prevItem.setSticky(true);
nextItem.setSticky(true);
// add action items into QuickAction
quickAction.addActionItem(nextItem);
quickAction.addActionItem(prevItem);
quickAction.addActionItem(searchItem);
// Set listener for action item clicked
final int position1 = view.getPositionForView(view1);
quickAction
.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {
#Override
public void onItemClick(QuickAction source, int pos,
int actionId) {
ActionItem actionItem = quickAction.getActionItem(pos);
if (actionId == ID_SEARCH) {
Intent i=new Intent(ExampleActivity.this,Second.class);
i.putExtra("position",position1 );
Log.v("position","po" +position1);
startActivity(i);
} else if (actionId == ID_INFO) {
Toast.makeText(getApplicationContext(),
"I have no info this time",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(),
actionItem.getTitle() + " selected",
Toast.LENGTH_SHORT).show();
}
}
});
quickAction.show(view1);
}
}
And my another activity
public class Second extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
EditText d1=(EditText) findViewById(R.id.editText1);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
final int position = (Integer) bundle.get("position");
Log.v("position","po1 " +position);
ArrayList<Weather> array = new ArrayList<Weather>();
Log.v("position","po1 " +array);
}
}
}
contacts adapter:
public class ContactsAdapter extends ArrayAdapter{
Context context;
int layoutResourceId;
Weather data[] = null;
public ContactsAdapter(Context context, int layoutResourceId, Weather[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
WeatherHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new WeatherHolder();
holder.imgIcon = (ImageView)row.findViewById(R.id.imageView1);
holder.imgIcon.findViewById(R.id.imageView1).setOnClickListener(mBuyButtonClickListener);
holder.txtTitle = (TextView)row.findViewById(R.id.textView1);
//((ImageView) row.findViewById(R.id.im)).setOnClickListener(mBuyButtonClickListener);
row.setTag(holder);
}
else
{
holder = (WeatherHolder)row.getTag();
}
Weather weather = data[position];
holder.txtTitle.setText(weather.title);
holder.imgIcon.setImageResource(weather.icon);
return row;
}
static class WeatherHolder
{
ImageView imgIcon;
TextView txtTitle;
}
private OnClickListener mBuyButtonClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
((ExampleActivity) context).functiontorun(v);
}
};
}
private OnClickListener mBuyButtonClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
WeatherHolder holder = (WeatherHolder) v.getTag;
String string = holder.txtTitle .getText();
((ExampleActivity) context).functiontorun(string);
}
};
just pass the string to your activity and change the parameter of the functiontorun as string
Here is the activity ShowData.java where i am setting up customized listview.. Now i need to click view on that listview and need to display another activity but throws force close.. It throws error on listview.setAdapter(adapter);
public class ShowData extends Activity
{
ListView listview;
Intent intent;
ArrayList<BeanClass> datalist;
CustomAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
listview = (ListView) findViewById(R.id.listView1);
Intent intent = getIntent();
datalist = (ArrayList<BeanClass>) intent.getSerializableExtra("list");
Log.i("Size of ArrayList", "is " +datalist.size());
adapter = new CustomAdapter(ShowData.this, R.layout.customrow , datalist);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3)
{
Intent i = new Intent(ShowData.this,DetailActivity.class);
i.putExtra("datalist", datalist);
i.putExtra("position", arg2);
startActivity(i);
}
}
}
Here is the CustomAdapter.java
public class CustomAdapter extends ArrayAdapter<BeanClass>
{
Context context;
int layoutResourceId;
BeanClass bc;
ArrayList<BeanClass> vbc;
public CustomAdapter(Context context, int layoutResourceId, ArrayList<BeanClass> vbc)
{
super(context, layoutResourceId, vbc);
this.context=context;
this.layoutResourceId=layoutResourceId;
this.vbc=vbc;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
StringReaderHolder holder;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent,false);
holder= new StringReaderHolder();
holder.mytext1 = (TextView) row.findViewById(R.id.textView1);
holder.mytext2 = (TextView) row.findViewById(R.id.textView2);
holder.mytext3 = (TextView) row.findViewById(R.id.textView3);
row.setTag(holder);
}
else
{
holder = (StringReaderHolder) row.getTag();
}
BeanClass bsObject = vbc.get(position);
holder.mytext1.setText(bsObject.name);
holder.mytext2.setText(bsObject.address);
holder.mytext3.setText(bsObject.phone);
return row;
}
static class StringReaderHolder
{
TextView mytext1,mytext2,mytext3;
}
}
How should i use setOnItemClickListener...??? Thanks...
In Your getView Method try:
row.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(context,DetailActivity.class);
i.putExtra("datalist", vbc);
i.putExtra("position", position);
startActivity(i);
}
});
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
EditText nameText = (EditText) findViewById(R.id.editText1);
name = nameText.getText().toString();
nameText.setText("");
EditText numberText = (EditText) findViewById(R.id.editText2);
number = numberText.getText().toString();
numberText.setText("");
person = new Person(name, number);
order.add(person);
PersonAdapter pa = new PersonAdapter(con, R.layout.row, order);
setListAdapter(pa);
}
});
}
#Override
protected void onListItemClick(ListView l, View v, final int position, long id) {
// TODO Auto-generated method stub
final int index = position;
AlertDialog.Builder aDialog = new AlertDialog.Builder(this);
aDialog.setTitle("What do you want??");
aDialog.setPositiveButton("Delete",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
order.remove(index);
PersonAdapter pa = new PersonAdapter(con, R.layout.row,
order);
setListAdapter(pa);
}
});
aDialog.setNeutralButton("Edit", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) con.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog, (ViewGroup)findViewById(R.id.root_layout));
builder = new AlertDialog.Builder(con);
builder.setView(layout);
builder.setTitle("Fill EditText");
builder.setIcon(R.drawable.icon);
editNum = (EditText)findViewById(R.id.editNum);
editName = (EditText)findViewById(R.id.editName);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
name = editName.getText().toString();
number = editNum.getText().toString();
order.set(position, new Person(name,number));
PersonAdapter pa = new PersonAdapter(con, R.layout.row, order);
setListAdapter(pa);
}
});
builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
alertDialog = builder.create();
alertDialog.show();
}
});
aDialog.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
AlertDialog ad = aDialog.create();
ad.show();
}
public PersonAdapter(Context context, int textViewResourceId,
ArrayList<Person> items) {
super(context, textViewResourceId, items);
this.items = items;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}
Person p = items.get(position);
if (p != null) {
TextView tt = (TextView) v.findViewById(R.id.android_toptext);
TextView bt = (TextView) v.findViewById(R.id.android_bottomtext);
if (tt != null) {
tt.setText(p.getName());
}
if (bt != null) {
bt.setText("TEL : " + p.getNumber());
}
}
return v;
}
private String name;
private String number;
public Person(String name, String number) {
this.name = name;
this.number = number;
}
public String getName() {
return name;
}
public String getNumber() {
return number;
}
}
I am practicing Widget(exactly I am making custom dialog) and Listener In android . But I stop now because ".getText().toString();" makes error ... I can't understand.. T^T Please Help me... why it makes error?? I think It doesn't have problem
You should declare your EditText globally
EditText editName;
EditText editNum;
#Override
public void onCreate(Bundle savedInstanceState) {
...
}
.....
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) con.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog, (ViewGroup)findViewById(R.id.root_layout));
builder = new AlertDialog.Builder(con);
builder.setView(layout);
builder.setTitle("Fill EditText modificated");
builder.setIcon(R.drawable.icon);
editName = (EditText)findViewById(R.id.editName);
editNum = (EditText)findViewById(R.id.editNum);