2013-10-24 3 views
0

Я пытаюсь реализовать флажок в каждом элементе listView. ListView - это совокупность всех установленных пользователями приложений. Проблема в том, что когда я запускаю свое приложение и перехожу в свой списокView, все, что я вижу, это всего лишь одна строка (значок приложения, а затем флажок рядом с ним), вместо того, чтобы в каждой строке был флажок. Вот мой макет, который определяет элементы ListView:Checkbox messes up all listView

<?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="?android:attr/listPreferredItemHeight" 
android:padding="5dip" 
> 

<ImageView 
    android:id="@+id/ivIcon" 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    android:layout_marginRight="5dip" 
    android:scaleType="center" 
    android:contentDescription="@string/desc" 
/> 

<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="0dip" 
    android:layout_height="fill_parent" 
    android:layout_weight="1"  
> 

    <TextView 
     android:id="@+id/tvName" 
     android:layout_width="fill_parent" 
     android:layout_height="0dip" 
     android:layout_weight="1" 
     android:gravity="center_vertical"   
    /> 

    <TextView 
     android:id="@+id/tvPack" 
     android:layout_width="fill_parent" 
     android:layout_height="0dip" 
     android:layout_weight="1" 
     android:singleLine="true" 
     android:ellipsize="marquee"   
    /> 

</LinearLayout> 

<CheckBox 
    android:id="@+id/addCheckbox" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" 
    android:gravity="center_vertical" 
/> 

</LinearLayout> 

Тогда вот мой заказ адаптер:

package com.example.awesomefilebuilderwidget; 

IMPORTS 

public class AppInfoAdapter extends BaseAdapter implements Filterable { 
private Context mContext; 
private List<ApplicationInfo> mListAppInfo; 
private PackageManager mPackManager; 
private List<ApplicationInfo> originalListAppInfo; 
private Filter filter; 

public AppInfoAdapter(Context c, List<ApplicationInfo> listApp, PackageManager pm) { 
    mContext = c; 
    this.originalListAppInfo = this.mListAppInfo = listApp; 
    mPackManager = pm; 
    } 

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

@Override 
public Object getItem(int position) { 
    return mListAppInfo.get(position); 
} 

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

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    // get the selected entry 
    ApplicationInfo entry = (ApplicationInfo) mListAppInfo.get(position); 

    // reference to convertView 
    View v = convertView; 

    // inflate new layout if null 
    if(v == null) { 
     LayoutInflater inflater = LayoutInflater.from(mContext); 
     v = inflater.inflate(R.layout.layout_appinfo, null); 
    } 

    // load controls from layout resources 
    ImageView ivAppIcon = (ImageView)v.findViewById(R.id.ivIcon); 
    TextView tvAppName = (TextView)v.findViewById(R.id.tvName); 
    TextView tvPkgName = (TextView)v.findViewById(R.id.tvPack); 
    CheckBox addCheckbox = (CheckBox)v.findViewById(R.id.addCheckbox); 

    // set data to display 
    ivAppIcon.setImageDrawable(entry.loadIcon(mPackManager)); 
    tvAppName.setText(entry.loadLabel(mPackManager)); 
    tvPkgName.setText(entry.packageName); 
    addCheckbox.setChecked(true); 
    checkButtonClick(); 

    // return view 
    return v; 
} 

private void checkButtonClick() { 
    // do nothing 
} 

Мой список работал хорошо, пока я не добавил флажок. Почему он это делает?

ответ

0

Поскольку вы используете weight, вы должны использовать "0dp" для вашего width. Измените ваш CheckBox на

<CheckBox 
    android:id="@+id/addCheckbox" 
    android:layout_width="0dp" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" 
    android:gravity="center_vertical" 
    /> 
+0

Fixed, но теперь некоторые из моих взглядов текста хрустел так, что будет хороший вес, так как флажок не нужно занимать много места – user2909006

+0

Вам придется играть с этим так как я не знаю точно, как вы хотите, чтобы это выглядело. Может быть, попробуй .5 – codeMagic

+0

Хорошо, я испортил это и получил то, что хотел. В любом случае спасибо! – user2909006

Смежные вопросы