adding space to make each list item bigger - java

My app that I am creating has two listviews. My second listview does not look the same as my second one. The lines that seperate each item are too close to the text, but I want them to have space on both the top and bottom, like the first list. I have tried to change the padding and the margins but that does not works and I have also tried to change it in one of the Java classes but I get error messages.
Here are two pictures for comparison(The first list is on the left and the second list is on the right)
The second list:
<ListView
android:id="#+id/countryselector"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_above="#+id/checkItem"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="60dp"
android:clickable="false"/>
<TextView
android:id="#+id/checkboxtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/emptyElement"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="NO ITEM AVAILABLE!"
android:textColor="#525252"
android:textSize="19.0sp"
android:visibility="gone" />
<Button
android:id="#+id/checklist"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentTop="true"
android:gravity="end"
android:layout_alignParentLeft="true"
android:text=""
android:background="#mipmap/arrow2"
My first list:
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="left"
android:text="Favorites"
android:textColor="#222"
android:textSize="15dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="2dp"
android:textStyle="normal" />
<Button
android:id="#+id/tofavorites"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentTop="true"
android:gravity="end"
android:layout_alignParentLeft="true"
android:text=""
android:background="#mipmap/arrow2" />
<ListView
android:layout_marginTop="60dp"
android:id="#+id/countrieselector"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:clickable="false" />
<TextView
android:id="#+id/checkboxtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
Here Is my adapter class for the second list:
public class AdapterFavorite extends BaseAdapter {
ArrayList<String> items;
ArrayList<String> items1;
LayoutInflater inflater;
ArrayList selectedItems;
List<Item> dummyItems;
ArrayList<Integer> itemsValue = new ArrayList<>();
Activity activity;
DbUtility dbUtility;
Context context;
AdapterFavorite(Activity activity, ArrayList<String> items, Context context) {
this.items = items;
inflater = activity.getLayoutInflater();
selectedItems = new ArrayList<String>();
this.activity = activity;
this.context = context;
dbUtility = new DbUtility(activity);
}
#Override
public int getCount() {
return items.size();
}
#Override
public String getItem(int i) {
return items.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(final int i, View view, ViewGroup viewGroup) {
dbUtility.open();
View v = view;
if (v == null) {
v = inflater.inflate(R.layout.checkboxlayoutfavorite, null);
}
TextView textview_countries = (TextView) v.findViewById(R.id.Textview_languages);
Button button = (Button) v.findViewById(R.id.checkbox);
items1 = new ArrayList<>();
textview_countries.setText(items.get(i));
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
////Where I tried to add/move items when the checkbox is clicked
dbUtility.delete_byID(items.get(i));
items.remove(i);
notifyDataSetChanged();
}
});
return v;
}
}
Here is my main Java class for the second listL:
ublic class FavoriteActivity extends AppCompatActivity {
DbUtility dbUtility;
List<Item> dummyItems = new ArrayList<>();
Button mainList;
KeyboardPrefManager keyboardPrefManager;
ArrayList<String> items;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
ListView chl = (ListView) findViewById(R.id.languageselector);
mainList = (Button) findViewById(R.id.checklist);
chl.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
items = new ArrayList<>();
dbUtility = new DbUtility(this);
dbUtility.open();
dummyItems = dbUtility.getAllContacts();
keyboardPrefManager = new KeyboardPrefManager(this);
for (int a = 0; a < dummyItems.size(); a++) {
items.add(dummyItems.get(a).getValue());
}
mainList.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(FavoriteActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
});
AdapterFavorite adapter = new AdapterFavorite(this, items, this);
chl.setAdapter(adapter);
chl.setEmptyView(findViewById(R.id.emptyElement));
chl.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String item = items.get(i); // Getting the dummy item from the List with position i
Toast.makeText(FavoriteActivity.this, "Clicked on " + item, Toast.LENGTH_SHORT).show();
keyboardPrefManager.selectKeyboard(item);
}
});}}

Please check your list item xml for both list same or not.Thanks

Related

ViewPager inside Dialog not showing

