具有文本视图和切换按钮i =的列表视图实现 [英] List view implementation with text view and toggle button i=
本文介绍了具有文本视图和切换按钮i =的列表视图实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的应用程序中,我想使用切换按钮和文本视图创建列表视图.当我单击列表项时,我要进行下一个活动.但是,当我单击"Toogle"按钮时,我需要显示它处于关闭模式还是打开模式.在下面添加我的代码.
Here in my application I want to create the list view with toggle button and text view. when I am clicking on the list item I want to go next Activity. But when I am clicking on the Toogle button I need to display it is in off mode or on mode. below I add my code.
$ 这是我的主要活动
$ This is my main activity
public class MainActivity extends Activity {
int startminute;
int endminute;
Date date;
ToggleButton togg;
ListView lv;
String[] days = { "SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY",
"FRIDAY", "SATURDAY" };
boolean[] onOff = new boolean[] { false, false, false, false, false, false,
false };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
if (savedInstanceState != null) {
onOff = savedInstanceState.getBooleanArray("status");
}
lv = (ListView) findViewById(R.id.listView1);
lv.setAdapter(new MyAdapter());
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
Intent in = new Intent(MainActivity.this, StartAndEndTime.class);
in.putExtra("position", position);
startActivity(in);
}
});
}
public class MyAdapter extends BaseAdapter {
public int getCount() {
return days.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public int getViewTypeCount() {
return days.length;
}
public View getView(final int position, View convertView,
ViewGroup parent) {
View v = null;
TextView arryText;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.inflate, null);
arryText = (TextView) v.findViewById(R.id.inflateText);
togg = (ToggleButton) v.findViewById(R.id.toggleButton1);
v.setTag(new ViewHolder(arryText, togg));
togg.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (togg.isChecked()) {
togg.setChecked(false);
onOff[position] = false;
Toast.makeText(MainActivity.this, "is off",
Toast.LENGTH_SHORT).show();
} else {
onOff[position] = true;
togg.setChecked(true);
Toast.makeText(MainActivity.this, "is on",
Toast.LENGTH_SHORT).show();
}
}
});
arryText.setText(days[position]);
togg.setChecked(onOff[position]);
}
return v;
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBooleanArray("status", onOff);
}
推荐答案
以这种方式更改您的getView
方法...
change your getView
method in this way...
public View getView(final int position, View convertView,
ViewGroup parent) {
View v = null;
TextView arryText;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.inflate, null);
}
arryText = (TextView) v.findViewById(R.id.inflateText);
togg = (ToggleButton) v.findViewById(R.id.toggleButton1);
v.setTag(new ViewHolder(arryText, togg));
togg.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (onOff[position]) {
togg.setChecked(false);
onOff[position] = false;
Toast.makeText(MainActivity.this, "is off",
Toast.LENGTH_SHORT).show();
} else {
onOff[position] = true;
togg.setChecked(true);
Toast.makeText(MainActivity.this, "is on",
Toast.LENGTH_SHORT).show();
}
}
});
arryText.setText(days[position]);
togg.setChecked(onOff[position]);
return v;
}
这篇关于具有文本视图和切换按钮i =的列表视图实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文