Android ListView add not clickable TextView - java

I have list view:
<ListView android:id="#+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
And I add some items to it:
final ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < items.length(); ++i) {
try {
list.add(items[i].getString("title"));
} catch (JSONException e) {
e.printStackTrace();
}
}
final StableArrayAdapter adapter = new StableArrayAdapter(this,
android.R.layout.simple_list_item_1, list);
listview.setAdapter(adapter);
But now I want to add some TextViews (at example if(i==5)) to the list, like a label. It should not be clickable.
How can I add it?

listview.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View view, int arg2,long arg3)
{
long pos = arg0.getItemIdAtPosition(arg2);
if(pos != 5)
{
//add textview
}
else
{
//nothing
}
}
});

You should use a custom row layout instead of android.R.layout.simple_list_item_1, for example a row.xml you have one text view whose visibility is set to gone by default:
<?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="wrap_content" >
<TextView
android:id="#+id/nonclickable_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"/>
<TextView
android:id="#+id/normal_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Put it in res/layout/ folder, then call R.layout.row instead of android.R.layout.simple_list_item_1 when you create your StableArrayAdapter. In the getView() method
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
if(convertView==null) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row, parent,false);
}
if(int position==5) {
TextView tv = (TextView)convertView.findViewById(R.id.nonclickable_text);
tv.setVisibility(View.VISIBLE);
}

Related

duplicating all activity instead of [check boxes and text only] (To do list android app)

I'm trying to make to-do list app. the design here "https://i.ibb.co/cXnQ9dP/Capture.png"
I got box and text code from "https://www.vogella.com/tutorials/AndroidListView/article.html" proj (15.1. Interaction between the model and Listview) but I put the XML code into my existing XML activity [not in a new activity] and changed activity name in super(context, R.layout.rowbuttonlayout, list); and view = inflator.inflate(R.layout.rowbuttonlayout, null); to my main activity name
<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="match_parent"
tools:context="com.example.mando.todolist.new_list">
.............background and buttons code here............
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignStart="#id/plus"
android:orientation="horizontal"
android:paddingTop="205dp"
android:id="#+id/repeat_layout"
>
<CheckBox
android:id="#+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="4px"
android:layout_marginRight="10px" >
</CheckBox>
<TextView
android:id="#+id/label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#+id/label"
android:textSize="50px"
android:layout_alignRight="#id/check"
>
</TextView>
</LinearLayout>
public InteractiveArrayAdapter(Activity context, List<Model> list) {
super(context, R.layout.activity_new_list, list);
this.context = context;
this.list = list;
}
static class ViewHolder {
protected TextView text;
protected CheckBox checkbox;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.activity_new_list.,null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) view.findViewById(R.id.label);
viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
viewHolder.checkbox
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
Model element = (Model) viewHolder.checkbox
.getTag();
element.setSelected(buttonView.isChecked());
}
});
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 new_list extends ListActivity {
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
// create an array of Strings, that will be put to our ListActivity
ArrayAdapter<Model> 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"));
// Initially select one of the items
list.get(1).setSelected(true);
return list;
}
private Model get(String s) {
return new Model(s);
}
}
The output is All activity has been duplicated [including background and buttons] instead duplicating checkbox with text only in the same activity
Move your background and buttons to the xml file where you put your ListView
it should look like this, because the item xml you are using where your CheckBox exists is the one used for the ListView, if you but the button it will duplicated too
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- background and buttons code here -->
<ListView
android:id="#id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:headerDividersEnabled="true"/>
</LinearLayout>

Custom Adapter is not showing anything from a Fragment [duplicate]

