更改列表视图中的图标

Change icon in listview

我正在尝试更改自定义列表视图的图标,但它总是崩溃。 如何更改每个页面的图标? 例如:

我尝试在 Forloop 中添加图标,但这只会让它崩溃,我也尝试 google 它,但找不到任何解决我问题的方法。我也是最近才开始开发 android。所以我对此了解不多。

这是"Text page"它的图标设置为文本图标。

Text Page

这是 "News page" 我希望它有新闻图标,但现在是这样的:

News Page

这是我用来生成 "Text Page"

的代码
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.comp_cont_bar);

    ListView lv = (ListView) findViewById(R.id.lv1);
    registerForContextMenu(lv);

    mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipelayout);

    mSwipeRefreshLayout.setOnRefreshListener(
            new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                finish();
                startActivity(getIntent());
                }
            }
    );

    try {
    APIClient api = new APIClient();
    JSONArray result = null;

    try {
        result = api.execute("http://test.soundwave.drieo.nl/api/content" + apikey).get();
    }
    catch (Exception e) {
       e.printStackTrace();
    }

    List<CustomListView> contents = new ArrayList<CustomListView>();

    for(int i = 0; i < result.length(); i++){
        try {
            JSONObject row = result.getJSONObject(i);

            String content = row.optString("FormattedName");
            String content2 = row.optString("CreatedBy");
            String content3 = row.optString("Id");

            CustomListView item = new CustomListView();

            item.firstLine = content;
            item.description = content2;
            item.ID = content3;

            contents.add(item);

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

        ListAdapter theAdapter = new CustomAdapter(MainComp_Content.this, contents);
        ListView theListView = (ListView) findViewById(R.id.lv1);
        theListView.setAdapter(theAdapter);

        theListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

                TextView textView = (TextView) view.findViewById(R.id.tbid);
                UID = textView.getText().toString();

                Intent intent = new Intent(MainComp_Content.this, MainComp_Content_edit.class);
                intent.putExtra("uid", UID);
                startActivity(intent);
            }
        });

        theListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                TextView textView = (TextView) view.findViewById(R.id.tbid);
                UID = textView.getText().toString();
                return false;
            }
        });

    } catch (Exception e) {
        e.printStackTrace();
    }

自定义适配器代码(我在其中声明了图标):

package nl.drieo.soundwave.test.cms;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class CustomAdapter extends ArrayAdapter<CustomListView> {
public CustomAdapter(Context context, List<CustomListView> contents) {
    super(context, R.layout.custom_row, contents);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater theInflater = LayoutInflater.from(getContext());

    View theView = theInflater.inflate(R.layout.custom_row, parent, false);

    CustomListView tvFirstItem = getItem(position);

    TextView theTextView = (TextView) theView.findViewById(R.id.firstLine);
    theTextView.setText(tvFirstItem.firstLine);

    TextView theTextView2 = (TextView) theView.findViewById(R.id.secondLine);
    theTextView2.setText(tvFirstItem.description);

    TextView theTextView3 = (TextView) theView.findViewById(R.id.tbid);
    theTextView3.setText(tvFirstItem.ID);

    ImageView theImageView = (ImageView) theView.findViewById(R.id.icon);
    theImageView.setImageResource(R.drawable.ic_content);

    return theView;
}
}

我刚刚复制了 CustomAdapter class,我只是觉得有比复制粘贴更好更简洁的方法。

回答你的问题,是的,有更好的方法来实现同样的事情。例如,如果新闻和测试页面的模型相同,您可以添加一个额外的参数,如:

public class CustomListView {

// Along with your other properties

private boolean isTestPage;

public boolean isTestPage() {
    return isTestPage;
}

public void setIsTestPage(boolean isTestPage) {
    this.isTestPage = isTestPage;
}
}

然后您可以使用相同的 CustomAdapter,

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater theInflater = LayoutInflater.from(getContext());

    View theView = theInflater.inflate(R.layout.custom_row, parent, false);

    CustomListView tvFirstItem = getItem(position);

    TextView theTextView = (TextView) theView.findViewById(R.id.firstLine);
    theTextView.setText(tvFirstItem.firstLine);

    TextView theTextView2 = (TextView) theView.findViewById(R.id.secondLine);
    theTextView2.setText(tvFirstItem.description);

    TextView theTextView3 = (TextView) theView.findViewById(R.id.tbid);
    theTextView3.setText(tvFirstItem.ID);

    ImageView theImageView = (ImageView) theView.findViewById(R.id.icon);

    if (tvFirstItem.isTestPage()) {
        // Test Page Image
        theImageView.setImageResource(R.drawable.ic_test_page);
    } else{
        // News Page Image
        theImageView.setImageResource(R.drawable.ic_news_page);
    }

    return theView;
}

其他解决方案,当您的模型对于两者都不相同时,您可以创建一个名为 CustomInterface 的接口并将其实现到您的两个模型。然后在适配器中使用 CustomInterface 而不是模型,在您的 getView 中,您必须检查当前模型,如:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater theInflater = LayoutInflater.from(getContext());
    View theView = theInflater.inflate(R.layout.custom_row, parent, false);
    if(entity instanceof Obj1){
        // Test Page
    } else if(entity instanceof Obj2){
        // News Page
    }
    return theView;
}

请记住,List 仅适用于同类集合。如果您有异构集合,您可以在所有模型上实现接口并制作接口列表,然后可以将实现相同接口的任何模型放入该列表中。

但是这个解决方案真的很复杂。简单且更好的方法是为不同的视图设置不同的适配器。