I have a custom dialog that has a ViewPager inside of it, when the dialog shows the ViewPager is just blank, not progressing on swipe or when pressing the "Next" button I implemented. I tried slightly altering my code and it didn't work. I saw several posts like this, but none of their solutions worked. PS if some things don't make sense or have mismatched names then that's because I renamed/removed some of the files/variables to simplify.
SliderAdapter:
public class SliderAdapter extends PagerAdapter {
private Context context;
private LayoutInflater layoutInflater;
private ArrayList<String> text;
public SliderAdapter(Context context, ArrayList<String> text) {
this.context = context;
this.text = text;
}
public String[] txtH = {
"test1",
"test2",
"test3"
};
#Override
public int getCount() {
return txtH.length;
}
#Override
public boolean isViewFromObject(#NonNull View view, #NonNull Object object) {
return view == (ConstraintLayout) object;
}
#NonNull
#Override
public Object instantiateItem(#NonNull ViewGroup container, int position) {
layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.slide_layout_wr, container, false);
TextView txt1 = view.findViewById(R.id.txt11);
TextView txt2 = view.findViewById(R.id.txt22);
txt1.setText(txtH[position]);
txt2.setText(text.get(position));
container.addView(view);
return view;
}
#Override
public void destroyItem(#NonNull ViewGroup container, int position, #NonNull Object object) {
container.removeView((ConstraintLayout) object);
}
}
Dialog itself:
public class DialogWeeklyReport extends AppCompatDialogFragment {
...
#NonNull
#Override
public Dialog onCreateDialog(#Nullable Bundle savedInstanceState) {
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(),
R.style.Dialog);
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.dialog, null);
preferences = getActivity().getSharedPreferences("label", 0);
Random random = new Random();
text.add("test1");
text.add("test2");
text.add("test3");
viewPager = view.findViewById(R.id.viewPager);
dotLayout = view.findViewById(R.id.dotLayout);
next = view.findViewById(R.id.next);
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (next.getText().toString().equals("Proceed")) {
dismiss();
} else {
viewPager.setCurrentItem(currentPage + 1);
}
}
});
back = view.findViewById(R.id.back);
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
viewPager.setCurrentItem(currentPage--);
}
});
builder.setView(view)
.setCancelable(true);
addDotsIndicator(0);
viewPager.setOnPageChangeListener(viewListener);
adapter = new SliderAdapter(getActivity(), text);
return builder.create();
}
private void addDotsIndicator(int position) {
dots = new TextView[3];
dotLayout.removeAllViews();
for (int i = 0; i<dots.length; i++) {
dots[i] = new TextView(getActivity());
dots[i].setText(Html.fromHtml("&#8226"));
dots[i].setTextSize(35);
dots[i].setTextColor(Color.parseColor("#404040"));
dotLayout.addView(dots[i]);
}
if(dots.length > 0) {
dots[position].setTextColor(Color.BLACK);
}
if (currentPage == 0) {
back.setEnabled(false);
next.setEnabled(true);
back.setText("");
next.setText("Next");
}
}
ViewPager.OnPageChangeListener viewListener = new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
addDotsIndicator(position);
currentPage = position;
if (currentPage == 0) {
back.setEnabled(false);
next.setEnabled(true);
back.setText("");
next.setText("Next");
} else if (currentPage == 1) {
back.setEnabled(true);
next.setEnabled(true);
back.setText("Back");
next.setText("Next");
} else if (currentPage == 2) {
back.setEnabled(true);
next.setEnabled(false);
back.setText("Back");
next.setText("Proceed");
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
};
}
Dialog XML:
<RelativeLayout
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:background="#drawable/dialog_bg"
app:cardCornerRadius="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<androidx.viewpager.widget.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:id="#+id/dotLayout"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_centerHorizontal="true"
android:layout_margin="15dp"
android:layout_below="#+id/viewPager"
android:orientation="horizontal" />
<Button
android:id="#+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_below="#+id/viewPager"
android:background="#android:color/transparent"
android:elevation="0dp"
android:text="Back"
android:textColor="#android:color/black" />
<Button
android:id="#+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_below="#+id/viewPager"
android:layout_margin="5dp"
android:background="#android:color/transparent"
android:elevation="0dp"
android:text="Next"
android:textColor="#android:color/black" />
</RelativeLayout>
ViewPager's slide layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"
android:background="#android:color/white">
<TextView
android:id="#+id/txt11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEST"
android:textColor="#android:color/black"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.24000001" />
<TextView
android:id="#+id/txt22"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Test"
android:textColor="#android:color/black"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="#+id/txt11"
app:layout_constraintStart_toStartOf="#+id/txt11"
app:layout_constraintTop_toBottomOf="#+id/txt11"
app:layout_constraintVertical_bias="0.26" />
</androidx.constraintlayout.widget.ConstraintLayout>
The problem is that you didn't set the ViewPager adapter
public class DialogWeeklyReport extends AppCompatDialogFragment {
...
#NonNull
#Override
public Dialog onCreateDialog(#Nullable Bundle savedInstanceState) {
...
viewPager = view.findViewById(R.id.viewPager);
...
adapter = new SliderAdapter(getActivity(), text);
viewPager.setAdapter(adapter); // <<<<<< change here
return builder.create();
}
...
Here is my test

Android Pagination ListView

Hi i have trouble in achieving pagination for listview in android. My task is to add Values from editTextto ListView and i need to add pagination to the list. But I tried and i am to insert only one value . While i try to add next values i end up in errors. Please kindly tell me the error on my code. i have added my layout,Activity and Log
MainActivity.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"
android:background="#drawable/agnes2_back"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.vivek.projectone.MainActivity" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#drawable/border" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="65dp"
android:layout_marginTop="15dp"
android:gravity="right"
android:text="#string/welcome"
android:textColor="#58FA58" />
<TextView
android:id="#+id/userView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:layout_marginTop="15dp"
android:text="#string/userLabel"
android:textColor="#FF0000" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearlayout2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignLeft="#+id/linearLayout1"
android:layout_below="#+id/linearLayout1"
android:layout_marginTop="14dp"
android:background="#drawable/border"
android:orientation="horizontal" >
<EditText
android:id="#+id/itemName"
android:layout_width="199dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
<Button
android:id="#+id/addButton1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/adds"
android:text="" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout3"
android:layout_width="match_parent"
android:layout_height="210dp"
android:layout_alignLeft="#+id/linearlayout2"
android:layout_below="#+id/linearlayout2"
android:layout_marginTop="16dp"
android:background="#drawable/border"
android:orientation="horizontal" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="184dp"
android:layout_weight="2.32"
android:background="#android:color/transparent"
android:cacheColorHint="#android:color/transparent"
tools:listitem="#android:layout/simple_list_item_checked" >
</ListView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignLeft="#+id/linearLayout3"
android:layout_alignParentBottom="true"
android:layout_below="#+id/linearLayout3"
android:orientation="horizontal" >
<Button
android:id="#+id/btn_Prev"
style="?android:attr/buttonStyleSmall"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_marginLeft="35dp"
android:layout_marginTop="10dp"
android:background="#drawable/buttonbackground"
android:text="#string/btn_prev" />
<Button
android:id="#+id/btn_Next"
style="?android:attr/buttonStyleSmall"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="10dp"
android:background="#drawable/buttonbackground"
android:text="#string/btn_next" />
</LinearLayout>
</RelativeLayout>
MainActivity.java
public class MultipleActivity extends Activity implements OnItemClickListener {
Button addToList;
EditText viewListItem1, viewListItem2;
ListView customItemList;
PackageManager packageManager;
ArrayList<String> checkedCustomItem;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_multiple);
addToList = (Button) findViewById(R.id.adToListBtn);
viewListItem1 = (EditText) findViewById(R.id.viewEditItem1);
viewListItem2 = (EditText) findViewById(R.id.viewEditItem2);
packageManager = getPackageManager();
final List<PackageInfo> packageList = packageManager
public class MainActivity extends Activity {
String userLabel;
EditText itemName;
Button addBut;
Button multipleBtn;
ListView itemList;
private ArrayList<String> itemAList;
ArrayAdapter<String> itemAdapter;
private int pageCount;
private Button buttonPrev;
private Button buttonNext;
private int increment = 0;
public int TOTAL_LIST_ITEMS = 1030;
public int NUM_ITEMS_PAGE = 5;
String item;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = (TextView) findViewById(R.id.userView);
itemName = (EditText) findViewById(R.id.itemName);
addBut = (Button) findViewById(R.id.addButton1);
buttonNext = (Button) findViewById(R.id.btn_Next);
buttonPrev = (Button) findViewById(R.id.btn_Prev);
itemList = (ListView) findViewById(R.id.listView1);
buttonPrev.setEnabled(false);
multipleBtn = (Button) findViewById(R.id.multipleValsBtn);
int val = TOTAL_LIST_ITEMS % NUM_ITEMS_PAGE;
val = val == 0 ? 0 : 1;
pageCount = TOTAL_LIST_ITEMS / NUM_ITEMS_PAGE + val;
Intent intent = getIntent();
userLabel = intent.getExtras().getString("emailID");
textView.setText(userLabel);
itemAList = new ArrayList<>();
itemAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, itemAList);
itemList.setAdapter(itemAdapter);
itemList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView parent, View v, int arg2,
long arg3) {
// TODO Auto-generated method stub
item = itemAList.get(arg2);
Toast.makeText(getApplicationContext(), item, 0).show();
}
});
buttonNext.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
increment++;
loadList(increment);
CheckEnable();
}
});
buttonPrev.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
increment--;
loadList(increment);
CheckEnable();
}
});
addBut.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
item = itemName.getText().toString();
itemAList.add(0, item);
loadList(0);
itemName.setText("");
}
});
multipleBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(MainActivity.this,
MultipleActivity.class);
startActivity(intent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void CheckEnable() {
if (increment + 1 == pageCount) {
buttonNext.setEnabled(false);
} else if (increment == 0) {
buttonPrev.setEnabled(false);
} else {
buttonPrev.setEnabled(true);
buttonNext.setEnabled(true);
}
}
private void loadList(int number) {
ArrayList<String> sort = new ArrayList<String>();
int start = number * NUM_ITEMS_PAGE;
for (int i = start; i < (start) + NUM_ITEMS_PAGE; i++) {
if (i < itemAList.size()) {
sort.add(itemAList.get(i));
} else {
break;
}
}
itemAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, sort);
itemList.setAdapter(itemAdapter);
}
}
You need to use setAdapter only once. For changing the list item next time (for loading next 5 items), you just need to use next notifyDataSetChanged
private void loadList(int number) {
ArrayList<String> sort = new ArrayList<String>();
int start = number * NUM_ITEMS_PAGE;
for (int i = start; i < (start) + NUM_ITEMS_PAGE; i++) {
if (i < itemAList.size()) {
sort.add(itemAList.get(i));
} else {
break;
}
}
if(itemAdapter ==null){
itemAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, sort);
itemList.setAdapter(itemAdapter);
}
else{
itemAdapter.notifyDataSetChanged();
}
}
Though I didn't get what you are doing, but try to do this change:
Remove this line
itemAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, sort);
or Add add one more line below this line
itemList.setAdapter(itemAdapter);
Because you have already called :itemAdapter.notifyDataSetChanged(); it should update the list.

