gpt4 book ai didi

java - 空 Toast 消息

转载 作者:行者123 更新时间:2023-12-01 14:47:31 26 4
gpt4 key购买 nike

我有一个“行” View ,其中有两个 TextView 和一个复选框。我正在尝试实现它,以便当单击复选框时,它会在同一行中显示两个 TextView 的字符串。

我实现了一个 for 循环来获取复选框 View 的父 View ,然后迭代所有父 View subview 并从其中获取字符串。但我收到的不是文本,而是一条空的 Toast 消息。

我的第一个类代码:

public class InteractiveArrayAdapter extends ArrayAdapter<model> implements
OnClickListener {

private final List<model> list;
private final Activity context;

public InteractiveArrayAdapter(Activity context, List<model> list) {
super(context, R.layout.rep, list);
this.context = context;
this.list = list;
}

static class ViewHolder {
protected TextView text;
protected CheckBox checkbox;
}

public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.rep, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) view.findViewById(R.id.TextView07);
viewHolder.checkbox = (CheckBox) view.findViewById(R.id.CheckBox05);

view.getParent();

viewHolder.checkbox
.setOnClickListener(new CompoundButton.OnClickListener() {

@Override
public void onClick(View v) {
boolean h = ((CompoundButton) v).isChecked();
model element = (model) viewHolder.checkbox
.getTag();
element.setSelected(h);

ViewGroup row = (ViewGroup) viewHolder.checkbox
.getParent();

for (int itemPos = 0; itemPos < ((ViewGroup) row)
.getChildCount(); itemPos++) {
View view = ((ViewGroup) row)
.getChildAt(itemPos);
if (view instanceof TextView) {
viewHolder.text = (TextView) view; //Found it!
Toast.makeText(context,
viewHolder.text.getText(), 10000)
.show();

break;
}
}
}
});

view.setTag(viewHolder);
viewHolder.checkbox.setTag(list.get(position));
} else {
view = convertView;
((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
}

ViewHolder holder = (ViewHolder) view.getTag();
holder.text.setText(list.get(position).getName());
holder.checkbox.setChecked(list.get(position).isSelected());
return view;
}
}

我的二等模型:

public class ListActivity1 extends ListActivity {

public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_list);
// Create an array of Strings, that will be put to our ListActivity
InteractiveArrayAdapter adapter = new InteractiveArrayAdapter(this,
getModel());
setListAdapter(adapter);
}

private List<model> getModel() {
List<model> list = new ArrayList<model>();
list.add(get("Linux"));
list.add(get("Windows7"));
list.add(get("Suse"));
list.add(get("Eclipse"));
list.add(get("Ubuntu"));
list.add(get("Solaris"));
list.add(get("Android"));
list.add(get("iPhone"));
list.add(get("iPhone"));
list.add(get("iPhone"));
list.add(get("iPhone"));
list.add(get("iPhone"));
list.add(get("iPhone"));
list.add(get("iPhone"));
list.add(get("iPhone"));
list.add(get("iPhone"));
list.add(get("iPhone"));
list.add(get("iPhone"));
list.add(get("iPhone"));

// Initially select one of the items
list.get(1).setSelected(true);
return list;
}

private model get(String s) {
return new model(s);
}
}

有人可以帮忙吗?

最佳答案

请尝试使用此适配器而不是您的自定义适配器。

public class ListAdapter<T extends BaseEntity> extends ArrayAdapter {
@SuppressWarnings("unused")
private final List<T> objects;
private Activity activity;

private final List<T> checkboxStatusListOfObject;

private OnClickListener listener;

@SuppressWarnings("unchecked")
public ListAdapter(Activity activity, List<T> objects,
OnClickListener listener, List<T> checkboxStatusListOfObject) {
super( R.layout.simple_row_checkbox_1_item_listview , objects);
this.objects = objects;
this.activity = activity;
this.listener = listener;
this.checkboxStatusListOfObject = checkboxStatusListOfObject;

getFilter();
}

@SuppressWarnings("unchecked")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
ObjectViews sqView = null;

if(rowView == null)
{
// Get a new instance of the row layout view
LayoutInflater inflater = this.activity.getLayoutInflater();
rowView = inflater.inflate(R.layout.YourCustomLAyout, null);

// Hold the view objects in an object,
// so they don't need to be re-fetched
sqView = new ObjectViews();
sqView.checbox = (CheckBox) rowView.findViewById(R.id.checkbox);
if(this.listener !=null){
sqView.checbox.setOnClickListener( this.listener);
}
sqView.text1 = (TextView) rowView.findViewById(R.id.text);
sqView.dataObject = new Object();

// Cache the view objects in the tag,
// so they can be re-accessed later
rowView.setTag(sqView);
} else {
sqView = (ObjectViews) rowView.getTag();
}

// Transfer the stock data from the data object
// to the view objects
T object = (T) objects.get(position);
sqView.text1.setText(object.toString());



sqView.checbox.setText("");
sqView.checbox.setTag(sqView);
sqView.dataObject = object;

if(sqView.getChecbox().isChecked()){
if(this.checkboxStatusListOfObject.indexOf( sqView.dataObject) != -1 ){
sqView.getChecbox().setChecked(true);
}
else{
sqView.getChecbox().setChecked(false);
}
}
return rowView;
}

public class ObjectViews {
Object dataObject;
CheckBox checbox;
TextView text1;
public Object getDataObject() {
return dataObject;
}
public void setDataObject(Object dataObject) {
this.dataObject = dataObject;
}
public CheckBox getChecbox() {
return checbox;
}
public void setChecbox(CheckBox checbox) {
this.checbox = checbox;
}
public TextView getText1() {
return text1;
}
public void setText1(TextView text1) {
this.text1 = text1;
}

}

在 Activity 中定义监听器并发送到适配器。

OnClickListener listener = new OnClickListener() {

@Override
public void onClick(View v) {
CheckBox cb = (CheckBox) v ;
// here your code
if(cb.isChecked()){
ObjectViews object = (ObjectViews) cb.getTag();
String text = object.getText1().getText();
Toast.makeText(context,text, Toast.LENGTH_LONG).show();
}
}
};

//您可以配置此适配器构造函数。这只是示例

yourListView.setAdapter(
new ListAdapter(getApplicationContext(), yourObjectList
listener, YourSelectedObjectList));

和布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="6dip"
android:paddingTop="4dip" >

<CheckBox
android:id="@+id/checkbox"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20dp" />

<TextView
android:id="@+id/text"
android:layout_width="0dp"
android:layout_marginLeft="10dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:textSize="20dp" />

</LinearLayout>

关于java - 空 Toast 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15270909/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com