2

I have a task where I have to make a ListView where each row should contain image, title, and details. But I need to create it programmaticaly, without any XML files. So my question is what I did I do wrong? Here is my solution:

 // My custom View which represents the row of ListView   
 public class CustomListItem extends LinearLayout{

//region Private fields
private TextView listItemTitle_;
private TextView listItemDetails_;
private ImageView listItemImage_;
//endregion

//region Public constructors
public CustomListItem(Context context) {
    super(context);
    init(null, 0);
}

public CustomListItem(Context context, AttributeSet attributeSet) {
    super(context, attributeSet);
    init(attributeSet, 0);
}

public CustomListItem(Context context, AttributeSet attributeSet, int defStyle) {
    super(context, attributeSet, defStyle);
    init(attributeSet, defStyle);
}
//endregion

//region Accessors
public void setListItemTitle(String title) {
    this.listItemTitle_.setText(title);
}

public String getTitle() {
    return listItemTitle_.getText().toString();
}

public void setListItemDetails(String details) {
    this.listItemDetails_.setText(details);
}

public String getListItemDetails() {
    return listItemDetails_.getText().toString();
}

//endregion

private void init(AttributeSet attributeSet, int defStyle) {
    LinearLayout linearLayout = new LinearLayout(getContext());
    linearLayout.setOrientation(LinearLayout.VERTICAL);
    linearLayout.setPadding(0, 70, 0, 0);
    listItemTitle_ = new TextView(getContext());
    listItemTitle_.setText("Title");

    listItemDetails_  = new TextView(getContext());
    listItemDetails_.setText("Details");

    listItemImage_ = new ImageView(getContext());
    listItemImage_.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.clear));

    addView(linearLayout);
    linearLayout.addView(listItemTitle_);
    linearLayout.addView(listItemDetails_);
}

}

Here is my adapter

   public class ItemAdapter extends ArrayAdapter<ItemInfo> {

public ItemAdapter(Context context, List<ItemInfo> items) {
    super(context, R.layout.custom_list_item, items);

}

@Override
public View getView(int postition,View convertView, ViewGroup parent){
    ItemInfo item = this.getItem(postition);
    CustomListItem customListItem_ = new CustomListItem(getContext());
    customListItem_.setListItemTitle(item.getTitle());
    customListItem_.setListItemTitle(item.getDescription());
    return convertView;
}

}

But I don't need to use R.layout.custom_list_item, I need to use my customView instead of layout.

Here my code onCreate in MainActivity()

  @Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    LinearLayout layout = new LinearLayout(getApplicationContext());
    ListView listView = new ListView(getApplicationContext());

    CustomListItem customListItem = new CustomListItem(getApplicationContext());

    List<ItemInfo> values = new ArrayList<ItemInfo>();
    Bitmap image = BitmapFactory.decodeResource(getResources(), R.mipmap.clear);
    ItemInfo info = new ItemInfo(image,"Hi","android");
    ItemInfo info2 = new ItemInfo(image,"Hi2","android");
    values.add(info);
    values.add(info2);

    Adapter_ = new ItemAdapter(this, values);

    setContentView(R.layout.activity_main);

    layout.addView(listView);
    setContentView(layout);
}

Here main_activity layout

xmlns:tools="http://schemas.android.com/tools"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">

<com.example.admin.lab_127.CustomListItem
    android:id="@+id/custom_vie2w"
    android:layout_width = "wrap_content"
    android:layout_height="wrap_content"
    />

2 Answers 2

2

You must set the Adapter_ to your ListView

listView.setAdapter(Adapter_);

also, the line:

Your getView should return the value:

@Override
public View getView(int postition,View convertView, ViewGroup parent){
    ItemInfo item = this.getItem(postition);
    CustomListItem customListItem_ = new CustomListItem(getContext());
    customListItem_.setListItemTitle(item.getTitle());
    customListItem_.setListItemTitle(item.getDescription());
    return customListItem;
}
Sign up to request clarification or add additional context in comments.

2 Comments

thx, but it doesn't work. Does anyone else know what's wrong?
I think the problem is in the getView() method in my adapter, because I didn't change convertView. But how I can add my custom view into convertView?
1

Here is my adapter

 @Override
   public View getView(int position, View convertView, ViewGroup parent) {
    HorizontalScrollView horizontalScrollView = new HorizontalScrollView(getContext());

    LinearLayout layout = new LinearLayout(getContext());
    layout.setLayoutParams(new LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT
    ));

    float paddingLeft = getContext().getResources().getDimension(R.dimen.padding_left_for_image);
    layout.setPadding((int) paddingLeft, 0, 0, 0);

    ItemInfo item = this.getItem(position);
    CustomListItem customListItem_ = new CustomListItem(getContext());
    customListItem_.setListItemTitle(item.getTitle());
    customListItem_.setListItemDetails(item.getDescription());
    customListItem_.setListItemImage(item.getImage());

    layout.addView(customListItem_);

    horizontalScrollView.addView(layout);
    convertView = horizontalScrollView;
    return convertView;
}

And here is onCreate in MainActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    LinearLayout layout = new LinearLayout(getApplicationContext());
    layout.setOrientation(LinearLayout.VERTICAL);
    ListView listView = new ListView(this);

    listView.setPadding(0, (int)getResources().getDimension(R.dimen.list_item_top), 0, 0);

    listView.setDivider(null);

    List<ItemInfo> values = new ArrayList<ItemInfo>();
    Drawable image = getResources().getDrawable(R.mipmap.clear);

    for (int i = 0; i < 20; i++) {
        ItemInfo info = new ItemInfo(image, getResources().getString(R.string.title) + i, getResources().getString(R.string.details) + i);
        values.add(info);
    }

    Adapter_ = new ItemAdapter(this, values);
    listView.setAdapter(Adapter_);
    layout.addView(listView);
    setContentView(layout);
}

Hope it will be usefull

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.