0

我有一个列表视图,在它从数据库加载后,onFetchComplete 将其中一行移动到列表的顶部。这是我的工作,但我也想更改第一行的背景颜色并更改第一行的一些 TextView 文本和背景颜色。我是 android 新手,所以很难弄清楚这一点。一些帮助将不胜感激。

这是我更改一行的地方:

@Override
public void onFetchComplete(List<Application> data) {
    String myDefault = "3";
    // dismiss the progress dialog
    if(dialog != null)  dialog.dismiss();
    // create new adapter
    ApplicationAdapter adapter = new ApplicationAdapter(this, data);

    for(int i = 0; i < data.size(); ++i){
        if(data.get(i).getAppID().equals(myDefault)){
            Application app = data.get(i);
            data.remove(i);
            data.add(0, app);
        }
    }


    // set the adapter to list
    setListAdapter(adapter);
}

我曾尝试在首次加载 ListView 时使用 get view 进行一些更改,但是当我这样做时,我正在更改第一行和最后一行。我不确定它为什么这样做。另外,我不确定如何更改整行的背景。

这是我尝试过的一个例子:

    @Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;

    if(v == null) {
        LayoutInflater li = LayoutInflater.from(getContext());
        v = li.inflate(R.layout.app_custom_list, null);            
    }

    Application app = items.get(position);

    if(app != null) {
        ImageView icon = (ImageView)v.findViewById(R.id.iconImg);
        TextView titleText = (TextView)v.findViewById(R.id.titleTxt);
        TextView descText = (TextView)v.findViewById(R.id.descTxt);            
        TextView creatorText = (TextView)v.findViewById(R.id.creatorTxt);
        TextView createdText = (TextView)v.findViewById(R.id.createdTxt);            
        TextView rankText = (TextView)v.findViewById(R.id.rankTxt);

        if(icon != null) {
            Resources res = getContext().getResources();
            String sIcon = "com.sj.jsondemo:drawable/" + app.getIcon();
            icon.setImageDrawable(res.getDrawable(res.getIdentifier(sIcon, null, null)));
        }

        if(position==0)
        {
            if(titleText != null) {
                titleText.setText(app.getTitle());
                titleText.setBackgroundColor(Color.parseColor("#E4D0C6"));
            }
            if(descText != null) descText.setText(app.getDesc());
            if(creatorText != null) creatorText.setText(app.getCreator());            
            if(createdText != null) createdText.setText(app.getCreated());            
            if(rankText != null) rankText.setText(app.getRank()); 
        }
        else
        {
            if(titleText != null) titleText.setText(app.getTitle());
            if(descText != null) descText.setText(app.getDesc());
            if(creatorText != null) creatorText.setText(app.getCreator());            
            if(createdText != null) createdText.setText(app.getCreated());            
            if(rankText != null) rankText.setText(app.getRank());                   
        }


    }

    return v;
}
4

0 回答 0