android : marquee on listview don't work

I write simple ListView but I have problem, I want to marquee text when I select some item from my listview but it not working I don't know why I think I write code correctly when I copy my code from listView.setOnItemSelectedListener(new OnItemSelectedListener() to listView.setOnItemClickListener(new OnItemClickListener() that's work correctly when I click, but I want this only for select.
That's my code :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
List<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < 7; ++i) {
HashMap<String, String> hm = new HashMap<String, String>();
hm.put("firstLine", menu[i]);
hm.put("secondLine", submenu[i]);
hm.put("icon", Integer.toString(ikons[i]));
aList.add(hm);
}
String[] from = { "icon", "firstLine", "secondLine" };
int[] to = { R.id.icon, R.id.firstLine, R.id.secondLine };
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList,
R.layout.alarm_row, from, to);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);
listView.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View view,
int arg2, long arg3) {
TextView t = (TextView) view.findViewById(R.id.secondLine);
t.setSelected(true);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view, int arg2,
long arg3) {
// TODO Auto-generated method stub
/*
* TextView t = (TextView) view.findViewById(R.id.secondLine);
* t.setFocusable(true); t.setSelected(true);
*/
}
});
}
XML :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/panel1"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:background="#drawable/listview_bg"
android:padding="6dip" >
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignBottom="#+id/secondLine"
android:layout_alignParentTop="true"
android:scaleType="center"
android:src="#drawable/alarm_icon" />
<TextView
android:id="#+id/secondLine"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_toRightOf="#id/icon"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:paddingLeft="15dip"
android:paddingRight="15dip"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="Alarm" />
<TextView
android:id="#+id/firstLine"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#id/secondLine"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:layout_toRightOf="#id/icon"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:paddingLeft="15dip"
android:paddingRight="15dip"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="Ustawienia Alarmu" />
</RelativeLayout>
I think that should work I don't have any idea what is wrong, I will be very gratefull for any help.
Try adding
t.requestFocus();
after
t.setSelected(true);
in onItemSelected
you should define a customized adapter, which extends e.g. BaseAdapter or which adapter you like and then add t.setSelected(true) in the getView method of that:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (convertView == null)
view = inflater.inflate(R.layout.row, null);
TextView t = (TextView) view.findViewById(R.id.secondLine);
text2.setSelected(true);
return view;
}

