Custom ListView is not displayed - java

Following some tutorials, I managed to make a custom list.
But the whole screen is just a list
And now want to put the list below the form. And to make the whole screen make scroll along with the list (see the last item in the list, the form needs to rise)
The log (Toasty) says that the item was added, but nothing appears. I do not know if the problem is in how I add, or is the way I try to display
This is my layout:
<?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="wrap_content"
android:background="#ffffff"
android:fillViewport="true"
android:padding="5dp" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/cliente" />
<EditText
android:id="#+id/venda_form_cliente"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:cursorVisible="false"
android:focusable="false"
android:hint="#string/selecione_um_cliente" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/produto" />
<EditText
android:id="#+id/venda_form_prod"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:cursorVisible="false"
android:focusable="false"
android:hint="#string/selecione_um_produto" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/quantidade" />
<EditText
android:id="#+id/venda_form_qtd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:selectAllOnFocus="true"
android:text="#string/um" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/valor" />
<EditText
android:id="#+id/venda_form_valor"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cursorVisible="false"
android:focusable="false"
android:inputType="none" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/desconto" />
<EditText
android:id="#+id/venda_form_desc"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:selectAllOnFocus="true"
android:text="#string/zero" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/subtotal" />
<EditText
android:id="#+id/venda_form_subtotal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cursorVisible="false"
android:focusable="false"
android:inputType="none" />
</LinearLayout>
</LinearLayout>
<Button
android:id="#+id/venda_form_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="10dp"
android:text="#string/adicionar" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:scrollbars="none" >
</ListView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
And my activity
public class VendaFormActivity extends Activity {
private Cliente cliente;
// private Spinner condicaoSelect;
private Produto produto;
private TextWatcher somar;
private ItemVenda itemVenda;
private ListaAdapter listaAdapter;
private EditText inputQuantidade;
private EditText inputDesconto;
private EditText inputSubTotal;
private EditText inputProduto;
private EditText inputValor;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_venda_form);
inputProduto = ((EditText) findViewById(R.id.venda_form_prod));
inputValor = ((EditText) findViewById(R.id.venda_form_valor));
((EditText) findViewById(R.id.venda_form_cliente)).setOnClickListener(new ClickListener(this, ClienteActivity.class, MainActivity.REQUEST_CLIENTE));
inputProduto.setOnClickListener(new ClickListener(this, ProdutoActivity.class, MainActivity.REQUEST_PRODUTO));
//condicaoSelect = (Spinner) findViewById(R.id.venda_form_condicao);
//new CondicaoHelper(this).popular(condicaoSelect);
bindCamposValores();
fazerLista();
bindBtnAdd();
}
private void limparCamposValores() {
inputDesconto.setText("0.00");
inputQuantidade.setText("1");
inputSubTotal.getText().clear();
inputProduto.getText().clear();
}
private void bindBtnAdd() {
((Button) findViewById(R.id.venda_form_btn)).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try{
Double quantidade = Double.parseDouble( inputQuantidade.getText().toString() );
Double desconto = Double.parseDouble( inputDesconto.getText().toString() );
itemVenda = new ItemVenda();
itemVenda.setDesconto(desconto);
itemVenda.setProduto(produto);
itemVenda.setQuantidade(quantidade);
listaAdapter.setNotifyOnChange(true);
listaAdapter.getItens().add(itemVenda);
listaAdapter.notifyDataSetChanged();
Toast.makeText(getApplication(), "Foi", Toast.LENGTH_SHORT).show();
limparCamposValores();
} catch (Exception e) {
Toast.makeText(getApplication(), "Invalido", Toast.LENGTH_SHORT).show();
// NPE no produto
// NumberFormat nos valores
}
}
});
}
private void fazerLista() {
listaAdapter = new ListaAdapter(getApplicationContext());
((ListView) findViewById(android.R.id.list)).setAdapter(listaAdapter);
}
static class ViewHolder {
protected TextView descricao;
protected TextView codigo;
protected TextView ean;
protected TextView referencia;
protected TextView quantidade;
protected TextView valor_unit;
protected TextView valor_item;
}
private class ListaAdapter extends ArrayAdapter<ItemVenda>{
private final Context context;
private final List<ItemVenda> itens;
public ListaAdapter(Context context) {
super(context, R.layout.produto_list);
this.context = context;
this.itens = new LinkedList<ItemVenda>();
}
public ListaAdapter(Context context, List<ItemVenda> itens) {
super(context, R.layout.produto_list, itens);
this.context = context;
this.itens = itens;
}
public List<ItemVenda> getItens() {
return itens;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflator.inflate(R.layout.produto_list_item, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.descricao = (TextView) view.findViewById(R.id.descricao);
viewHolder.codigo = (TextView) view.findViewById(R.id.codigo);
viewHolder.ean = (TextView) view.findViewById(R.id.ean);
viewHolder.referencia = (TextView) view.findViewById(R.id.referencia);
viewHolder.quantidade = (TextView) view.findViewById(R.id.quantidade);
viewHolder.valor_unit = (TextView) view.findViewById(R.id.valor_unit);
viewHolder.valor_item = (TextView) view.findViewById(R.id.valor_item);
view.setTag(viewHolder);
} else {
view = convertView;
}
ViewHolder holder = (ViewHolder) view.getTag();
ItemVenda item = itens.get(position);
holder.descricao.setText(item.getProduto().getNome());
holder.codigo.setText(item.getProduto().getCodigo());
holder.ean.setText(item.getProduto().getEan());
holder.referencia.setText(item.getProduto().getReferencia());
holder.quantidade.setText(item.getQuantidade().toString());
holder.valor_unit.setText(item.getProduto().getPreco().toString());
holder.valor_item.setText(item.getSubTotal().toString());
return view;
}
}
private void bindCamposValores() {
inputQuantidade = ((EditText) findViewById(R.id.venda_form_qtd));
inputDesconto = ((EditText) findViewById(R.id.venda_form_desc));
inputSubTotal = ((EditText) findViewById(R.id.venda_form_subtotal));
somar = new TextWatcher() {
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { }
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { }
#Override
public void afterTextChanged(Editable arg0) {
Double sub = 0.0;
try{
itemVenda = new ItemVenda();
itemVenda.setQuantidade(Double.parseDouble(inputQuantidade.getText().toString()));
itemVenda.setDesconto(Double.parseDouble(inputDesconto.getText().toString()));
itemVenda.setProduto(produto);
sub = itemVenda.getSubTotal();
} catch (Exception e) {
// algum campo não é numero/está vazio
}
inputSubTotal.setText(""+sub);
}
};
// Se mudar o valor
inputQuantidade.addTextChangedListener(somar);
inputDesconto.addTextChangedListener(somar);
// quando sair do campo
inputQuantidade.setOnFocusChangeListener(new Focus("1"));
inputDesconto.setOnFocusChangeListener(new Focus("0.00"));
}
private class Focus implements OnFocusChangeListener {
private final String padrao;
public Focus(String padrao){
this.padrao = padrao;
}
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus){
Editable text = ((EditText) v).getText();
if(null == text || "".equals(text.toString())){
text.append(padrao);
}
}
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_venda_form, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.menu_copiar:
copiar();
return true;
case R.id.menu_simulacao:
simulacao();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void copiar() {
Toast.makeText(getApplication(), "Nada ainda", Toast.LENGTH_SHORT).show();
}
private void simulacao() {
Intent intent = new Intent(getApplicationContext(), SimulacaoPagtoActivity.class);
startActivityForResult(intent, MainActivity.REQUEST_SIMULACAO);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == MainActivity.REQUEST_CLIENTE) {
if (resultCode == RESULT_OK) {
cliente = (Cliente) intent.getSerializableExtra(MainActivity.RESULT_MODEL_LIST);
((EditText) findViewById(R.id.venda_form_cliente)).setText(cliente.getNome());
}
} else if (requestCode == MainActivity.REQUEST_PRODUTO) {
if (resultCode == RESULT_OK) {
produto = (Produto) intent.getSerializableExtra(MainActivity.RESULT_MODEL_LIST);
inputProduto.setText(produto.getNome());
inputValor.setText(""+produto.getPreco());
somar.afterTextChanged(null);
}
} else if(requestCode == MainActivity.REQUEST_SIMULACAO){
// o que fazer quando voltar da simulação ?
}
}
}
Finally, my item layout
<?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="wrap_content"
android:baselineAligned="false"
android:orientation="vertical"
android:padding="5dp" >
<TextView
android:id="#+id/descricao"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#333"
android:textSize="16sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/codigo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="CODIGO" />
<TextView
android:id="#+id/ean"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="EAN" />
<TextView
android:id="#+id/referencia"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="REFERENCIA" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/quantidade"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="QUANT." />
<TextView
android:id="#+id/valor_unit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="VLR UNIT" />
<TextView
android:id="#+id/valor_item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="VLR ITEM" />
</LinearLayout>
</LinearLayout>
UPDATE
When I add an item in the list, nothing is shown. But after I click a TextEdit, and the virtual keyboard up, after typing some value, I close the keyboard, then the adapter.getView () is called and the list appears

