this is the image of layout tile.xml カスタムアダプターを使用してリストを作成しました.2つのテキストビューと2つのボタンがあります。ボタンのクリック時にtextviewの表示の変更が必要です。ボタン上のテキストビューの内容を変更するリストビュー内でクリックする
カスタムアダプターの外側にあるボタンのクリックを処理しています。オン/オフボタンを使用して、id tvstatusの2番目のテキストビューの表示切り替えを切り替えたいとします。
これはこれは私がlistelement
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_margin="2dp"
android:layout_height="100dp"
card_view:cardCornerRadius="4dp"
android:layout_alignParentStart="true"
android:background="#611818"
android:paddingBottom="10dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OFF"
android:id="@+id/boff"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginStart="37dp"
android:focusable="false"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bon"
android:text="ON"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginEnd="28dp"
android:focusable="false"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ttile"
android:id="@+id/tvappname"
android:textSize="20sp"
android:layout_above="@+id/boff"
android:layout_centerHorizontal="true"
android:textColor="@color/abc_input_method_navigation_guard" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ON"
android:id="@+id/tvstatus"
android:layout_below="@+id/tvappname"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:textColor="#060505"
android:visibility="invisible"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
として使用しています私のxmlファイルのコードですcustomadapter
package slide.apptech.com.rpiconnect;
/**
* Created by MOHIT on 09-06-2016.
*/
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
//custom adapter class extends a arrayadapter
public class customadapter2 extends ArrayAdapter<String> {
private final Context context;
private final ArrayList values;
private String stv = "Ststus";
public customadapter2(Context context, ArrayList values) {
//for super constructor pass
// context files
//layout file required for list
//arraylist that has strings to be displayed
super(context, R.layout.tile, values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final int pos = position;
final ViewGroup par = parent;
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.tile, parent, false);
final TextView textView = (TextView) rowView.findViewById(R.id.tvappname);
Button on = (Button) rowView.findViewById(R.id.bon);
Button off = (Button) rowView.findViewById(R.id.boff);
final TextView status = (TextView) rowView.findViewById(R.id.tvstatus);
on.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
status.setVisibility(View.VISIBLE);
notifyDataSetChanged();
((ListView) par).performItemClick(v, pos, 0); // Let the event be handled in onItemClick()
}
});
off.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) ;
((ListView) par).performItemClick(v, pos, 0); // Let the event be handled in onItemClick()
}
});
//get(position method is used to access the elements of arraylist)
String val = (String) values.get(position);
textView.setText(val);
// Change the icon for Windows and iPhone
String s = (String) values.get(position);
return rowView;
}
public void myClickHandler(View v)
{
}
}
するためのコードで、あなたが他のファイル
を助けることを願っていますか? – Neo
いいえ私は状態を格納するためにarraylistを使用していません。デフォルト値 'on'を持っています。xmlファイルを参照してください。 – M2skills
状況を確認して、可視性を変更してください。オフになっている場合は非表示にし、それ以外の場合は非表示にします。あなたのarraylistとnotifyDatasetchange()の値を変更するには、オフボタンをクリックするとコードが訂正されます:) – Neo