customised listview using arrayadapter class in android

how to select row item using Tick mark like iphone in android?iam using imageview in list_row.xml.when i click the list row item then i show image in row imageview.
if(getItem(position)!=null){
img.setvisibilty(View.Visible);}
else{System.out.println("imagenull");}
iam using this but image display in last row only.please help me how to select item using tickmark image.
public class DistanceArrayAdapter extends ArrayAdapter<Constant>{
public static String category,state,miles;
public ImageView img;
private Context context;
private int current = -1;
ArrayList<Constant> dataObject;
public DistanceArrayAdapter(Context context, int textViewResourceId,
ArrayList<Constant> dataObject) {
super(context, textViewResourceId, dataObject);
this.context=context;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View rowView=convertView;
if(rowView==null){
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.category_row, parent, false);
}
//TextView textView = (TextView) rowView.findViewById(R.id.text1);
TextView textView1 = (TextView) rowView.findViewById(R.id.text2);
//textView.setText(""+getItem(position).id);
textView1.setText(""+getItem(position).caption);
img=(ImageView)rowView.findViewById(R.id.img);
img.setVisibility(View.GONE);
if(position%2==1)
{
rowView.setBackgroundResource(R.color.even_list);
}
else
{
rowView.setBackgroundResource(R.color.odd_list);
}
rowView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(img.getVisibility()==View.GONE)
{
img.setVisibility(View.VISIBLE);
System.out.println("1");
}
if(img.getVisibility()==View.VISIBLE){
img.setVisibility(View.GONE);
System.out.println("12");
}
miles=getItem(position).caption;
System.out.println("miles"+miles);
}
});
return rowView;
}
}
Drawing from https://groups.google.com/forum/?fromgroups#!topic/android-developers/No0LrgJ6q2M
public class MainActivity extends Activity implements AdapterView.OnItemClickListener {
String[] GENRES = new String[] {"Action", "Adventure", "Animation", "Children", "Comedy", "Documentary", "Drama", "Foreign", "History", "Independent", "Romance", "Sci-Fi", "Television", "Thriller"};
private CheckBoxAdapter mCheckBoxAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView listView = (ListView) findViewById(R.id.lv);
listView.setItemsCanFocus(false);
listView.setTextFilterEnabled(true);
listView.setOnItemClickListener(this);
mCheckBoxAdapter = new CheckBoxAdapter(this, GENRES);
listView.setAdapter(mCheckBoxAdapter);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
StringBuilder result = new StringBuilder();
for (int i = 0; i < GENRES.length; i++) {
if (mCheckBoxAdapter.mCheckStates.get(i) == true) {
result.append(GENRES[i]);
result.append("\n");
}
}
Toast.makeText(MainActivity.this, result, 1000).show();
}
});
}
public void onItemClick(AdapterView parent, View view, int position, long id) {
mCheckBoxAdapter.toggle(position);
}
class CheckBoxAdapter extends ArrayAdapter implements CompoundButton.OnCheckedChangeListener {
LayoutInflater mInflater;
TextView tv1, tv;
CheckBox cb;
String[] gen;
private SparseBooleanArray mCheckStates;
private SparseBooleanArray mCheckStates;
CheckBoxAdapter(MainActivity context, String[] genres) {
super(context, 0, genres);
mCheckStates = new SparseBooleanArray(genres.length);
mInflater = (LayoutInflater) MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
gen = genres;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return gen.length;
}
}
}
activity_main.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="#+id/lv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button1"/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Button" />
</RelativeLayout>
And the XML file for the checkboxes:
<?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" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="15dp"
android:layout_marginTop="34dp"
android:text="TextView" />
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/textView1"
android:layout_marginRight="22dp"
android:layout_marginTop="23dp" />
</RelativeLayout>
When you click the button a toast message with list of item choosen is displayed. You can modify the above according to your requirements.
Set selection mode on your ListView
//if using ListActivity or ListFragment
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
//or
myListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
//myListView is reference to your ListView
1.Make visibility gone to you tick mark image
2.Implement view.setOnClickListener in arrayadapter.
3.In that check image.getVisibility()==View.GONE then make image.setVisibity(View.Visible)
4.if image.getVisiblity()==View.VISIBLE then make your image.setVisibity(View.GONE)
Try this.