I think I found your problem. Shouldn't the following code:
((ListView) findViewById(android.R.id.list)).setAdapter(listaAdapter);
Be:
((ListView) findViewById(R.id.list)).setAdapter(listaAdapter);
instead?
You are trying to find a default Android view, rather than the view that you have created, which means that you are not actually setting the adapter for your ListView.

I am not sure about that, but if you want the list to be scrollable with the activity, you should use ScrollView, and then check if you scroll the list or the view itself. Try it
You can also try to see with debugger if the item was actually added to the list. I hope you will find an error.

Found the solution
1) The adapter have 2 constructors, but just 1 with List<ItemVenda> itens, and it isn't called.
I fixed it.
2) To add an item on the list, I need to call adapter.add() and not adapter.getItens().add()`. I think this trigger others methods.
3) Finally, to take the scroll list, and apply the scroll across the screen, I need to increase the size of the list. Then I override the method adapter.add(), and calculate the new height of the list, and add android:scrollbars="none" on ListView
Thanks to all, especially to #Marek

Related

Getting the progress of multiple seekbars in recycler view

So I am displaying a recylerview with an Image and the name of a person. Below that is a seekbar, which the user can move to rate that person.
Now I want to store the ratings (=progress) of each seekbar in a list. However right now it only stores the rating of the last seekbar in the list.
So it stores only the progress of the last seekbar right now. I would like that when the user clicks the button that every current rating value of every seekbar is stored in a list.
Thank you very much.
That is is my layout_listitem:
<?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="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="3dp"
android:id="#+id/parent_layoutREC"
android:orientation="horizontal"
android:layout_marginTop="13dp"
android:gravity="center">
<de.hdodenhof.circleimageview.CircleImageView
android:paddingTop="2dp"
android:paddingLeft="2dp"
android:layout_width="73dp"
android:layout_height="73dp"
android:id="#+id/kunde_imageREC"
android:src="#mipmap/ic_launcher"/>
<TextView
android:layout_marginLeft="40dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Canada"
android:id="#+id/kunde_nameREC"
android:textColor="#000"
android:textSize="19sp"
android:textStyle="bold"/>
</LinearLayout>
<SeekBar
android:layout_marginRight="35dp"
android:layout_marginLeft="35dp"
android:layout_marginTop="8dp"
android:id="#+id/seek_Bar"
android:max="10"
android:progress="5"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="0 1 2 3 4 5"
android:textSize="20sp"/>
That is my RecyclerViewAdapter class:
public class RecyclerViewAdap extends
RecyclerView.Adapter<RecyclerViewAdap.ViewHolder> {
private static final String TAG = "RecyclerViewAdap";
private ArrayList<String> mImageUrl = new ArrayList<>();
private ArrayList<String> mKundeNamen = new ArrayList<>();
public ArrayList<Integer> mBewertungen = new ArrayList<>();
private Context mContext;
public String bewertung;
private TextView progressSeekbar;
public RecyclerViewAdap(ArrayList<String> imageUrl, ArrayList<String> kundeNamen, Context context) {
mImageUrl = imageUrl;
mKundeNamen = kundeNamen;
mContext = context;
}
public class ViewHolder extends RecyclerView.ViewHolder {
CircleImageView imageView;
TextView kundeName;
LinearLayout parentLayout;
SeekBar seekBar1;
public ViewHolder(#NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.kunde_imageREC);
kundeName = itemView.findViewById(R.id.kunde_nameREC);
parentLayout = itemView.findViewById(R.id.parent_layoutREC);
seekBar1 = itemView.findViewById(R.id.seek_Bar);
progressSeekbar = itemView.findViewById(R.id.progress_seekbar);
seekBar1.setOnSeekBarChangeListener(seekBarChangeListener);
//int progress = seekBar1.getProgress();
//String.valueOf(Math.abs((long)progress)).charAt(0);
//progressSeekbar.setText("Bewertung: " +
// String.valueOf(Math.abs((long)progress)).charAt(0) + "." + String.valueOf(Math.abs((long)progress)).charAt(1));
}
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_listitem, parent, false);
ViewHolder holder = new ViewHolder(view);
Log.d(TAG, "onCreateViewHolder: ");
return holder;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, final int position) {
Glide.with(mContext)
.load(mImageUrl.get(position))
.into(holder.imageView);
holder.kundeName.setText(mKundeNamen.get(position));
holder.parentLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(mContext, mKundeNamen.get(position),
Toast.LENGTH_SHORT).show();
}
});
holder.seekBar1.setTag(position);
}
#Override
public int getItemCount() {
return mImageUrl.size();
}
SeekBar.OnSeekBarChangeListener seekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// updated continuously as the user slides the thumb
progressSeekbar.setText(String.valueOf(progress));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// called when the user first touches the SeekBar
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// called after the user finishes moving the SeekBar
if (seekBar.getTag().toString().equals("1")) {
mBewertungen.add(seekBar.getProgress());
if (mBewertungen.size() == 2) {
mBewertungen.remove(0);
Toast.makeText(mContext, "onProgressChanged: " + mBewertungen.toString(), Toast.LENGTH_SHORT).show();
}
}
if (seekBar.getTag().toString().equals("2")) {
mBewertungen.add(seekBar.getProgress());
if (mBewertungen.size() == 3) {
mBewertungen.remove(1);
Toast.makeText(mContext, "onProgressChanged: " + mBewertungen.toString(), Toast.LENGTH_SHORT).show();
}
}
}
};
}
That is the activity where the Recyclerview is being displayed:
<?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="match_parent"
tools:context=".BewertungEsserActvity"
android:orientation="vertical"
app:layoutManager="LinearLayoutManager"
android:background="#drawable/gradient_background">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="14dp"
android:text="Bewerte deine Gäste"
android:textColor="#color/colorDarkGrey"
android:textSize="30sp"
android:textStyle="bold" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/colorBlack"
android:layout_marginBottom="10dp"/>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/recycler_view">
</androidx.recyclerview.widget.RecyclerView>
<ProgressBar
android:visibility="invisible"
android:id="#+id/progressbar_4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="13dp" />
<Button
android:layout_marginRight="66dp"
android:layout_marginLeft="66dp"
android:id="#+id/bewerten_Btn"
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_marginTop="10dp"
android:background="#drawable/btn_background_profil"
android:padding="10dp"
android:text="Bewerten"
android:textAllCaps="false"
android:textColor="#color/colorWhite"
android:textSize="16sp"
android:textStyle="normal" />
<View
android:layout_marginBottom="10dp"
android:layout_marginTop="34dp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#color/colorBlack" />
</LinearLayout>
</ScrollView>
You can do it in multiple ways:
1- You can store seekbars value as a property in your model and update that property on OnSeekBarChangeListener and when the user hits the button get the data source from the adapter and then run a loop on it and call getter of that attribute.
2- You can store seekbars value as a List<> in your adapter and whenever the user hits the button get the list from the adapter.

Android List View shows items, but text is empty

I am experiencing a very weird issue where I am unable to view any text/data, but it clearly shows the number of items in the list.
I have confirmed that dataList has data, and I am able to get the exact same string that I set after debugging.
Parent Fragment:
ListView InboxListView = (ListView) view.findViewById(R.id.listViewInbox);
InboxAdapter adapter = new InboxAdapter(mActivity, R.layout.activity_inbox_item, dataList, NotificationInbox.this);
XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/SwipeContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:padding="15dp"
android:id="#+id/searchBarInbox"
android:hint="Search"
android:layout_height="wrap_content" />
<ListView
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_marginTop="49dp"
android:id="#+id/listViewInbox" />
</RelativeLayout>
</android.support.v4.widget.SwipeRefreshLayout>
Adapter: http://pastebin.com/mSUrdLcW << CLICK
public class InboxAdapter extends ArrayAdapter<InboxBO> {
NotificationInbox fragment;
Context context;
List<InboxBO> inboxForSharedPref;
List<InboxBO> inboxList;
int layoutResID;
SharedPreferences prefs;
private ArrayList<InboxBO> inboxarraylist;
private SparseBooleanArray mSelectedItemIds;
public InboxAdapter(Context context, int layoutResourceID, List<InboxBO> objs, NotificationInbox fragment) {
super(context, layoutResourceID, objs);
mSelectedItemIds = new SparseBooleanArray();
this.context = context;
this.inboxList = objs;
this.layoutResID = layoutResourceID;
this.inboxarraylist = new ArrayList<InboxBO>();
this.inboxarraylist.addAll(inboxList);
this.fragment = fragment;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
inboxHolder inboxholder;
View view = convertView;
if (view == null) {
view = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(layoutResID, parent, false);
//LayoutInflater inflater = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE));
//LayoutInflater inflater = LayoutInflater.from(getContext());
//inflater.inflate(R.layout.activity_inbox_item, null);
inboxholder = new inboxHolder();
//view = inflater.inflate(layoutResID, parent, false);
prefs = context.getSharedPreferences(
Constants.PREF_NAME, 0);
inboxholder.swipeLayout = (SwipeLayout)view.findViewById(R.id.swipe_layout);
inboxholder.senderNameText = (TextView) view.findViewById(R.id.senderNameText);
inboxholder.pushDateText = (TextView) view.findViewById(R.id.pushDateText);
inboxholder.pushTimeText = (TextView) view.findViewById(R.id.pushTimeText);
inboxholder.messageText = (TextView) view.findViewById(R.id.messageText);
inboxholder.delete = (TextView)view.findViewById(R.id.delete);
inboxholder.itemLayout = (RelativeLayout) view.findViewById(R.id.relativeViewInbox);
inboxholder.swipeLayout.setShowMode(SwipeLayout.ShowMode.PullOut);
inboxholder.delete.setOnClickListener(onDeleteListener(position, inboxholder));
view.setTag(inboxholder);
}
else {
inboxholder = (inboxHolder) view.getTag();
}
InboxBO mItem = inboxList.get(position);
if(mItem!=null){
inboxholder.senderNameText.setText((String)mItem.getTitle());
inboxholder.pushDateText.setText(new Date().toString());
inboxholder.pushTimeText.setText(new Date().toString());
inboxholder.messageText.setText((String)mItem.getMessage());
}
return view;
}
// For swipe action
private View.OnClickListener onDeleteListener(final int position, final inboxHolder holder) {
return new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder alert = new AlertDialog.Builder(
context);
alert.setTitle("Delete Message");
alert.setMessage("Are you sure you wish to delete this message?");
alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
inboxList.remove(position);
Gson gson = new Gson();
inboxForSharedPref = fragment.getStoredMessages();
inboxForSharedPref = inboxList;
String jsonSavePref = gson.toJson(inboxForSharedPref);
fragment.commitToStoredList(jsonSavePref);
Toast.makeText(context, "Message Deleted!",
Toast.LENGTH_SHORT).show();
holder.swipeLayout.close();
fragment.retrieveMessage();
dialog.dismiss();
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alert.show();
}
};
}
// Algorithm to filter out listview based on text changed in listview searchbox
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
inboxList.clear();
if (charText.length() == 0) {
inboxList.addAll(inboxarraylist);
}
else
{
for (InboxBO ilist : inboxarraylist)
{
/*if (ilist.getDate().toLowerCase(Locale.getDefault()).contains(charText) || ilist.getMessage().toLowerCase(Locale.getDefault()).contains(charText) || ilist.getSendername().toLowerCase(Locale.getDefault()).contains(charText))
{
inboxList.add(ilist);
}*/
if(ilist.getTitle().toLowerCase(Locale.getDefault()).contains(charText) || ilist.getMessage().toLowerCase(Locale.getDefault()).contains(charText) ){
inboxList.add(ilist);
}
}
}
notifyDataSetChanged();
}
public static class inboxHolder {
TextView senderNameText, pushDateText, pushTimeText, messageText, delete, title;
RelativeLayout itemLayout;
private SwipeLayout swipeLayout;
}
// Methods below are for multi-deletion
public void toggleSelection(int position) {
selectView(position, !mSelectedItemIds.get(position));
}
// Remove selection after unchecked
public void remove(InboxBO object) {
inboxList.remove(object);
Gson gson = new Gson();
inboxForSharedPref = fragment.getStoredMessages();
inboxForSharedPref = inboxList;
String jsonSavePref = gson.toJson(inboxForSharedPref);
fragment.commitToStoredList(jsonSavePref);
notifyDataSetChanged();
}
// Item checked on selection
public void selectView(int position, boolean value) {
if (value)
mSelectedItemIds.put(position, value);
else
mSelectedItemIds.delete(position);
notifyDataSetChanged();
}
// Get number of selected item
public int getSelectedCount() {
return mSelectedItemIds.size();
}
public SparseBooleanArray getSelectedIds() {
return mSelectedItemIds;
}
public void removeSelection() {
mSelectedItemIds = new SparseBooleanArray();
notifyDataSetChanged();
}
}
Adapter XML:
<?xml version="1.0" encoding="utf-8"?>
<com.daimajia.swipe.SwipeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swipe_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/bottom_wrapper"
android:layout_width="100dp"
android:layout_gravity="start"
android:layout_height="match_parent"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ff0000">
<TextView
android:id="#+id/delete"
android:text="Delete"
android:textSize="18sp"
android:textColor="#color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
</LinearLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:background="?android:attr/activatedBackgroundIndicator"
android:orientation="vertical"
android:layout_width="match_parent"
android:id="#+id/relativeViewInbox"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:orientation="horizontal"
android:id="#+id/linearLayout">
<TextView
android:layout_width="match_parent"
android:text="SenderName"
android:textColor="#color/black"
android:textAppearance="?android:attr/textAppearanceMedium"
android:gravity="left"
android:layout_weight="1"
android:layout_height="match_parent"
android:id="#+id/senderNameText"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:text="PushDate"
android:gravity="right"
android:textColor="#color/black"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_weight="1"
android:layout_height="match_parent"
android:id="#+id/pushDateText" />
</LinearLayout>
<TextView
android:layout_width="320dp"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:text="Message..."
android:id="#+id/messageText"
android:textColor="#color/black"
android:textAppearance="?android:attr/textAppearanceMedium"
android:ellipsize="end"
android:maxLines="1"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/linearLayout" />
<TextView
android:layout_width="match_parent"
android:text="PushTime"
android:textAppearance="?android:attr/textAppearanceMedium"
android:gravity="right"
android:layout_weight="1"
android:textColor="#color/black"
android:layout_height="match_parent"
android:id="#+id/pushTimeText"
android:layout_alignTop="#+id/messageText" />
</RelativeLayout>
</com.daimajia.swipe.SwipeLayout>
EDIT: Screenshot added
EDIT2: Clarified parent fragment calling the adapter.
EDIT3: I did a temporary fix. Apparently the RelativeLayout and the Linear Layout in the AdapterXML were the cause of my misery. I'm not exactly sure why, though. I think it's the RelativeLayout and the LinearLayout somehow not displaying the text. Removing those two tags will display the texts in a linear fashion.
Try this
Adapter XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/bottom_wrapper"
android:layout_width="100dp"
android:layout_gravity="start"
android:layout_height="match_parent"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ff0000">
<TextView
android:id="#+id/delete"
android:text="Delete"
android:textSize="18sp"
android:textColor="#color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
</LinearLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:background="?android:attr/activatedBackgroundIndicator"
android:orientation="vertical"
android:layout_width="match_parent"
android:id="#+id/relativeViewInbox"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:orientation="horizontal"
android:id="#+id/linearLayout">
<TextView
android:layout_width="match_parent"
android:text="SenderName"
android:textColor="#color/black"
android:textAppearance="?android:attr/textAppearanceMedium"
android:gravity="left"
android:layout_weight="1"
android:layout_height="match_parent"
android:id="#+id/senderNameText"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:text="PushDate"
android:gravity="right"
android:textColor="#color/black"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_weight="1"
android:layout_height="match_parent"
android:id="#+id/pushDateText" />
</LinearLayout>
<TextView
android:layout_width="320dp"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:text="Message..."
android:id="#+id/messageText"
android:textColor="#color/black"
android:textAppearance="?android:attr/textAppearanceMedium"
android:ellipsize="end"
android:maxLines="1"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/linearLayout" />
<TextView
android:layout_width="match_parent"
android:text="PushTime"
android:textAppearance="?android:attr/textAppearanceMedium"
android:gravity="right"
android:layout_weight="1"
android:textColor="#color/black"
android:layout_height="match_parent"
android:id="#+id/pushTimeText"
android:layout_alignTop="#+id/messageText" />
</RelativeLayout>

onClickListener on a button that is added through a layout inflator

Here is my code so far based on various answers to related questions on SO.
In my Activity
GridView gv = (GridView) findViewById(R.id.gridView1);
MyCustomAdapter mAdapter = new MyCustomAdapter();
mAdapter.addItem("Action1");
mAdapter.addItem("Action2");
mAdapter.addItem("Action3");
gv.setAdapter(mAdapter);
My Adapter
private class MyCustomAdapter extends BaseAdapter {
private ArrayList<String> mData = new ArrayList<String>();
private LayoutInflater mInflater;
public MyCustomAdapter() {
mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void addItem(final String item) {
mData.add(item);
notifyDataSetChanged();
}
#Override
public int getCount() {
return mData.size();
}
#Override
public String getItem(int position) {
return mData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
NumericViewHolder holder = new NumericViewHolder();
if (convertView == null) {
convertView = mInflater.inflate(R.layout.horizontalnumberpicker, null);
holder.textView = (TextView)convertView.findViewById(R.id.txtNPTitle);
holder.minus = (Button)convertView.findViewById(R.id.btnMinus);
holder.plus = (Button)convertView.findViewById(R.id.btnPlus);
holder.value = (TextView)convertView.findViewById(R.id.txtNPValue);
holder.minus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TextView tv = (TextView) v.findViewById(R.id.txtNPValue);
int value = Integer.parseInt(tv.getText().toString());
Toast.makeText(getApplicationContext(), Integer.toString(value), Toast.LENGTH_SHORT).show();
if (value > 0) {
value = value - 1;
tv.setText(Integer.toString(value));
}
}
});
holder.plus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TextView tv = (TextView) v.findViewById(R.id.txtNPValue);
int value = Integer.parseInt(tv.getText().toString()); <--Error is here
Toast.makeText(getApplicationContext(), Integer.toString(value), Toast.LENGTH_SHORT).show();
value = value + 1;
tv.setText(Integer.toString(value));
}
});
convertView.setTag(holder);
} else {
holder = (NumericViewHolder)convertView.getTag();
}
holder.textView.setText(mData.get(position));
return convertView;
}
}
XML - Main_Activity
<?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:id="#+id/rlAddProduct"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/btnSaveProduct"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:text="#string/Save" />
<TextView
android:id="#+id/lblSelectCategory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/btnSaveProduct"
android:layout_alignBottom="#+id/btnSaveProduct"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"
android:text="#string/selectCategory" />
<Spinner
android:id="#+id/spCategory"
android:layout_width="250dp"
android:layout_height="40dp"
android:layout_alignBottom="#+id/lblSelectCategory"
android:layout_marginLeft="33dp"
android:layout_marginStart="33dp"
android:layout_toEndOf="#+id/lblSelectCategory"
android:layout_toRightOf="#+id/lblSelectCategory" />
<GridView
android:id="#+id/gridView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/lblSelectCategory"
android:layout_below="#+id/btnSaveProduct"
android:layout_marginTop="14dp"
android:numColumns="auto_fit" >
</GridView>
</RelativeLayout>
XML - Horizontalnumberpicker
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/txtNPTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="EnterTextHere"
android:layout_marginTop="5dp" />
<Button
android:id="#+id/btnPlus"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignBaseline="#+id/txtNPValue"
android:layout_alignBottom="#+id/txtNPValue"
android:layout_toRightOf="#+id/txtNPValue"
android:text="+" />
<TextView
android:id="#+id/txtNPValue"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignTop="#+id/btnMinus"
android:layout_toRightOf="#+id/btnMinus"
android:gravity="center"
android:text="0"
android:textAppearance="#style/AppBaseTheme"
android:textSize="20dp" />
<Button
android:id="#+id/btnMinus"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/txtNPTitle"
android:layout_marginTop="2dp"
android:text="-" />
</RelativeLayout>
The requirement is that when I press the plus button, I need to increment the value in the corresponding txtNPValue. Similarly with the minus button, I need to decrement the value in the txtNPValue.
The error is
01-31 22:40:10.854: E/AndroidRuntime(32198): java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference
Also I do not know if this is the right way to program such a requirement and would like some pointers.
TextView tv = (TextView) v.findViewById(R.id.txtNPValue);
this will return null pointer exception, as the view that u r getting is of the button or the clicked item..
Use this :-
RelativeLayout rlLayout = (RelativeLayout) v.getParent();
TextView tv = (TextView) rlLayout.findViewById(R.id.txtNPValue);
please remove:
TextView tv = (TextView) v.findViewById(R.id.txtNPValue);
from onClick to resolve nullpointerException

Change inflated row in adapter on click

I have an adapter with buttons and text views. It represents friend requests.
TextView text would be : Do you want to accept X as your friend?
There are 2 buttons : one for accepting, one for declining. Pressing Decline, it would just remove the row from the ListView, while pressing accept will change the text into: X and Y are now friends.
My question is how can I get the position of the row and change it on button press and also save the View for next time I access it?
Adapter : At the moment I don't have proper data structure for the adapter, so for testing purposes, just using a String:
public class FriendRequestAdapter extends BaseAdapter {
String mName;
Context mContext;
public FriendRequestAdapter(String name, Context context) {
this.mName = name;
this.mContext = context;
}
#Override
public int getCount() {
return 1;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.friend_request_layout, null);
}
TextView sender_name = (TextView) convertView.findViewById(R.id.wannabe_name);
if (mName.length() > 11) {
mName = mName.substring(0, 12);
}
sender_name.setText(mName);
ImageButton friend_request_accepted = (ImageButton) convertView.findViewById(R.id.accept_friend_request);
friend_request_accepted.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new AsyncTask<Void, Void, Void>() {
#Override
protected Void doInBackground(Void... params) {
try {
new APIService().confirmInvite();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
};
}
});
return convertView;
}
Inflated Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/changing_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/check_in_map_menu"
android:descendantFocusability="blocksDescendants">
<RelativeLayout
android:id="#+id/friend_image_container"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="7dp"
android:background="#drawable/polaroid_frame_friend_list">
<ImageView
android:id="#+id/facebook_friend_pic"
android:layout_width="55dp"
android:layout_height="45dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:background="#drawable/category_explore"/>
<TextView
android:id="#+id/name_facebook"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/facebook_friend_pic"
android:layout_centerHorizontal="true"
android:text="dummy"
android:textColor="#color/enloop_dark_gray"
android:textSize="9sp"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="7dp"
android:layout_toRightOf="#+id/friend_image_container">
<TextView
android:id="#+id/wannabe_name"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_centerVertical="true"
android:text="dada"
android:textColor="#color/enloop_dark_gray"
android:textSize="15dp"/>
<TextView
android:id="#+id/facebook_friend_name"
android:layout_width="250dp"
android:layout_height="50dp"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#+id/wannabe_name"
android:text=" has sent you a request"
android:textColor="#color/enloop_dark_gray"
android:textSize="15dp"/>
<ImageButton
android:id="#+id/later_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="40dp"
android:background="#drawable/btn_nothanks"
android:clickable="false"
android:focusable="false"/>
<ImageButton
android:id="#+id/accept_friend_request"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="40dp"
android:layout_toRightOf="#+id/later_button"
android:background="#drawable/btn_accept_text"
android:clickable="false"
android:focusable="false"/>
<ImageButton
android:id="#+id/invite_facebook_friends"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:background="#drawable/btn_delete_notification"
android:clickable="false"
android:focusable="false"/>
</RelativeLayout>
</RelativeLayout>

Why doesn't my fragment with custom layout check the given checkedItem

I have a DialogFragment which shows single choice items with radio buttons.
Here is my code:
SimpleAdapter simpleAdapter = new SimpleAdapter(
getActivity(),
listItem, //this is a hashmap
R.layout.my_custom_list_item,
new String[] { "id", "name", "status" },
new int[] { R.id.textview_ID, R.id.textview_name, R.id.textview_status });
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Title").setSingleChoiceItems(simpleAdapter, 0, //check the first item
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInteface,int position) {
//do something
}
}).setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,int id) {
//do something
}
});
I use ArrayAdapter with android.R.layout.simple_list_item_single_choice on a different place and it checks the first item. Note that i already gave 0 to the checkedItem parameter. I think there is a problem with my custom layout.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="10sp"
android:paddingRight="10sp"
android:paddingTop="2sp" >
<RadioButton
android:id="#+id/selected_cert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginRight="15dp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textview_ID"
android:layout_width="fill_parent"
android:layout_height="29dp"
android:layout_marginTop="5dp"
android:textSize="16sp"
android:textStyle="bold"
android:typeface="sans" >
</TextView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="28dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name :"
android:textSize="13sp"
android:textStyle="bold" >
</TextView>
<TextView
android:id="#+id/textview_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="13sp" >
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="30dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Satatus:"
android:textSize="13sp"
android:textStyle="bold" />
<TextView
android:id="#+id/textview_qualifiedstatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="13sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
How do i make the first custom item selected when user opens the dialog fragment.
I found out that i needed to create a custom LinearLayout and implement Checkable interface. Then use it as root view of my list item.
My custom LinearLayout:
public class CheckableLinearLayout extends LinearLayout implements Checkable {
RadioButton mRadioButton;
public CheckableLinearLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public CheckableLinearLayout(Context context) {
super(context);
}
public CheckableLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
protected void onFinishInflate() {
super.onFinishInflate();
findCheckableView(this);
}
//finds RadioButton recursively (comment if you know a better way to do that)
void findCheckableView(ViewGroup parent) {
for (int i = 0; i < parent.getChildCount(); i++) {
View child = parent.getChildAt(i);
if (child instanceof ViewGroup) {
findCheckableView((ViewGroup) child);
} else if (child != null) {
if (child instanceof RadioButton) {
mRadioButton = (RadioButton) child;
}
}
}
}
#Override
public void setChecked(boolean checked) {
if (mRadioButton != null) {
mRadioButton.setChecked(checked);
}
}
#Override
public boolean isChecked() {
return mRadioButton != null ? mRadioButton.isChecked() : false;
}
#Override
public void toggle() {
if (mRadioButton != null) {
mRadioButton.toggle();
}
}
}
My List item
<com.foo.customViews.CheckableLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="10sp"
android:paddingRight="10sp"
android:paddingTop="2sp" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="87dp"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/radio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginRight="15dp" />
<TextView
android:id="#+id/tv"
android:layout_width="fill_parent"
android:layout_height="29dp"
android:layout_marginTop="5dp"
android:textSize="16sp"
android:textStyle="bold"
android:typeface="sans" >
</TextView>
</LinearLayout>
<!-- some more views -->
</com.foo.customViews.CheckableLinearLayout>
Here is the article which helped me:
http://tokudu.com/post/50023900640/android-checkable-linear-layout

Categories

Resources