0

Androidアプリケーションのテキストとリストビュー内にカスタムグリッドビューを作成する必要があります。

ここでは、グリッドビューにカテゴリ名を表示する必要があり、カテゴリ名が一致する場合、リストを表示する必要があることを意味し、対応するカテゴリ名の下のリストビューに表示されます。ここでは、以下のチュートリアルを参照しました:私の参照チュートリアル

カテゴリ名が notchbolly の場合、gridview.here にカテゴリ名を表示しました。これは、ノッチボリー カテゴリ名の上の水平リストビューにグリッドビュー リスト アイテムが表示されることを意味します。

しかし、ここでアプリを実行する必要があるのは、else 部分がすべてのカテゴリ名でのみ実行されることを意味します。

以下のコードを使用しました:

public class LazyAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater = null;
public ImageLoader imageLoader;
ListAdapter adapter;
public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
    activity = a;
    data = d;
    inflater = (LayoutInflater) activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader = new ImageLoader(activity.getApplicationContext());
}

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

public Object getItem(int position) {
    return position;
}

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

public View getView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    if (convertView == null)
        vi = inflater.inflate(R.layout.list_row, null);

    TextView title = (TextView) vi.findViewById(R.id.title); 
    ListView imageView = (ListView) vi
            .findViewById(R.id.grid_item_image);

            HashMap<String, String> item = new HashMap<String, String>();
    item = data.get(position);
    // Setting all values in listview
    title.setText(item.get(MainActivity.KEY_TITLE));

        if (item.equals("notchbolly")) {
                 listview.setAdapter(this);
            }  else {
                    listview.setBackgroundResource(R.drawable.rihanna);
                } 
         return vi;
               }

MainActivity.java:

public class MainActivity extends Activity {
  static final String URL = "http://webservices/new_feed_articls.xml";
 static String KEY_CATEGORY = "Categories";
 static final String KEY_TITLE = "Category";
 static final String KEY_TITLENAME = "name";

 LazyAdapter adapter;

 @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
      final ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
    XMLParser parser = new XMLParser();
        String xml = parser.getXmlFromUrl(URL); // getting XML from URL
        Document doc = parser.getDomElement(xml); // getting DOM element

        NodeList nl = doc.getElementsByTagName(KEY_TITLE);

        // looping through all song nodes &lt;song&gt;
        for (int i = 0; i < nl.getLength(); i++) {
            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>   ();
            Element e = (Element) nl.item(i);
             map.put( KEY_TITLE,((Element)nl.item(i)).getAttribute(KEY_TITLENAME));
             // map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
            songsList.add(map);
        }
        GridView list = (GridView) findViewById(R.id.listView1);
    adapter = new LazyAdapter(this, songsList);
     list.setAdapter(adapter);

これらの解決策を教えてください。私のコードで何が間違っていますか????

編集:

list_row.xml

<?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="fill_parent"
android:orientation="vertical"
   >

  <com.example.notch.HorizontalListView
android:id="@+id/grid_item_image"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="#FFFFFF"
 />
<TextView
    android:id="@+id/title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"

    >
</TextView>

</LinearLayout>

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" 
android:background="#FFFFFF"
android:orientation="vertical">

 <GridView
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
/>

カテゴリ名が notchbolly の場合、gridview.here にカテゴリ名を表示しました。これは、ノッチボリー カテゴリ名の上の水平リストビューにグリッドビュー リスト アイテムが表示されることを意味します。

しかし、ここでアプリを実行する必要があるのは、else 部分がすべてのカテゴリ名でのみ実行されることを意味します。

ここではelse部分のみが実行されました...

4

2 に答える 2

0

細かなところは修正していただきたいところもあると思います。

番号 1:XML file line #11: Error inflating class

これは、XML レイアウト ファイルで、質問からログを削除したため、クラスをインフレートするときに問題があるかどうかわからないことを意味します。これは、com.example.notch.HorizontalListViewlist_row.xml でインフレートするときに発生する可能性が最も高いです。Eclipse の [ Graphical Layout ] タブを確認してください。通常、カスタム ビューを作成できなかったことを示すエラーが表示されます。線に沿った何か:

The following classes could not be found:
- com.example.custom.view (Change to android.view.View, Fix Build Path, Edit XML, Create Class)

番号 2: ListView をカスタム ビューにキャストしているようです。getView() メソッドから取得:

ListView imageView = (ListView) vi
        .findViewById(R.id.grid_item_image);

list_row のどこで、grid_item_image はcom.example.notch.HorizontalListView. これにより、いくつかの小さな問題も発生する可能性があります。

番号 3: アダプターのアイテム内にアダプターを設定するのはなぜですか? :crazyFace: グリッドビュー内にリストビューを配置する予定がある場合は、2 つの異なるアダプターとビューの 2 つの異なるレイアウトを作成します。

繰り返しますが、getView() から:

if (item.equals("notchbolly")) {
             listview.setAdapter(this);
        } 

ところで、上記の 3 行にヌル ポインター例外がありますが、listviewこれはアダプターで定義されていない項目です。あなたListViewが定義した は、別の識別子: を持っているimageViewので、それもチェックしたいと思います。

于 2013-02-18T18:08:00.770 に答える
0

たとえば、5 つのギャラリー コントロールを備えた ScrollView + LinearLayout (5 つのカテゴリがある場合)。

于 2013-02-16T05:47:00.523 に答える