Android: Stuck on updating textView in custom adapter

I'm new to programming for Android (and Java) and have managed to get a custom adapter working, but am stuck on how to update textViews inside of it. Below you will see my score_save function with the code I want, but I'm not sure how to apply this to all rows of my adapter at once.
public class MainActivity extends Activity {
private ListView listView1;
private int currentPlayer;
private int currentScore;
ArrayList<Integer> allScoreChange = new ArrayList<Integer>();
ArrayList<Integer> allScore = new ArrayList<Integer>();
Button button_add, button_add_big, button_sub, button_sub_big,
button_add_num, button_save;
TextView name;
TextView score;
TextView scoreChange;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Score score_data[] = new Score[] { new Score("NameA"),
new Score("NameB"), new Score("NameC"), new Score("NameD"),
new Score("NameE") };
//sets score lists to 0
for (int i = 0; i < 5; i++) {
allScoreChange.add(0);
allScore.add(0);
}
ScoreAdapter adapter = new ScoreAdapter(this,
R.layout.listview_item_row, score_data);
listView1 = (ListView) findViewById(R.id.listView1);
listView1.setAdapter(adapter);
listView1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
currentScore = allScoreChange.get(position);
// Gets fields from current row
score = (TextView) view.findViewById(R.id.txtScore);
scoreChange = (TextView) view.findViewById(R.id.txtScoreChange);
currentPlayer = position;
}
});
button_save.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
score_save();
}
});
}
public void score_save() {
// this needs to go through all rows of listview
currentPlayer = 0;
// update array with new score
allScore.set(currentPlayer, allScore.get(currentPlayer)
+ allScoreChange.get(currentPlayer));
// display new score
score.setText(allScore.get(currentPlayer));
// reset score change to zero
allScoreChange.set(currentPlayer, 0);
// display score change as blank
scoreChange.setText("");
}
}
Here's the XML for the row, I want to update txtScoreChange and txtScore with the values out of the allScoreChange and allScore ArrayLists. I cut out the code that defines these lists, in case you're wondering.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:padding="10dp" >
<TextView
android:id="#+id/txtTitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:layout_weight="70"
android:text="Name"
android:textColor="#000000"
android:textSize="22sp"
android:textStyle="bold" />
<TextView
android:id="#+id/txtScoreChange"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginBottom="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_weight="15"
android:textColor="#ff0000"
android:textSize="22sp"
android:gravity="right" />
<TextView
android:id="#+id/txtScore"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginBottom="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_weight="15"
android:text="1"
android:textColor="#666666"
android:textSize="22sp"
android:gravity="right" />
Here's my adapter:
public class ScoreAdapter extends ArrayAdapter<Score>{
Context context;
int layoutResourceId;
Score data[] = null;
public ScoreAdapter(Context context, int layoutResourceId, Score[] 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;
ScoreHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ScoreHolder();
holder.txtScore = (TextView)row.findViewById(R.id.txtScore);
holder.txtScoreChange = (TextView)row.findViewById(R.id.txtScoreChange);
holder.txtName = (TextView)row.findViewById(R.id.txtName);
row.setTag(holder);
}
else
{
holder = (ScoreHolder)row.getTag();
}
Score scoreData = data[position];
holder.txtName.setText(scoreData.name);
holder.txtScore.setText(scoreData.score);
return row;
}
static class ScoreHolder
{
TextView txtScore;
TextView txtName;
TextView txtScoreChange;
}
}
Call adapter.notifyDataSetChanged(). That will cause it to repopulate the listview, calling adapter.getView on all visible views. Have getView() set the correct values for its row.

Categories