I created a custom listview with the base adapter, but for some reason can not click on the button inside the individual list views.
Here is the code the the main layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Light List"
android:id="#+id/lightName"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/loginbutton"
android:id="#+id/button"
android:onClick="buttonClicked"
android:layout_alignParentBottom="true"
android:layout_alignLeft="#+id/lightName"
android:layout_alignStart="#+id/lightName" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/lightList"
android:layout_below="#+id/lightName"
android:layout_above="#+id/button"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
</RelativeLayout>
Here is the code for views in the list:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/lightList"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/lightName"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/statusText"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/buttonoff"
android:focusable="true"
android:enabled="true"
android:clickable="true" />
</LinearLayout>
Lastly here is the adapter code:
public ArrayList<myLight> dataSet = new ArrayList<myLight>();
public void makeLightList(ArrayList<myLight> lights) {
Iterator<myLight> iter = lights.iterator();
while(iter.hasNext()) {
dataSet.add(iter.next());
}
}
#Override
public int getCount() {
return dataSet.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v;
LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.lightlistlayout, null);
TextView name = (TextView) findViewById(R.id.lightName);
name.setText(dataSet.get(position).name);
TextView status = (TextView) findViewById(R.id.statusText);
if(dataSet.get(position).reachable) {
if(dataSet.get(position).isOn) {
status.setText("Light is On");
} else {
status.setText("Light is Off");
}
} else {
status.setText("Light is currently not reachable");
}
dataHolder dh = new dataHolder();
dh.name = dataSet.get(position).name;
//Handle Off button click from list view
Button offButton = (Button) findViewById(R.id.buttonoff);
offButton.setTag(dh);
offButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dataHolder dhv = new dataHolder();
dhv = (dataHolder)v.getTag();
Toast.makeText(getBaseContext(), dhv.name + " Turned Off", Toast.LENGTH_LONG).show();
}
});
return v;
}
};
I am not sure if my onclicklistener is correct as I can't click on the button at all yet. Here is a picture showing the button. I have messed with different focus and clickable settings in the xml file and no luck. I added a seeker bar that I was able to slide that back and forth, but the button will not let me click it.
v = inflater.inflate(R.layout.lightlistlayout, null);
Here you have inflated the row file for your adapter and the below are your id initialization and for that you have not added your view to your id, so try this.
TextView name = (TextView) v.findViewById(R.id.lightName);
TextView status = (TextView) v.findViewById(R.id.statusText);
Button offButton = (Button) v.findViewById(R.id.buttonoff);
try changing
android:focusable="true" to android:focusable="false" in Button's xml
Related
I'm trying to create an AlertDialog that has many TextViews (so that scrolling in necessary) and an EditText at the end for users to enter a value that is not included in the list. When AlertDialog is first presented, things look good. However, when I scroll back to the top after scrolling to the bottom problems occur.
Here is the bottom of the list initially presented. All good.
But here is what it looks like after I scroll to the top. Not so good.
This is the Adapter class that I am using:
public class MultiSelectOtherDialog extends DialogFragment {
public static final String TAG = "MultiSelectOtherDialog";
private ArrayList<Bean> mList = new ArrayList<>();
public static MultiSelectOtherDialog newInstance(String aTitle, String[] aElems) {
MultiSelectOtherDialog frag = new MultiSelectOtherDialog();
Bundle args = new Bundle();
args.putString("title", aTitle);
args.putStringArray("elems", aElems);
frag.setArguments(args);
return frag;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// length of this list determines how many items to present - need an extra for the EditText
// at the end for "Other"
int mListLen = (getArguments().getStringArray("elems").length) + 1;
//int mListLen = (getArguments().getStringArray("elems").length);
for (int i = 0; i < mListLen; i++) {
mList.add(new Bean());
}
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
View view = getActivity().getLayoutInflater().inflate(R.layout.scb_listview2, null);
ListView listViewItems = (ListView)view.findViewById(R.id.lvScb);
listViewItems.setAdapter(new MultiSelectOtherAdapter());
listViewItems.setOnItemClickListener(new OnItemClickListenerListViewItem());
builder.setTitle(getArguments().getString("title")).setView(view);
AlertDialog diagFragDialog = builder.create();
return diagFragDialog;
}
public class MultiSelectOtherAdapter extends BaseAdapter {
#Override
public int getCount() {
return mList.size();
}
#Override
public Object getItem(int position) {
return mList.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
// needed for the "Other" EditText at the end
if (position < mList.size() - 1) {
convertView = View.inflate(getActivity(), R.layout.scb_item, null);
holder.tv = (TextView) convertView.findViewById(R.id.tv);
Log.d(TAG, "1)");
}
else {
convertView = View.inflate(getActivity(), R.layout.scb_item_other, null);
holder.tv = (TextView) convertView.findViewById(R.id.et);
Log.d(TAG, "2)");
}
holder.cb = (SmoothCheckBox) convertView.findViewById(R.id.scb);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
Log.d(TAG, "3)");
}
final Bean bean = mList.get(position);
holder.cb.setOnCheckedChangeListener(new SmoothCheckBox.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(SmoothCheckBox checkBox, boolean isChecked) {
bean.isChecked = isChecked;
}
});
// needed for the "Other" EditText at the end
if (position < getArguments().getStringArray("elems").length) {
String text = getArguments().getStringArray("elems")[position];
holder.tv.setText(text);
Log.d(TAG, "4)");
}
else {
convertView = View.inflate(getActivity(), R.layout.scb_item_other, null);
holder.tv = (TextView) convertView.findViewById(R.id.et);
holder.cb = (SmoothCheckBox) convertView.findViewById(R.id.scb);
convertView.setTag(holder);
Log.d(TAG, "5)");
}
holder.cb.setChecked(bean.isChecked);
Log.d(TAG, "6)");
return convertView;
}
class ViewHolder {
SmoothCheckBox cb;
TextView tv;
}
}
public class OnItemClickListenerListViewItem implements AdapterView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Bean bean = (Bean) parent.getAdapter().getItem(position);
bean.isChecked = !bean.isChecked;
SmoothCheckBox checkBox = (SmoothCheckBox) view.findViewById(R.id.scb);
checkBox.setChecked(bean.isChecked, true);
}
}
class Bean implements Serializable {
boolean isChecked;
}
}
And the layouts:
AlertDialog layout:
<?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:orientation="vertical" >
<ListView
android:id="#+id/lvScb"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:cacheColorHint="#0000"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:layout_above="#+id/viewLineHoriz" />
<View
android:id="#+id/viewLineHoriz"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_marginBottom="0dp"
android:layout_above="#+id/bottonRowScb"
android:background="?android:attr/dividerVertical" />
<LinearLayout
style="?android:attr/buttonBarStyle"
android:id="#+id/bottonRowScb"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:measureWithLargestChild="true"
android:paddingTop="0dip"
android:orientation="horizontal" >
<Button
style="?android:attr/buttonBarButtonStyle"
android:id="#+id/buttonOKScb"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/ok_string" />
<View
android:id="#+id/viewLineVert"
android:layout_height="fill_parent"
android:layout_width="1dp"
android:layout_marginBottom="0dp"
android:background="?android:attr/dividerVertical" />
<Button
style="?android:attr/buttonBarButtonStyle"
android:id="#+id/buttonCancelScb"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/cancel_string" />
</LinearLayout>
</RelativeLayout>
TextView item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="horizontal">
<TextView
android:id="#+id/tv"
android:layout_weight="100"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="15sp" />
<cn.refactor.library.SmoothCheckBox
android:id="#+id/scb"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center_vertical|right"
android:layout_weight="1"
android:layout_margin="5dp"/>
</LinearLayout>
And EditText item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="horizontal">
<EditText
android:id="#+id/et"
android:hint="#string/other_notes_string"
android:layout_weight="100"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="15sp"
android:textStyle="bold|italic"/>
<cn.refactor.library.SmoothCheckBox
android:id="#+id/scb"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center_vertical|right"
android:layout_weight="1"
android:layout_margin="5dp"/>
</LinearLayout>
In your adapter change this:
#Override
public long getItemId(int position) {
return position;
}
And, also in your getView() method, put this code:
converView = null;
before :
if(convertView == null){
// your codes
}
For some reason tapping a row in my list view doesn't seem to work even if I have the correct listener code. There are only textviews in the template of the lists. I know there's been discussion about assigning a listener when there is a button in the template of a list view. see here.
Here is my code:
ScheduleActivity:
public class ScheduleActivity extends AppCompatActivity {
private String url;
JSONObject data = null;
Toolbar toolbar;
Intent intent;
String userId;
int eventId;
ListView scheduleListView;
ScheduleAdapter scheduleAdapter;
ArrayList<Schedule> scheduleList = new ArrayList<>();
DBManager dbManager = new DBManager(ScheduleActivity.this);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.schedule_layout);
Log.d("Test", ">>>ScheduleActivity<<<");
Bundle extras = getIntent().getExtras();
userId = extras.getString("userId");
eventId = extras.getInt("eventId");
toolbar = (Toolbar) findViewById(R.id.schedule_toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Schedules");
scheduleList = dbManager.getSchedules(eventId);
scheduleAdapter = new ScheduleAdapter(ScheduleActivity.this, scheduleList);
scheduleListView = (ListView) findViewById(R.id.scheduleListView);
scheduleListView.setAdapter(scheduleAdapter);
scheduleListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.w("Test", scheduleList.get(position).toString());
intent = new Intent(ScheduleActivity.this, UpdateScheduleActivity.class);
intent.putExtra("userId", userId);
intent.putExtra("eventId", eventId);
startActivity(intent);
}
});
scheduleListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Log.w("Test", "Long click works");
return true;
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.schedule_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.new_schedule) {
intent = new Intent(ScheduleActivity.this, CreateScheduleActivity.class);
intent.putExtra("eventId", eventId);
startActivity(intent);
}else if(id == R.id.rankings){
Log.d("Test", "Rankings Activity clicked!");
}
return super.onOptionsItemSelected(item);
}
ScheduleAdapter:
public class ScheduleAdapter extends ArrayAdapter<Schedule>{
public ScheduleAdapter(Context context, List<Schedule> schedule) {
super(context, R.layout.schedule_list_row, schedule);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater eventInflater = LayoutInflater.from(getContext());
if (convertView == null) {
convertView = eventInflater.inflate(R.layout.schedule_list_row, parent, false);
}
Schedule singleSchedule = getItem(position);
//TODO implement ViewHolder pattern
TextView club1Code = (TextView) convertView.findViewById(R.id.club1TextView);
TextView club2Code= (TextView) convertView.findViewById(R.id.club2TextView);
TextView club1Score = (TextView) convertView.findViewById(R.id.club1ScoreTextView);
TextView club2Score = (TextView) convertView.findViewById(R.id.club2ScoreTextView);
TextView club1SpiritScore = (TextView) convertView.findViewById(R.id.club1SpiritScoreTextView);
TextView club2SpiritScore = (TextView) convertView.findViewById(R.id.club2SpiritScoreTextView);
TextView time = (TextView) convertView.findViewById(R.id.timeTextView);
TextView day = (TextView) convertView.findViewById(R.id.dayTextView);
club1Code.setText(singleSchedule.getClub1Id());
club2Code.setText(singleSchedule.getClub2Id());
day.setText("Day " + singleSchedule.getDay());
time.setText(singleSchedule.getStartTime() + " - " + singleSchedule.getEndTime());
club1Score.setText(Integer.toString(singleSchedule.getClub1Score()));
club2Score.setText(Integer.toString(singleSchedule.getClub2Score()));
club1SpiritScore.setText(Integer.toString(singleSchedule.getClub1SpiritScore()));
club2SpiritScore.setText(Integer.toString(singleSchedule.getClub2SpiritScore()));
return convertView;
}
}
Activity Layout:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.kennanseno.ultimate_scoreboard_app.Activity.ScheduleActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/schedule_toolbar"
android:minHeight="?attr/actionBarSize"
android:background="#2196F3"
android:title="#string/event_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true">
</android.support.v7.widget.Toolbar>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/scheduleListView"
android:layout_centerHorizontal="true"
android:layout_below="#+id/schedule_toolbar" />
</RelativeLayout>
Row Template:
<?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:clickable="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/club1_name_text"
android:id="#+id/club1TextView"
android:textAlignment="textStart"
android:layout_above="#+id/club1SpiritScoreTextView"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/club2_name_text"
android:id="#+id/club2TextView"
android:layout_gravity="right"
android:textAlignment="textEnd"
android:layout_below="#+id/dayTextView"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/score_divider"
android:id="#+id/score_divider"
android:textSize="50sp"
android:layout_below="#+id/timeTextView"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/club1_score_text"
android:id="#+id/club1ScoreTextView"
android:textSize="50sp"
android:layout_marginEnd="22dp"
android:layout_below="#+id/timeTextView"
android:layout_toStartOf="#+id/score_divider"
android:textAlignment="center" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/club2_score_text"
android:id="#+id/club2ScoreTextView"
android:textSize="50sp"
android:layout_marginStart="22dp"
android:layout_below="#+id/timeTextView"
android:layout_toEndOf="#+id/score_divider"
android:textAlignment="center" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/start_time_text"
android:id="#+id/timeTextView"
android:textAlignment="center"
android:layout_below="#+id/dayTextView"
android:layout_alignEnd="#+id/club2ScoreTextView"
android:layout_alignStart="#+id/club1ScoreTextView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/club1_spirit_score_text"
android:id="#+id/club1SpiritScoreTextView"
android:textAlignment="textStart"
android:textSize="30sp"
android:layout_alignTop="#+id/club2SpiritScoreTextView"
android:layout_alignStart="#+id/club1TextView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/club2_spirit_score_text"
android:id="#+id/club2SpiritScoreTextView"
android:textAlignment="textEnd"
android:textSize="30sp"
android:layout_below="#+id/club2TextView"
android:layout_alignEnd="#+id/club2TextView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/schedule_day_text"
android:id="#+id/dayTextView"
android:textAlignment="center"
android:layout_alignParentTop="true"
android:layout_alignEnd="#+id/club2ScoreTextView"
android:layout_alignStart="#+id/club1ScoreTextView" />
</RelativeLayout>
In Row Template xml:
android:clickable="true"
causing issue because RelativeLayout is clickable with match_parent height-width.
if you want to get click event for whole row then no need to make parent layout clickable just set setOnItemClickListener to ListView otherwise you need to set onClickListener for RelativeLayout in ScheduleAdapter class.
I want to change the button of android grid view row item, when I click in the first row, I can change the button (play) to (pause), then when I go to another row, also I can change the play to pause, but I need to change the previous row automatically to pause, because it is now still play.
I have tried the code below but not effect.
Main Grid XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<GridView
android:id="#+id/gridView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:numColumns="1"
android:horizontalSpacing="16dp"
android:verticalSpacing="16dp"
android:padding="16dp"
android:clipToPadding="false"/>
</RelativeLayout>
Row XML
<?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="wrap_content">
<ImageView
android:id="#+id/imageViewMain"
android:layout_width="120dp"
android:layout_height="150dp"
android:scaleType="fitXY"
android:src="#drawable/img4" />
<TextView
android:id="#+id/textViewMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/imageViewMain"
android:layout_margin="5dp"
android:layout_toRightOf="#+id/imageViewMain"
android:text="PRİNCESS BİRTHDAY"
android:textColor="#000"
android:textSize="24dp" />
<TextView
android:id="#+id/textViewYazar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#412204"
android:textStyle="bold"
android:textSize="10sp"
android:text="Yazar : "
android:layout_below="#+id/textViewMain"
android:layout_alignLeft="#+id/textViewMain"
android:layout_alignStart="#+id/textViewMain" />
<TextView
android:id="#+id/textViewYazarName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yazar adı"
android:textColor="#412204"
android:textSize="10sp"
android:layout_below="#+id/textViewMain"
android:layout_toRightOf="#+id/textViewYazar"
android:layout_toEndOf="#+id/textViewYazar"
android:layout_marginLeft="26dp"/>
<TextView
android:id="#+id/textViewSeslendiren"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#412204"
android:textStyle="bold"
android:textSize="10sp"
android:text="Seslendiren : "
android:layout_below="#+id/textViewYazar"
android:layout_alignLeft="#+id/textViewMain"
android:layout_alignStart="#+id/textViewMain" />
<TextView
android:id="#+id/textViewSeslendirenName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Seslendiren Adı"
android:textColor="#412204"
android:textSize="10sp"
android:layout_below="#+id/textViewYazarName"
android:layout_toRightOf="#+id/textViewYazar"
android:layout_alignStart="#+id/textViewYazarName"/>
<TextView
android:id="#+id/textViewSizeDuration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#412204"
android:textStyle="bold"
android:textSize="10sp"
android:text="120 MB / 130 Mins"
android:layout_below="#+id/textViewSeslendirenName"
android:layout_alignLeft="#+id/textViewMain"
android:layout_alignStart="#+id/textViewMain" />
<Button
android:id="#+id/buttonPlay"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_above="#+id/imageViewBottomLine"
android:layout_alignLeft="#+id/textViewMain"
android:background="#drawable/ic_action_play"/>
<ImageView
android:id="#+id/imageViewTL"
android:layout_width="20dp"
android:layout_height="20dp"
android:src="#drawable/tl_simgesi"
android:layout_alignTop="#+id/textViewPrice"
android:layout_toLeftOf="#+id/textViewPrice"/>
<TextView
android:id="#+id/textViewPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2.70"
android:textColor="#E85404"
android:textSize="18dp"
android:textStyle="bold"
android:layout_marginRight="50dp"
android:layout_marginBottom="10dp"
android:layout_above="#+id/imageViewBottomLine"
android:layout_alignParentRight="true" />
<Button
android:id="#+id/buttonAdd"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/ic_action_basket_dark"
android:layout_above="#+id/imageViewBottomLine"
android:layout_alignParentRight="true"
android:onClick="onAddToCard"/>
<ImageView
android:id="#+id/imageViewBottomLine"
android:layout_width="wrap_content"
android:layout_height="1dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignBottom="#+id/imageViewMain"
android:scaleType="fitXY"
android:src="#drawable/top_line" />
</RelativeLayout>
View Adaptor GetView Code
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final ViewHolder holder;
LayoutInflater inflator = activity.getLayoutInflater();
imageLoader = new ImageLoader(activity.getApplicationContext());
if (convertView == null) {
holder = new ViewHolder();
convertView = inflator.inflate(R.layout.gridrow2, null);
holder.imgViewMain = (ImageView) convertView.findViewById(R.id.imageViewMain);
holder.txtViewTitle = (TextView) convertView.findViewById(R.id.textViewMain);
holder.txtViewPrice = (TextView) convertView.findViewById(R.id.textViewPrice);
holder.txtViewCast = (TextView) convertView.findViewById(R.id.textViewSeslendirenName);
holder.txtViewAuthor = (TextView) convertView.findViewById(R.id.textViewYazarName);
holder.txtViewSizeDuration = (TextView) convertView.findViewById(R.id.textViewSizeDuration);
holder.play = (Button) convertView.findViewById(R.id.buttonPlay);
holder.addcart = (Button) convertView.findViewById(R.id.buttonAdd);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
imageLoader.DisplayImage(urlList.get(position), holder.imgViewMain);
if (title.get(position).length() > 30) {
holder.txtViewTitle.setTextSize(20);
}
holder.txtViewTitle.setText(title.get(position));
holder.txtViewPrice.setText(price.get(position).replaceAll(" TL", ""));
holder.txtViewCast.setText(cast.get(position));
holder.txtViewAuthor.setText(author.get(position));
holder.txtViewSizeDuration.setText(size.get(position) + " / " + duration.get(position));
holder.imgViewMain.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent in = new Intent(activity.getApplicationContext(), SubMenu.class);
in.putExtra("Title", title.get(position));
in.putExtra("Id", id.get(position));
in.putExtra("Author", author.get(position));
in.putExtra("Description", description.get(position));
in.putExtra("Img", urlList.get(position));
in.putExtra("Price", price.get(position));
in.putExtra("Size", size.get(position));
in.putExtra("Duration", duration.get(position));
in.putExtra("Publication", publication.get(position));
in.putExtra("Cast", cast.get(position));
activity.startActivity(in);
}
});
holder.play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (holder.click == 0) {
stopPlayer(v.getContext(), position, previousPosition);
productId = id.get(position);
PlayExecute(v.getContext());
v.setBackground(v.getContext().getResources().getDrawable(R.drawable.ic_action_pause));
holder.click = 1;
previousPosition = position;
} else {
stopPlayer(v.getContext(), position, previousPosition);
v.setBackground(v.getContext().getResources().getDrawable(R.drawable.ic_action_play));
holder.click = 0;
}
}
});
holder.addcart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.setBackground(v.getContext().getResources().getDrawable(R.drawable.ic_action_basket));
prfs = PreferenceManager.getDefaultSharedPreferences(activity);
productId = id.get(position);
AddExecute();
}
});
return convertView;
}
StopPlayer and change previous icon
public void stopPlayer(Context c, int current, int previous) {
System.out.println("current : " + current);
System.out.println("previous : " + previous);
if (mp != null && mp.isPlaying()) {
mp.stop();
}
if (current!=previous){
View v= this.getView(previousPosition, null, null);
Button b = (Button) v.findViewById(R.id.buttonPlay);
b.setBackground(c.getResources().getDrawable(R.drawable.ic_action_play));
this.notifyDataSetChanged();
mGridview.refreshDrawableState();
}
}
see the attached image please;
http://postimg.org/image/b3fbpc211/
Instead of doing this
View v= this.getView(previousPosition, null, null);
change your drawable on item click method
mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Button b = (Button) view.findViewById(R.id.buttonPlay);
b.setBackground(c.getResources().getDrawable(R.drawable.ic_action_play));
your_adapter.notifyDataSetChanged();
}
});
I think , this will work for you :)
I think of you question as a partial refresh one. We all run into partial refresh problem when dealing with ListView or GridView. As for your case, partial refresh may be like this:
private void refreshPartially(int position){
int firstVisiblePosition = listview.getFirstVisiblePosition();
int lastVisiblePosition = listview.getLastVisiblePosition();
if(position>=firstVisiblePosition && position<=lastVisiblePosition){
View view = listview.getChildAt(position - firstVisiblePosition);
if(view.getTag() instanceof ViewHolder){
ViewHolder vh = (ViewHolder)view.getTag();
//holder.play.setBackgroundResource(resId);//Do something here.
...
}
}
}
I have implemented a custom listview with checkboxes in the single choice mode of the android listview.I want a functionality such that when I click on the checkbox the row corresponding to that check box must get added(or rather say the "Person" object shown in that row should get added to another list and be removed from that list) and on itemclick on that listview should take me to another screen in android.I tried the other ways but they specify that the checkbox needs to be focasable:false.Also I want the click listener to work on the check box only.Please any suggestions or help on these.Thanks in advance
This id the xml code.
<?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" >
<CheckBox
android:id="#+id/projects_check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:focusable="false" />
<TextView
android:id="#+id/project_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/projects_check"
android:layout_alignBottom="#+id/projects_check"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_toEndOf="#+id/projects_check"
android:layout_toRightOf="#+id/projects_check"
android:text="TextView" />
</RelativeLayout>
And this is my onitem click listener code..
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long arg3) {
Projects info = (Projects) parent.getItemAtPosition(position);
info.toggleChecked();
ProjectsHolder viewHolder = (ProjectsHolder) view.getTag();
viewHolder.getmChoiceSelect().setChecked(info.isChecked());
if (viewHolder.getmChoiceSelect().isChecked()) {
completed_projects.add(info);
mCompProjAdapter.notifyDataSetChanged();
projects_list.remove(info);
mProjAdapter.notifyDataSetChanged();
}
}
To manage click onto the row itself, you can just write listener for listView itself.
This is how i manage checkbox click.
class CustomAdapter extends ArrayAdapter<BeanClass> {
private ArrayList<BeanClass> items;
public FieldPlannedAdapter(Context context,
ArrayList<BeanClass> items) {
super(context, R.layout.custom_list_row, items);
this.items = items;
}
public class ViewHolder {
protected CheckBox checkbox;
protected TextView name;
}
#Override
public View getView(final int position, View convertView,
ViewGroup parent) {
final ViewHolder viewHolder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) getActivity()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.custom_list_row,
null);
viewHolder = new ViewHolder();
viewHolder.checkbox = (CheckBox) convertView
.findViewById(R.id.checkIsMissed);
viewHolder.name = (TextView) convertView
.findViewById(R.id.txtViewMTPname);
convertView.setTag(viewHolder);
viewHolder.checkbox
.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final CheckBox cb = (CheckBox) v;
final BeanClass item = (BeanClass) cb
.getTag();
item.setSelected(cb.isChecked());
if (cb.isChecked()) {
// DO SOMETHING
} else {
}
}
});
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
BeanClass item = items.get(position);
viewHolder.name.setTag(item);
viewHolder.checkbox.setTag(item);
viewHolder.name.setText(item.getName());
viewHolder.checkbox.setChecked(item.isSelected());
if (viewHolder.checkbox.isChecked()) {
// DO SOMETHING
} else {
// DO SOMETHING
}
return convertView;
}
}
Custom row example :
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp" >
<LinearLayout
android:id="#+id/row"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/card_ui"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp" >
<TextView
android:id="#+id/txtViewMTPname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Customer Name"
android:textColor="#drawable/text_selector"
android:textSize="14sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/txtViewMtpDarCity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="dgdgdgdg"
android:textColor="#drawable/text_selector"
android:textSize="16sp"
android:textStyle="italic" />
<CheckBox
android:id="#+id/checkIsMissed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:gravity="center"
android:text="Missed" />
</LinearLayout>
</LinearLayout>
</FrameLayout>
i have an android program that simulate chat. for it i use a list and items of list are in a XML file named activity_chat_conversation in this layout i have to linear layout that in each of them i have a EditText and an ImageView.
code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/llmain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:ignore="UseCompoundDrawables" >
<LinearLayout
android:id="#+id/llFrom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="7dp"
android:visibility="gone" >
<ImageView
android:id="#+id/imgChatConversationFrom"
android:layout_width="32dp"
android:layout_height="32dp"
android:background="#color/gray"
android:contentDescription="#string/user_image"
android:scaleType="fitCenter"
android:src="#drawable/ic_user_image" />
<TextView
android:id="#+id/txtChatConversationFrom"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:background="#drawable/from"
android:gravity="right|top"
android:layoutDirection="rtl"
android:paddingLeft="45dp"
android:paddingRight="10dp"
android:text="#string/user_name"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textDirection="rtl" />
</LinearLayout>
<LinearLayout
android:id="#+id/llTo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="7dp"
android:visibility="gone" >
<TextView
android:id="#+id/txtChatConversationTo"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:background="#drawable/to"
android:gravity="right|top"
android:layoutDirection="rtl"
android:paddingRight="48dp"
android:paddingTop="5dp"
android:text="#string/user_name"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textDirection="rtl" />
<ImageView
android:id="#+id/imgChatConversationTo"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_gravity="bottom"
android:background="#color/gray"
android:contentDescription="#string/user_image"
android:scaleType="fitCenter"
android:src="#drawable/ic_user_image" />
</LinearLayout>
for filling list i create an adapter and in it i set what linear layout should be shown.
code:
globalVars.barConversation = new BaseAdapter() {
int searchUser = globalVars.findConversationByUsername(friendId);
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if(convertView == null) {
view = getLayoutInflater().inflate(R.layout.activity_chat_conversation, null);
}
LinearLayout llfrom = (LinearLayout)view.findViewById(R.id.llFrom);
LinearLayout llto = (LinearLayout)view.findViewById(R.id.llTo);
TextView textViewTo = (TextView)view.findViewById(R.id.txtChatConversationTo);
ImageView imageViewTo = (ImageView)view.findViewById(R.id.imgChatConversationTo);
TextView textViewFrom = (TextView)view.findViewById(R.id.txtChatConversationFrom);
ImageView imageViewFrom = (ImageView)view.findViewById(R.id.imgChatConversationFrom);
String pmUser = friendId;
if (searchUser > -1) {
if (globalVars.conversations.get(searchUser).
getConversation().get(position).getSender()) {
pmUser = CurrentUser;
if (globalVars.userImage != null) {
imageViewTo.setImageBitmap(globalVars.userImage);
}
}else {
try{
if(globalVars.friends.get(friendInt).getImage() != null) {
imageViewFrom.setImageBitmap(globalVars.friends.get(friendInt).getImage());
}
}
catch(Exception ex)
{
Log.e("Chat get friend picture", ex.toString());
}
}
if (!globalVars.conversations.get(searchUser).
getConversation().get(position).getIsRead()) {
globalVars.conversations.get(searchUser).
getConversation().get(position).setIsRead(true);
globalVars.newConversations--;
}
if(pmUser == CurrentUser)
{
llto.setVisibility(0);
llfrom.setVisibility(8);
textViewFrom.setVisibility(8);
imageViewFrom.setVisibility(8);
textViewTo.setText(globalVars.conversations.get(searchUser).
getConversation().get(position).getText());
textViewTo.setHeight(30);
}
else
{
llfrom.setVisibility(0);
llto.setVisibility(8);
textViewTo.setVisibility(8);
imageViewTo.setVisibility(8);
textViewFrom.setText(globalVars.conversations.get(searchUser).
getConversation().get(position).getText());
}
}
return view;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public int getCount() {
if (searchUser > -1) {
return globalVars.conversations.get(searchUser).getConversation().size();
}
return 0;
}
};
when i add new item to list and set visibility for it previous items visibility will be changed.
how i can solve this problem ?
It happens because convertView == previously getView() method. You need to refresh parameters for Your new item. For example:
if(pmUser == CurrentUser)
{
llto.setVisibility(0);
textViewTo.setVisibility(0);
imageViewTo.setVisibility(0); //this
llfrom.setVisibility(8);
textViewFrom.setVisibility(8);
imageViewFrom.setVisibility(8);
textViewTo.setText(globalVars.conversations.get(searchUser).
getConversation().get(position).getText());
textViewTo.setHeight(30);
}
else
{
llfrom.setVisibility(0);
textViewFrom.setVisibility(0);
imageViewFrom.setVisibility(0); // this
llto.setVisibility(8);
textViewTo.setVisibility(8);
imageViewTo.setVisibility(8);
textViewFrom.setText(globalVars.conversations.get(searchUser).
getConversation().get(position).getText());
}