2012-12-04 2 views
0

안드로이드리스트 뷰 어플리케이션에서 xml 파싱을 통해 카테고리, 하위 카테고리 및 제품 정보를 전달해야합니다.제품이 xml 파싱을 사용하여 안드로이드리스트 뷰의 카테고리에 속합니다.

This is my xml feed: 

<Feed> 
    <category> 
     <Category> 
      <Categoryname>Books</Categoryname> 
      <SubCategory> 
       <subcategoryname>Internet</subcategoryname> 
       <Product> 
        <Name>New Masters of Flash</Name> 
        <Price>79.99</Price> 
       </Product> 
       <Product> 
        <Name>Professional Java Server Programming</Name> 
        <Price>63.99</Price> 
       </Product> 
      </SubCategory> 
      <SubCategory> 
       <subcategoryname>Software</subcategoryname> 
       <Product> 
        <Name>Krishnaveni</Name> 
        <Price>79.99</Price></Product> 
       <Product> 
        <Name>Veeman</Name> 
        <Price>63.99</Price> 
       </Product> 
      </SubCategory> 
     </Category> 
    </category> 
</Feed> 

여기 몇 가지 문제가 있습니다.

내가 응용 프로그램을 실행할 필요는

----> 주요 활동 만 표시 책 (카테고리)를 의미합니다. ----> 도서 목록 뷰를 클릭해야 함은 (하위 카테고리)를 얻는다는 것을 의미합니다. 그들은 다음과 같습니다

Internet 
Software 

----> 내가 수단이 특정 (인터넷) 범주의 제품을 얻고있다 인터넷을 클릭해야합니다 후. 는 그들은 : 단지 내가 몇 가지 문제에 직면 한 다음

Krishnaveni 
Veeman 

:

New Masters of Flash 
Professional Java Server Programming 

내가 2 차 하위 범주 (소프트웨어) 수단 내가 아래의 결과를 얻을 수있는 오전을 클릭해야합니다.

여기는 인터넷 하위 카테고리를 클릭해야합니다. 4 가지 제품을 얻는 것을 의미합니다. 소프트웨어에 대한 방법도 있습니다. 소프트웨어 하위 카테고리를 클릭하면 4 가지 제품을 얻을 수 있습니다.

어떻게 내가 this.please 도움 me.how 특정 하위 범주에 대한 제품을 얻을 수 있습니다.

이 내 코딩 일부입니다

public class AndroidXMLParsingActivity extends Activity { 
static final String URL = "http://192.168.1.168/xcart432pro/feed.xml"; 
private static final int Tech = 0; 

static String KEY_CATEGORY = "Category"; 
static final String KEY_TITLE = "Categoryname"; 

ListView lview3; 
LazyAdapter adapter; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.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_CATEGORY); 

     // 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, parser.getValue(e, KEY_TITLE)); 

      songsList.add(map); 
     } 
    lview3 = (ListView) findViewById(R.id.listView1); 
    adapter = new LazyAdapter(this, songsList); 
    lview3.setAdapter(adapter); 

    lview3.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 
      HashMap<String, String> map = songsList.get(position); 
     switch (position) 
     { 
      case Tech: 
       Intent tech = new Intent(AndroidXMLParsingActivity.this, com.androidhive.xmlparsing.SubCate.class); 
       tech.putExtra(KEY_TITLE, "Books"); 
       startActivity(tech); 
       break; 


      default: 
       break; 
     } 
     } 

    }); 

두 번째 활동 :

public class SubCate extends Activity { 
static final String URL = "http://192.168.1.168/xcart432pro/feed.xml"; 
private static final int Tech = 0; 
private static final int Sport =1; 
static String KEY_CATEGORY = "SubCategory"; 
static final String KEY_SUBCATE = "subcategoryname"; 
ListView lview3; 
ListViewCustomAdapter adapter; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.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_CATEGORY); 

     // 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); 
      // adding each child node to HashMap key =&gt; value 

      map.put(KEY_SUBCATE, parser.getValue(e, KEY_SUBCATE)); 

      songsList.add(map); 
     } 
    lview3 = (ListView) findViewById(R.id.listView1); 
    adapter = new ListViewCustomAdapter(this, songsList); 
    lview3.setAdapter(adapter); 

    lview3.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 
      HashMap<String, String> map = songsList.get(position); 
     switch (position) 
     { 
      case Tech: 
       Intent tech = new Intent(SubCate.this, com.androidhive.xmlparsing.Catalogue.class); 
       tech.putExtra(KEY_SUBCATE, "Internet"); 
       startActivity(tech); 
       break; 
      case Sport: 
       Intent sport = new Intent(SubCate.this, com.androidhive.xmlparsing.Catalogue.class); 
       sport.putExtra(KEY_SUBCATE, "Software"); 
        startActivity(sport); 
       break; 


      default: 
       break; 
     } 
     } 

    }); 