This question already has answers here:
ListView not showing at all
(1 answer)
Null pointer Exception - findViewById()
(12 answers)
Closed 4 years ago.
I have a custom Adapter called MyAdapter where i'm trying to load a layout which supposedly should be inserted into a listview, but even when Android is not giving errors the view is showing in blank.
In the next screenshot, i show what i get after build my app.
What i'm expecting to load is my layout into my listview:
ListView
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".Fragments.NewsFragment">
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</FrameLayout>
Layout
<?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:id="#+id/list_item"
android:layout_height="80dp"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageabdcf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_margin="25dp"
tools:srcCompat="#android:drawable/btn_star_big_on" />
<TextView
android:id="#+id/textabcdf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:text="TextView" />
</LinearLayout>
NewsFragment.java
public class NewsFragment extends Fragment {
private ListView listView;
private List<String> names;
public NewsFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_news, container, false);
listView = (ListView) v.findViewById(R.id.listView);
names = new ArrayList<String>();
names.add("Fernando");
names.add("Roberto");
names.add("Torres");
names.add("Urban");
ArrayAdapter<String> adapter = new ArrayAdapter<>(v.getContext(), android.R.layout.simple_list_item_1, names);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getView().getContext(), "Clicked: " + names.get(position), Toast.LENGTH_LONG).show();
}
});
MyAdapter myAdapter = new MyAdapter(v.getContext(), R.layout.card_view_news, names);
listView.setAdapter(myAdapter);
// Inflate the layout for this fragment
return v;
}
}
MyAdapter.java
public class MyAdapter extends BaseAdapter {
private Context context;
private int layout;
private List<String> names;
public MyAdapter(Context context, int layout, List<String> names) {
this.context = context;
this.layout = layout;
this.names = names;
}
#Override
public int getCount() {
return 0;
}
#Override
public Object getItem(int position) {
return this.names.get(position);
}
#Override
public long getItemId(int id) {
return id;
}
#Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
View v = convertView;
LayoutInflater layoutInflater = LayoutInflater.from(this.context);
v = layoutInflater.inflate(R.layout.fragment_news, null);
String currentName = names.get(position);
//currentName = (String) getItem(position);
TextView textView = (TextView) v.findViewById(R.id.textabcdf);
textView.setText(currentName);
return v;
}
}
And what i'm expecting for is something like:

Identify AutocompleteTextView in ListView Item

I have a couple of AutocompleteTextView as items in ListView; these items are dynamically loaded using Custom adapter. When an item is selected in the AutoCompleteTextView, I need to update another TextView which is on the same row as the AutoCompleteTextView. I'm having trouble identifying position of AutoCompleteTextView where an item was selected. Any pointers would be appreciated:
My Activity looks like this, where Listview is defined:
<ListView
android:id="#+id/listview_orders"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
ListView Items are defined in another layout file as:
<?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:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<AutoCompleteTextView
android:id="#+id/product_code_row_item"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:ems="10" />
<AutoCompleteTextView android:id="#+id/product_name_row_item"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
/>
<TextView android:id="#+id/product_size_row_item"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
/>
</LinearLayout>
ListView adapter is being set as below:
ListView view = (ListView) findViewById(R.id.listview_orders);
ListViewItemAdapter adapter = new ListViewItemAdapter(this, orderItems, prodCodeList, prodNameList);
view.setAdapter(adapter);
Custom Adapter getView method implementation is as follows:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = activity.getLayoutInflater();
convertView = inflater.inflate(R.layout.order_list_row, null);
}
AutoCompleteTextView productCode = (AutoCompleteTextView ) convertView.findViewById(R.id.product_code_row_item);
AutoCompleteTextView productName = (AutoCompleteTextView) convertView.findViewById(R.id.product_name_row_item);
TextView productSize = (TextView) convertView.findViewById(R.id.product_size_row_item);
TextView mQty = (TextView) convertView.findViewById(R.id.product_mqty_row_item);
OrderItem orderItem = (OrderItem)orderItemList.get(position);
productCode.setText(orderItem.getProductCode());
productCode.setTag(position);
productName.setText(orderItem.getDescription());
productName.setTag(position);
productSize.setText(orderItem.getProductSize());
//Setting up autocorrect adapter for product codes
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(activity, android.R.layout.select_dialog_item, prodCodeList);
//Getting the instance of AutoCompleteTextView
productCode.setThreshold(1);//will start working from first character
productCode.setAdapter(adapter);//setting the adapter data into the AutoCompleteTextView
productCode.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View arg1, int pos,
long id) {
Toast.makeText(OrderApplication.getCustomAppContext(), (CharSequence)parent.getItemAtPosition(pos), Toast.LENGTH_LONG).show();
int itemPosition = (Integer) arg1.getTag();
//loadOrderItem(newOrderItem, (String) parent.getItemAtPosition(pos), true);
}
});
//Setting up autocorrect adapter for product names
adapter = new ArrayAdapter<String>
(activity, android.R.layout.select_dialog_item, prodNameList);
//Getting the instance of AutoCompleteTextView
productName.setThreshold(2);//will start working from first character
productName.setAdapter(adapter);//setting the adapter data into the AutoCompleteTextView
return convertView;
}
you should use -
adapter.getItem(pos)
inside your onItemClick
I found a workaround to get reference of AutoCompleteTextView in listview.
holder.productCodeView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus) {
currentFocusView = v;
}
}
});
holder.productCodeView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View arg1, int pos, long id) {
if (currentFocusView != null) {
int iPosition = (Integer) currentFocusView.getTag();
}
}
});

