Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add my Dynamic Layout to ListView?

I have created a Dynamic LinearLayout and I want to add these layouts to ListView. The code is as follows:

DynamicLayout code:

import android.content.Context;
import android.widget.LinearLayout;
import android.widget.TextView;

public class DynamicContentLayout extends LinearLayout{
    private Context context;
    private int linearLayoutMargin;

    public DynamicContentLayout(Context context) {
        super(context);
        this.context = context;
        linearLayoutMargin = (int) context.getResources().getDimension(R.dimen.xlarge_margin_padding_fixed);
        initViews();
    }

    private void initViews() {
        LinearLayout.LayoutParams layout_lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        layout_lp.setMargins(linearLayoutMargin, linearLayoutMargin, linearLayoutMargin, linearLayoutMargin);
        this.setLayoutParams(layout_lp);
        this.setOrientation(LinearLayout.VERTICAL);
        this.addView(headerTitle("My Text"));

    }

    private TextView headerTitle(String title) {
        TextView txtView = new TextView(context);
        txtView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        txtView.setPadding(6, 6, 6, 6);
        txtView.setText(title);
        txtView.setBackgroundColor(context.getResources().getColor(R.color.red));
        txtView.setTextColor(context.getResources().getColor(R.color.white));
        return txtView;
    }

}

ListAdpaterCode:

import java.util.ArrayList;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

public class ListViewAdapter extends ArrayAdapter<DynamicContentLayout> {

    private Context context;
    private ArrayList<DynamicContentLayout> layoutList = null;

    public ListViewAdapter(Context context, int textViewResourceId, ArrayList<DynamicContentLayout> layoutList) {
        super(context, textViewResourceId, layoutList);
        this.context = context;
        this.layoutList = layoutList;
    }

    @Override
    public int getCount() {
        return layoutList.size();
    }

    @Override
    public DynamicContentLayout getItem(int position){
        return layoutList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        DynamicContentLayout entry = (DynamicContentLayout) layoutList.get(position);
        convertView = (View) entry;
        return convertView;
    }
}

Finally the code in the MainActivity where I am setting the Adapter:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ListView listView = (ListView) findViewById(R.id.listView);
    ArrayList<DynamicContentLayout> contents = new ArrayList<DynamicContentLayout>();
    contents.add(new DynamicContentLayout(getApplicationContext()));
    contents.add(new DynamicContentLayout(getApplicationContext()));
    contents.add(new DynamicContentLayout(getApplicationContext()));
    ArrayAdapter<DynamicContentLayout> arrayAdapter = new ArrayAdapter<DynamicContentLayout>(getApplicationContext(), android.R.layout.simple_list_item_1, contents);
    listView.setAdapter(arrayAdapter);
    arrayAdapter.notifyDataSetChanged();
}

Xml Layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
>

    <ListView
        android:id="@+id/listView"
        android:layout_width="fill_parent"
        android:background="@color/blue"
        android:layout_height="fill_parent"
        android:alwaysDrawnWithCache="true"
        android:clickable="true"
        android:layout_alignParentTop="true"
        android:divider="@color/grey"
        android:drawingCacheQuality="auto"
        android:fastScrollEnabled="true"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:scrollbars="none" >
    </ListView>

</RelativeLayout>

Now, when I run , there is no error reported. But I couldn't see listItems layouts. What I can see is name of the class only. for example: "com.myexample.dynamicontentoverview.DynamicContentLayout@405193d0" as the row of the listItem instead of the Layout.

There is one more point I want to add. I cannot use XMl Layout as my views inside my DynamicLinearLayout can increase/decrease for certain rows.

Please suggests something.

Thanks.

like image 644
Debopam Mitra Avatar asked Nov 14 '22 01:11

Debopam Mitra


1 Answers

You got the concept of custom listViews all wrong.

You should define your custom layout in an xml layout. The type of your array adapter shouldn't be a layout. It should be the object type you are trying to populate the listView with.

Check out this tutorial to get a better idea of the steps you should do.

like image 75
Benito Bertoli Avatar answered Jan 10 '23 19:01

Benito Bertoli