세 번째 활동 :

public class Catalogue extends Activity { 

// static String URL = "https://dl.dropbox.com/u/48258247/catalogue.json"; 
static final String URL = "http://192.168.1.168/xcart432pro/feed.xml"; 

static String KEY_CATEGORY = "Product"; 


static final String KEY_TITLE = "Name"; 

static final String KEY_DESCRIPTION = "Description"; 
static final String KEY_COST = "Price"; 
//static final String KEY_THUMB_URL = "Image"; 

ListView list; 
ListAdapter adapter; 

/** Called when the activity is first created. */ 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.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_CATEGORY); 

     // 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); 
      // adding each child node to HashMap key =&gt; value 

      map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE)); 
      map.put(KEY_DESCRIPTION, parser.getValue(e, KEY_DESCRIPTION)); 
      map.put(KEY_COST, parser.getValue(e, KEY_COST)); 
      // map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL)); 

      // adding HashList to ArrayList 
      songsList.add(map); 
     } 

     list=(ListView)findViewById(R.id.listView1); 

     // Getting adapter by passing xml data ArrayList 
     adapter=new ListAdapter(this, songsList); 
     list.setAdapter(adapter); 


     System.out.println("Category is:- " + KEY_CATEGORY); 
     // Click event for single list row 
     list.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View view, 
        int position, long id) { 
       HashMap<String, String> map = songsList.get(position); 

      } 

     }); 

코드를 확인하고 this.please 도움을 나에게 솔루션을 제공하십시오 나.

편집 : 나는 내 두번째 활동에 코드를 추가 할 필요가

(SubCate.java)

이 첫 번째 줄 아래에 추가에서 onCreate 함수 후 KEY_TITLE

static final String KEY_TITLE = "Categoryname"; 

을 선언

Bundle bdl = getIntent().getExtras(); 
    KEY_CATEGORY = bdl.getString(KEY_TITLE); 

나는 adde d 3 번째 코드 (Catalogue.java)에도 코드가 있습니다.()에서 onCreate 후 라인 아래에 추가 기능이 후

static final String KEY_SUBCATE = "subcategoryname"; 

:

먼저 KEY_SUBCATE를 선언

 Bundle bdl = getIntent().getExtras(); 
     KEY_CATEGORY = bdl.getString(KEY_SUBCATE); 

지금 내가 응용 프로그램을 실행할 필요는 오전 어떤 제품을 얻을하지 않습니다 의미 . 제발 help me.how 나는 올바른 카테고리에 제품을 얻을 수 있습니다.

답변

0

그냥 하위 범주를 클릭하기 전에 모든 제품 final을 확인하십시오. 나는 당신의 문제가 해결 될 것이라고 생각합니다. 만약 당신이 최종적인 가치를 만들지 않는다면 매번 마지막 자리를 차지할 것입니다. ---

final String key_title=parser.getValue(e, KEY_TITLE); 
final String key_desc=parser.getValue(e, KEY_DESCRIPTION); 
fiinal String key_cost=parser.getValue(e, KEY_COST); 


    map.put(KEY_TITLE, key_title); 
     map.put(KEY_DESCRIPTION, key_desc); 
     map.put(KEY_COST, key_cost); 
+0

내가 여기 변경해야 detailly.which 파일과 줄을 설명해주십시오 : ---

 map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE)); map.put(KEY_DESCRIPTION, parser.getValue(e, KEY_DESCRIPTION)); map.put(KEY_COST, parser.getValue(e, KEY_COST)); 

그냥 그것을 바꾸기 :

나는 여기에 제품 양식을 보내 생각합니다. – user1859172

+0

예를 들면 다음과 같습니다. -final int temp_veriable = ur_current_value; 그런 다음 temp_variable을 넣으면 ur 위치에 따라 값이 적용됩니다. –

+0

나는 모든 제품에 대해서만 최종 값을 사용했다. 내 코드를 확인한다. – user1859172

관련 문제