在Fragment中使用ListActivity

时间:2014-03-23 15:54:28

标签: android android-fragments fragment android-fragmentactivity android-listfragment

以下代码运行在单独项目中实现的自定义列表视图。

public class MainActivity extends ListActivity implements OnTouchListener{

private MyCustomAdapter mAdapter;
Activity temp = this;
String []s = new String[500];
ArrayList<GS> q = new ArrayList<GS>();
ListView lv;
int count=0;

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

 DBAdapter db = DBAdapter.getDBAdapter(getApplicationContext());
    if (!db.checkDatabase()) 
    {
        db.createDatabase(getApplicationContext());
    }
    db.openDatabase();

    q = db.getData();

    mAdapter = new MyCustomAdapter();


mAdapter.addSeparatorItem(new ContentWrapper(q.get(0).getA_name(),null));
mAdapter.addItem(new ContentWrapper(q.get(0).getAS_name(), q.get(0).getDesc_art()));


for (int i = 1; i < 460; i++) {

  if (!(q.get(i).getA_name().trim().equals(q.get(i-1).getA_name().trim()))) {

      mAdapter.addSeparatorItem(new ContentWrapper(q.get(i).getA_name(), null));

  }
  mAdapter.addItem(new ContentWrapper(q.get(i).getAS_name(), q.get(i).getDesc_art()));

    }

    setListAdapter(mAdapter);        

}

//Adapter Class
private class MyCustomAdapter extends BaseAdapter {

    private static final int TYPE_ITEM = 0;
    private static final int TYPE_SEPARATOR = 1;
    private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;

    private ArrayList<ContentWrapper> mData = new ArrayList<ContentWrapper>();
    private LayoutInflater mInflater;

    private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();

    public MyCustomAdapter() {
        mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void addItem(ContentWrapper value) {
        mData.add(value);
        notifyDataSetChanged();
    }

    public void addSeparatorItem(ContentWrapper value) {
        mData.add(value);
        // save separator position
        mSeparatorsSet.add(mData.size() - 1);
        notifyDataSetChanged();
    }

    public ContentWrapper getItem(int position) {
        return mData.get(position);
    }
    @Override
    public int getItemViewType(int position) {
        return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
    }

    @Override
    public int getViewTypeCount() {
        return TYPE_MAX_COUNT;
    }

    public int getCount() {
        return mData.size();
    }

    public long getItemId(int position) {
        Log.v("getItemId Position", ""+position);
        return position;

    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        int type = getItemViewType(position);
        if (convertView == null) {
            holder = new ViewHolder();
            switch (type) {
            case TYPE_ITEM:
                convertView = mInflater.inflate(R.layout.activity_main1, null);
                holder.textView = (TextView)convertView.findViewById(R.id.text);

                break;
            case TYPE_SEPARATOR:
                convertView = mInflater.inflate(R.layout.activity_main2, null);
                holder.textView = (TextView)convertView.findViewById(R.id.textSeparator);
                count++;
                break;
            }
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        } holder.textView.setText(mData.get(position).getItem());

        if (type == TYPE_ITEM) {
            holder.textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    AlertDialog.Builder x = new AlertDialog.Builder(temp);
                    Log.v("position",""+position);
                           x.setIcon(R.drawable.ic_launcher)

                            // .setTitle(q.get(position-count).getAS_name())
                            .setTitle(mData.get(position).getItem())

                            // .setMessage(q.get(position-count).getDesc_art())
                            .setMessage(mData.get(position).getItemDescription())

                            .setCancelable(true)
                            .setPositiveButton("OK",
                                    new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface arg,
                                                int arg1) {
                                        }
                                    });
                     AlertDialog a = x.create();
                     a.show();
                 }
             });
        } else {
            holder.textView.setOnClickListener(null);
        }

    return convertView;
}
}
public static class ViewHolder {
    public TextView textView;
}

public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return false;
}

}

现在我希望它在我的应用 with Fragment 中运行相同的内容。 我只知道使用fragment = new ContentsFragment();会在片段中启动它。 应该使用默认构造函数&amp;扩展ListFragment一个onCreateView(...),我已经夸大了另一个活动

我是片段和片段的新手我不知道代码中应该更改哪些内容。