How to change an ImageView in a row of ListView?

I have a ListView which is populated using a SimpleCursorAdapter. The row layout is given below:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/row"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp" />
<ImageView
android:id="#+id/enable_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:padding="10dp"
android:contentDescription="#string/image_description"
android:src="#android:drawable/checkbox_off_background" />
</RelativeLayout>
What i want to do is to change the source of the imageview to something else based on some shared preference when the activity is created. This is the code that i use but it doesn't work:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
long appliedProfileId = prefs.getLong("appliedProfileId", -1);
if(appliedProfileId != -1) {
/*View rowView = (View) getListView().getAdapter().getView(info.psition, null, null);
TextView textView = (TextView) rowView.findViewById(R.id.row);*/
ListView listView = getListView();
ListAdapter adapter = listView.getAdapter();
View appliedRowLayout = (View) adapter.getView(getItemPositionByAdapterId(adapter, appliedProfileId), null, null);
TextView textView = (TextView) appliedRowLayout.findViewById(R.id.row);
Log.d("asdasd", textView.getText().toString());
ImageView imageView = (ImageView) appliedRowLayout.findViewById(R.id.enable_image);
imageView.setImageResource(android.R.drawable.checkbox_on_background);
setListAdapter(adapter);
}
This is the getItemPositionByAdapterId method:
private int getItemPositionByAdapterId(ListAdapter adapter, final long id)
{
for (int i = 0; i < adapter.getCount(); i++)
{
if (adapter.getItemId(i) == id)
return i;
}
return -1;
}
I am able to read the contents of the listview but cannot change them.. Pleas help :)
You need to override the getView method for this listview. Check Customizing Android ListView Items
You need to do something like this.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (position == 0) // first element in the list
{
convertView.findViewById(R.id.image1).setImageURI(....)
}
and so on....

Change Background colour of ListView in android at run time

I am doing project in Android. I want to change background color as well as textcolor of selected item from ListView. Here is my code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="right"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ListView
android:id="#+id/listView1"
android:layout_width="265dp"
android:layout_height="366dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_weight="0.00"
android:drawSelectorOnTop="true" >
</ListView>
</LinearLayout>
</LinearLayout>
So,I have ListView with some student names and with facility of multiple choice by using checkbox.
ListView stud_lst=(ListView) findViewById(R.id.listView1);
stud_lst.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
I want to change the background and text color of selected student.
I already saw some answers but I am not getting it.
Please help me.
Use a custom adapter and in your activity class do the following:
// mListview is ur listview object.
mListview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
view.setBackgroundColor("your bg's color id");
}
}
You have to create a Custom Adapter to change the item's Background Color. Here is the example of Custom adapter:
public class PaListAdapter extends BaseAdapter{
private LayoutInflater mInflater;
private ArrayList<String> platevalue = new ArrayList<String>();
ViewHolder holder;
public PaListAdapter(Context context,ArrayList<String> value)
{
// Cache the LayoutInflate to avoid asking for a new one each time.
mInflater = LayoutInflater.from(context);
//mycontext = context;
platevalue.clear();
platevalue =value;
}
public int getCount()
{
return platevalue.size();
}
public Object getItem(int position)
{
return position;
}
public long getItemId(int position)
{
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.select_dialog, null);
holder = new ViewHolder();
holder.hTransID =(TextView) convertView.findViewById(R.id.txtChoice);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.hTransID.setText(platevalue.get(position));
return convertView;
}
static class ViewHolder
{
TextView hTransID;
}
}
select_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:descendantFocusability="blocksDescendants"
android:background="#000000"
>
<TextView
android:id="#+id/txtChoice"
android:layout_gravity="center_vertical|left"
android:gravity="center_vertical|left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"/>
</LinearLayout>
In Activity Class.Define it like:
simpleefficientadapter efficientadapter;
efficientadapter=new simpleefficientadapter(CLASSNAME.this, VALUES);
listView.setAdapter(efficientadapter);

Categories