请帮忙!

修改

我正在展示我已尝试过的实现代码&amp;我收到4个错误,这些错误已在代码中指定:

public class ContentsFragment extends Fragment  implements OnTouchListener{

private MyCustomAdapter mAdapter;
Activity temp = this;// error: Type mismatch: cannot convert from ContentsFragment to Activity

String []s = new String[500];
ArrayList<GS> q = new ArrayList<GS>();
ListView lv;
int count=0;

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

DBAdapter db = DBAdapter.getDBAdapter(getApplicationContext());//error :The method getApplicationContext() is undefined for the type ContentsFragment
if (!db.checkDatabase()) 
{
    db.createDatabase(getApplicationContext());//error : The method getApplicationContext() is undefined for the type ContentsFragment  
}
db.openDatabase();

q = db.getData();

mAdapter = new MyCustomAdapter();


mAdapter.addSeparatorItem(new ContentWrapper(q.get(0).getA_name(),null));
mAdapter.addItem(new ContentWrapper(q.get(0).getAS_name(), q.get(0).getDesc_art()));


for (int i = 1; i < 460; i++) {

if (!(q.get(i).getA_name().trim().equals(q.get(i-1).getA_name().trim()))) {

  mAdapter.addSeparatorItem(new ContentWrapper(q.get(i).getA_name(), null));

}
mAdapter.addItem(new ContentWrapper(q.get(i).getAS_name(), q.get(i).getDesc_art()));

}

setListAdapter(mAdapter);     //error :   The method getApplicationContext() is undefined for the type ContentsFragment

}

//Adapter Class
private class MyCustomAdapter extends BaseAdapter {

private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;

private ArrayList<ContentWrapper> mData = new ArrayList<ContentWrapper>();
private LayoutInflater mInflater;

private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();

public MyCustomAdapter() {
    mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public void addItem(ContentWrapper value) {
    mData.add(value);
    notifyDataSetChanged();
}

public void addSeparatorItem(ContentWrapper value) {
    mData.add(value);
    // save separator position
    mSeparatorsSet.add(mData.size() - 1);
    notifyDataSetChanged();
}

public ContentWrapper getItem(int position) {
    return mData.get(position);
}
@Override
public int getItemViewType(int position) {
    return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
}

@Override
public int getViewTypeCount() {
    return TYPE_MAX_COUNT;
}

public int getCount() {
    return mData.size();
}

public long getItemId(int position) {
    Log.v("getItemId Position", ""+position);
    return position;

}

public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    int type = getItemViewType(position);
    if (convertView == null) {
        holder = new ViewHolder();
        switch (type) {
        case TYPE_ITEM:
            convertView = mInflater.inflate(R.layout.activity_main1, null);
            holder.textView = (TextView)convertView.findViewById(R.id.text);

            break;
        case TYPE_SEPARATOR:
            convertView = mInflater.inflate(R.layout.activity_main2, null);
            holder.textView = (TextView)convertView.findViewById(R.id.textSeparator);
            count++;
            break;
        }
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder)convertView.getTag();
    } holder.textView.setText(mData.get(position).getItem());

    if (type == TYPE_ITEM) {
        holder.textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder x = new AlertDialog.Builder(temp);
                Log.v("position",""+position);
                       x.setIcon(R.drawable.ic_launcher)

                        // .setTitle(q.get(position-count).getAS_name())
                        .setTitle(mData.get(position).getItem())

                        // .setMessage(q.get(position-count).getDesc_art())
                        .setMessage(mData.get(position).getItemDescription())

                        .setCancelable(true)
                        .setPositiveButton("OK",
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface arg,
                                            int arg1) {
                                    }
                                });
                 AlertDialog a = x.create();
                 a.show();
             }
         });
    } else {
        holder.textView.setOnClickListener(null);
    }

return convertView;
} 
}
public static class ViewHolder {
public TextView textView;
}

public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return false;
}
}

1 个答案:

答案 0 :(得分:2)

setListAdapterListFragment的一种方法。因此,您需要扩展ListFragment以使用相同的内容。

更改此

mAdapter = new MyCustomAdapter();

mAdapter = new MyCustomAdapter(getActivity());

然后

public MyCustomAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}